accessory-generator

command module
v0.2.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 23, 2022 License: MIT Imports: 3 Imported by: 0

README

accessory-generator

Original repo: github.com/masaushi/accessory

test release

accessory-generator is an accessor generator for Go programming language.

What is accessory-generator?

Accessory is a tool that generates accessor methods from any structs.

Sometimes you might make struct fields unexported in order for values of fields not to be accessed or modified from anywhere in your codebases, and define getters or setters for values to be handled in a desired way.

But writing accessors for so many fields is time-consuming, but not exciting or creative.

Accessory frees you from tedious, monotonous tasks.

Installation

To get the latest released version

Go version < 1.16
go get github.com/Marble-Technologies/accessory-generator
Go 1.16+
go install github.com/Marble-Technologies/accessory-generator@latest

Usage

Declare Struct with accessor Tag

accessory-generator generates accessor methods from defined structs, so you need to declare a struct and fields with accessor tag.

Values for accessor tag is getter and setter, getter is for generating getter method and setter is for setter methods.

Here is an example:

type MyStruct struct {
    Field1 string    `accessor:"getter"`
    field2 *int      `accessor:"setter"`
    Field3 time.Time `accessor:"getter,setter"`
}

Generated methods will be

func(m *MyStruct) GetField1() string {
    if m == nil {
        return ""
    }
    return m.field1
}

func(m *MyStruct) SetField2(val *int) {
    if m == nil {
        return
    }
    m.field2 = val
}

func(m *MyStruct) GetField3() time.Time {
    if m == nil {
        return time.Time{}
    }
    return m.field3
}

func(m *MyStruct) SetField3(val time.Time) {
    if m == nil {
        return
    }
    m.field3 = val
}
Default Setters and Getters naming

Setter's name is Set<FieldName>() and getter's name is Get<FieldName>() by default.

Note: Adding Get prefix to the getter function is NOT recommended see convention, but by default accessory-generator assumes the struct fields can be exported (mainly to support the encoding/json Marshal/Unmarshal)

You can customize names for setter and getter if you want.

type MyStruct struct {
    field1 string `accessor:"getter:GetFirstField"`
    field2 int    `accessor:"setter:ChangeSecondField"`
}

Generated methods will be

func(m *MyStruct) GetFirstField() string {
    if m == nil {
        return ""
    }
    return m.field1
}

func(m *MyStruct) ChangeSecondField(val *int) {
    if m == nil {
        return
    }
    m.field2 = val
}

Accessor methods won't be generated if accessor tag isn't specified. But you can explicitly skip generation by using - for tag value.

type MyStruct struct {
    ignoredField `accessor:"-"`
}
Run accessory-generator command

To generate accessor methods, you need to run accessory-generator command.

$ accessory-generator [flags] source-dir

source-dir
  source-dir is the directory where the definition of the target struct is located.
  If source-dir is not specified, current directory is set as source-dir.

Flags:
  -json
        add json marshal function, function name is Bytes
  -lock string
        lock name
  -output string
        output file name; default <type_name>_accessor.go
  -pointer
        pointer receiver or value receiver, default values receiver (false)
  -receiver string
        receiver name; default first letter of type name
  -type string
        type name; must be set
  -version
        show the version of accessory

Example:

$ accessory-generator -type MyStruct -receiver myStruct -output my_struct_accessor.go path/to/target
go generate

You can also generate accessors by using go generate.

package mypackage

//go:generate accessory-generator -type MyStruct -receiver myStruct -output my_struct_accessor.go

type MyStruct struct {
    field1 string `accessor:"getter"`
    field2 *int   `accessor:"setter"`
}

Then run go generate for your package.

License

The Accessory project (and all code) is licensed under the MIT License.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL