accessory

command module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2023 License: MIT Imports: 3 Imported by: 0

README

accessory

test release

accessory is an accessor generator for Go programming language.

What is accessory?

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/masaushi/accessory
Go 1.16+
go install github.com/masaushi/accessory@latest

Usage

Declare Struct with accessor Tag

accessory 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) Field1() string {
    if m == nil {
        return ""
    }
    return m.field1
}

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

func(m *MyStruct) Field3() 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
}

Following to convention, setter's name is Set<FieldName>() and getter's name is <FieldName>() by default, in other words, Set will be put into setter's name and Get will not be put into getter's name.

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 command

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

$ accessory [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
  -type string <required>
      name of target struct

  -receiver string
      receiver receiver for generated accessor methods
      default: first letter of struct

  -output string
      output file name
      default: <type_name>_accessor.go

  -version
      show the current version of accessory

Example:

$ accessory -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 -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