pkg

package module
v0.0.15 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2024 License: MIT Imports: 2 Imported by: 0

README

Go: genprop

Generates getter/setter functions for private fields.

INSTALL

CLI
go install github.com/hidori/go-genprop/cmd/genprop@latest
Docker
docker pull hidori/genprop:latest

USAGE

usage: genprop [OPTION]... <FILE>

option(s):
  -version
        show version information
  -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

INPUT

example.go

package example

import (
 "github.com/go-playground/validator/v10"
 "github.com/pkg/errors"
)

type Struct struct {
 value1 int `property:"get"`
 value2 int `property:"set"`
 value3 int `property:"get,set"`
 value4 int `property:"set=private"`
 value5 int `property:"get,set" validate:"min=1,max=100"`
 value6 int `property:"get,set=private" validate:"min=1,max=100"`
}

var _validator = validator.New()

func validateFieldValue(name string, v any, tag string) error {
 if err := _validator.Var(v, tag); err != nil {
  return errors.Wrapf(errors.WithStack(err), "fail to validator.Var() name='%s'", name)
 }

 return nil
}

func NewStruct(v1 int, v2 int, v3 int, v4 int, v5 int, v6 int) (*Struct, error) {
 v := &Struct{
  value1: v1, // has no setter
 }

 v.SetValue2(v2)
 v.SetValue3(v3)
 v.setValue4(v4)

 err := v.SetValue5(v5)
 if err != nil {
  return nil, errors.WithStack(err)
 }

 err = v.setValue6(v6)
 if err != nil {
  return nil, errors.WithStack(err)
 }

 return v, nil
}
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 *Struct) GetValue1() int {
 return t.value1
}
func (t *Struct) SetValue2(v int) {
 t.value2 = v
}
func (t *Struct) GetValue3() int {
 return t.value3
}
func (t *Struct) SetValue3(v int) {
 t.value3 = v
}
func (t *Struct) setValue4(v int) {
 t.value4 = v
}
func (t *Struct) GetValue5() int {
 return t.value5
}
func (t *Struct) SetValue5(v int) error {
 err := validateFieldValue(v, "min=1,max=100")
 if err != nil {
  return err
 }
 t.value5 = v
 return nil
}
func (t *Struct) GetValue6() int {
 return t.value6
}
func (t *Struct) setValue6(v int) error {
 err := validateFieldValue(v, "min=1,max=100")
 if err != nil {
  return err
 }
 t.value6 = v
 return nil
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetVersion added in v0.0.12

func GetVersion() string

Types

This section is empty.

Directories

Path Synopsis
cmd
Code generated by github.com/hidori/go-genprop/cmd/genprop DO NOT EDIT.
Code generated by github.com/hidori/go-genprop/cmd/genprop DO NOT EDIT.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL