accessory

command module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2023 License: MIT Imports: 3 Imported by: 0

README

accessory

lint and test release

Accessory is a tool designed for the Go programming language that automates the generation of accessor methods for struct fields. It helps developers maintain encapsulation by allowing struct fields to be unexported, while still providing controlled access through generated getter and setter methods.

Why Use Accessory?

  • Encapsulation: Keep your struct fields unexported, ensuring they're not accessed or modified unexpectedly.
  • Automation: Writing accessors for numerous fields can be repetitive. Accessory automates this task, saving you time.
  • Customization: While the tool follows Go conventions by default, you can customize accessor names as needed.

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
or Homebrew for macOS or Linux
brew install masaushi/tap/accessory

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:"-"`
}
Specify rules for setting value

You can specify validation rules for each fields. We underlying the

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 <optional>
      receiver receiver for generated accessor methods
      default: first letter of struct

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

  -lock string <optional>
      specify lock field name and generate codes obtaining and releasing lock
      this is used to prevent race condition when concurrent access can be expected

  -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