Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 17, 2021 05:16 am GMT

Generating accessor methods easily

Hi all,
I have made a go tool to generate accessor methods(getter or setter) for unexported struct fields, so let me introduce it.

https://github.com/masaushi/accessory

GitHub logo masaushi / accessory

accessor methods generator for Go programming language

accessory

testrelease

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 accessedor 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

Sometimes you might make struct fields unexported in order for values of the fields not to be accessed or modified from anywhere in your codebases.

Making struct fields unexported will prevent data from unexpected use or change.
But you sometimes might need accessor methods for some fields to get/set data in a desired way.

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

This tool frees you from that tedious, monotonous task.

Example

1. Declare a model with accessor tag.
type MyStruct struct {    field1 string    `accessor:"getter"`    field2 *int      `accessor:"setter"`    field3 time.Time `accessor:"getter,setter"`}
2. Run accessory command
$ accessory -type MyStruct .
3. Accessor methods will be generated.
func(m *MyStruct) Field1() string {    return m.field1}func(m *MyStruct) SetField2(val *int) {    m.field2 = val}func(m *MyStruct) Field3() time.Time {    return m.field3}func(m *MyStruct) SetField3(val time.Time) {    m.field3 = val}
4. You can customize method name
type MyStruct struct {    field1 string `accessor:"getter:GetFirstField"`    field2 int    `accessor:"setter:ChangeSecondField"`}

Generated methods will be

func(m *MyStruct) GetFirstField() string {    return m.field1}func(m *MyStruct) ChangeSecondField(val *int) {    m.field2 = val}
5. You can also generate accessors with go generate
//go:generate accessory -type MyStructtype MyStruct struct {    field1 string `accessor:"getter"`    field2 *int   `accessor:"setter"`}

Conclusion

Generating accessors easily will save your time and help you focus on writing important business logics.

I'm looking forward to getting your feedback.

Have a wonderful day!


Original Link: https://dev.to/masaushi/generating-accessor-methods-easily-3p61

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To