fat

package
v3.2.13+incompatible Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2015 License: MIT, MIT Imports: 10 Imported by: 0

README

fat

Fat fields and structs for go

Idea

have one struct and use it:

  • to validate data
  • to set the values
  • to have default values
  • to allow further useful things 3rd party libraries could do with structs, like:
    • fill with values from posted forms
    • use them as placeholders in templates (with escaping)
    • construct urls based on them
    • to database operations, like creating a table o querying

a fat field is a field that knows about its name, its validation, its type and the name of its struct, all without resorting to field tags and without code duplication

The third party libraries could allow operations for single fat fields or more of them by associating the field path (which includes the field name and the struct type name) with special field tags in a registry, so that every struct needs to be registered once.

Then the library could offer top level functions to return specialized objects for a given field that knows its path and therefor those fat structs might be extended from the outside with arbitrary data.

Examples

see example directory

Documentation

Index

Constants

This section is empty.

Variables

View Source
var StringMustNotBeEmpty = ValidaterFunc(func(f *Field) (err error) {

	if f.String() == "" {
		return fmt.Errorf("must not be empty")
	}
	return
})

Functions

func Bool

func Bool(b bool) *bool_

func Bools

func Bools(bools ...bool) *slice

func Float

func Float(f float64) *float_

func Floats

func Floats(floats ...float64) *slice

func Int

func Int(i int64) *int_

func Ints

func Ints(ints ...int64) *slice

func Map

func Map(givenmap interface{}) *map_

func MapBools

func MapBools(m map[string]bool) *map_

func MapFloats

func MapFloats(m map[string]float64) *map_

func MapInts

func MapInts(m map[string]int64) *map_

func MapStrings

func MapStrings(m map[string]string) *map_

func MapTimes

func MapTimes(m map[string]time.Time) *map_

func MapType

func MapType(typ string, vals ...interface{}) *map_

panics if there are different types

func New

func New(øprototype interface{}, ønewstruct interface{}) (ønew interface{})

prefills the given newstruct based on the given prototype

func Proto

func Proto(østruct interface{}) (østru interface{})

sets all attributes of a struct that are of type *Field to a *Field with the Type set to what is given in the tag "fat.type" with defaults set to what is given in the tag "fat.default" and with enums set to what is in the tag "fat.enum", separated by pipe symbols (|)

func Slice

func Slice(typ string, intfs ...interface{}) *slice

panics if there are different types

func String

func String(s string) *string_

func Strings

func Strings(strings ...string) *slice

func StructType

func StructType(østruct interface{}) string

func Time

func Time(t time.Time) *time_

func Times

func Times(times ...time.Time) *slice

func Validaters

func Validaters(vs ...Validater) func(f *Field) (errs []error)

Types

type Field

type Field struct {
	Type
	Struct interface{} // a reference to the struct

	IsSet bool // is true, if the value was set, i.e. the type was correct, it may however by invalid
	// saves the input for a failed scan,
	// is empty if the scan did not fail
	FailedScanInput string
	// contains filtered or unexported fields
}

func (Field) Default

func (øfieldSpec Field) Default() Type

func (Field) Enum

func (øfieldSpec Field) Enum() []Type

func (*Field) Get

func (øField *Field) Get() (i interface{})

overwrite Type.Get to return default value, if IsSet is false

func (*Field) IsValid

func (øField *Field) IsValid() bool

returns if field is valid

func (*Field) MarshalJSON

func (øField *Field) MarshalJSON() ([]byte, error)

func (*Field) MustScanAndValidate

func (øField *Field) MustScanAndValidate(s string)

func (*Field) MustScanString

func (øField *Field) MustScanString(s string)

calls Scan and panics if there is an error

func (*Field) MustSet

func (øField *Field) MustSet(i interface{})

calls Set and panics if there is an error

func (*Field) MustValidate

func (øField *Field) MustValidate()

panics on first error

func (Field) Name

func (øfieldSpec Field) Name() string

func (Field) New

func (øfieldSpec Field) New(østruct interface{}) *Field

func (Field) Path

func (øfieldSpec Field) Path() string

func (*Field) Scan

func (øField *Field) Scan(src interface{}) error

func (*Field) ScanAndValidate

func (øField *Field) ScanAndValidate(s string) (errs []error)

func (*Field) ScanString

func (øField *Field) ScanString(s string) error

overwrite Type.Scan to track, if the field was set field only is set if scan was successful Scan does not validation check, that must be run after Scan, or use ScanAndValidate

func (*Field) Set

func (øField *Field) Set(i interface{}) error

overwrite Type.Set to track, if the field was set field may be set and invalid at the same time IsSet only tells us, wether the type is correct

func (*Field) String

func (øField *Field) String() string

overwrite Type.String to return default value, if IsSet is false

func (Field) StructType

func (øfieldSpec Field) StructType() string

func (*Field) UnmarshalJSON

func (øField *Field) UnmarshalJSON(data []byte) (err error)

func (*Field) Validate

func (øField *Field) Validate() (errs []error)

validates the content of field and returns all errors

func (*Field) Value

func (øField *Field) Value() (v driver.Value, err error)

type Type

type Type interface {
	Typ() string
	Get() interface{}  // must be typecasted
	String() string    // represent value as string
	Scan(string) error // sets the value based on a string
	Set(interface{}) error
}

type Validater

type Validater interface {
	Validate(*Field) error
}

type ValidaterFunc

type ValidaterFunc func(*Field) error

func (ValidaterFunc) Validate

func (vf ValidaterFunc) Validate(f *Field) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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