Go: genprop
Generates getter/setter functions for private fields.
INSTALL
CLI
go install github.com/hidori/go-genprop/cmd/genprop@latest
Docker
docker run --rm -w $PWD -v $PWD:$PWD hidori/genprop@latest example.go > example.prop.go
USAGE
usage: genprop [OPTION]... <FILE>
option(s):
-initialism string
names to which initialism should be applied (default "id,url,api")
-validation-func string
specify validation func name (default "validateFieldValue")
-validation-tag string
specify validation tag name (default "validate")
EXAMPLE
example.go
package example
type ExampleAliasType = int // ignored
type ExampleDefinedType int // ignored
type ExampleStruct struct {
value0 int // ignored
value1 int `property:"get"`
value2 int `property:"set"`
value3 int `property:"get,set"`
id int `property:"get"`
api string `property:"get"`
url string `property:"get"`
http string `property:"get"`
}
RUN
genprop example.go > example.prop.go
or
docker run --rm -w $PWD -v $PWD:$PWD hidori/genprop@latest example.go > example.prop.go
OUTPUT
example.prop.go
// Code generated by github.com/hidori/go-genprop/cmd/genprop DO NOT EDIT.
package example
func (t *ExampleStruct) GetValue1() int {
return t.value1
}
func (t *ExampleStruct) SetValue2(v int) {
t.value2 = v
}
func (t *ExampleStruct) GetValue3() int {
return t.value3
}
func (t *ExampleStruct) SetValue3(v int) {
t.value3 = v
}
func (t *ExampleStruct) GetID() int {
return t.id
}
func (t *ExampleStruct) GetAPI() string {
return t.api
}
func (t *ExampleStruct) GetURL() string {
return t.url
}
func (t *ExampleStruct) GetHttp() string {
return t.http
}
EXAMPLE2
example2.go
package example
import (
"github.com/go-playground/validator/v10"
"github.com/pkg/errors"
)
var _validator = validator.New()
func validateFieldValue(v any, tag string) error {
if err := _validator.Var(v, tag); err != nil {
return errors.WithStack(err)
}
return nil
}
type Example2AliasType = int // ignored
type Example2DefinedType int // ignored
type Example2Struct struct {
value0 int // ignored
value1 int `property:"get"`
value2 int `property:"set"`
value3 int `property:"get,set"`
value4 int `property:"get,set" validate:"required,min=1"`
id int `property:"get"`
api string `property:"get"`
url string `property:"get"`
http string `property:"get"`
}
func NewExample2Struct(v2 int, v3 int, v4 int) (*Example2Struct, error) {
v := &Example2Struct{}
v.SetValue2(v3)
v.SetValue3(v3)
err := v.SetValue4(v4)
if err != nil {
return nil, errors.WithStack(err)
}
return v, nil
}
RUN
genprop example2.go > example2.prop.go
or
docker run --rm -w $PWD -v $PWD:$PWD hidori/genprop@latest example2.go > example2.prop.go
OUTPUT
example2.prop.go
// Code generated by github.com/hidori/go-genprop/cmd/genprop DO NOT EDIT.
package example
func (t *Example2Struct) GetValue1() int {
return t.value1
}
func (t *Example2Struct) SetValue2(v int) {
t.value2 = v
}
func (t *Example2Struct) GetValue3() int {
return t.value3
}
func (t *Example2Struct) SetValue3(v int) {
t.value3 = v
}
func (t *Example2Struct) GetValue4() int {
return t.value4
}
func (t *Example2Struct) SetValue4(v int) error {
err := validateFieldValue(v, "required,min=1")
if err != nil {
return err
}
t.value4 = v
return nil
}
func (t *Example2Struct) GetID() int {
return t.id
}
func (t *Example2Struct) GetAPI() string {
return t.api
}
func (t *Example2Struct) GetURL() string {
return t.url
}
func (t *Example2Struct) GetHttp() string {
return t.http
}