An Interest In:
Web News this Week
- March 22, 2024
- March 21, 2024
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
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
accessory
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
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To