/argumenter

argumenter is golang argumentation struct generater.

Primary LanguageGoMIT LicenseMIT

argumenter

  • argumenter is golang argumentation struct's validation function generator.

Usage

argumenter -type T1[,T2] [-out OUTPUT] INPUT

Input code

  • file name is file.go
package main

type MyType struct {
	I  int          `arg:"default=5,min=0,max=10"`
	S  string       `arg:"default=hello"`
	SI []int        `arg:"required,lenmin=1,lenmax=4"`
	M  map[int]bool `arg:"required"`
	F  func()       `arg:"required"`
	IN interface{}  `arg:"required"`
	P  *int         `arg:"required"`
}

Generate command

argumenter -type MyType file.go

Generated code

  • output file name is file_argumenter.go
// Code generated by "argumenter -type MyType"; DO NOT EDIT
package main

import "errors"

func (m *MyType) Valid() error {

	if m.I == 0 {
		m.I = 5
	}

	if m.I < 0 {
		return errors.New("I must greater than or equal 0")
	}

	if m.I > 10 {
		return errors.New("I must less than or equal 10")
	}

	if m.S == "" {
		m.S = "hello"
	}

	if m.SI == nil {
		return errors.New("SI must not nil")
	}

	if len(m.SI) < 1 {
		return errors.New("SI length must greater than or equal 1")
	}

	if len(m.SI) > 4 {
		return errors.New("SI length must less than or equal 4")
	}

	if m.M == nil {
		return errors.New("M must not nil")
	}

	if m.F == nil {
		return errors.New("F must not nil")
	}

	if m.IN == nil {
		return errors.New("IN must not nil")
	}

	if m.P == nil {
		return errors.New("P must not nil")
	}

	return nil
}

How to use generated function

func AnyFunc(m *MyType) {
    if e := m.Valud() {
        // Validation error handling
    }
    num := m.I // I field is validated and default value filled
}

License

MIT