argumenter

command module
v0.0.0-...-d1c4a5c Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2017 License: MIT Imports: 16 Imported by: 0

README

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

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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