traits

package module
v0.0.0-...-d4ee804 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2021 License: MIT Imports: 17 Imported by: 0

README

go-traits

Test PkgGoDev Go Report Card

go-traits is a concept package that helps implement mixin behavior using embedded structs and hook interfaces.

Trait list:

  • traits.Hasher - An extension for unique hash generators.
  • traits.Converter - An extension for miscellaneous converters.
  • traits.Stringer - fmt.Stringer implementation extension.
  • traits.Validator - Struct fields validation extension.
  • traits.Default - Struct fields initialization extension.

Marker trait list:

  • traits.PreventUnkeyed - A struct to embed when you need to forbid unkeyed literals usage.
  • traits.NonComparable - A struct to embed when you need to prevent structs comparison.

Hook interfaces:

  • traits.bootstrap - the Bootstrap function will be triggered on traits.Init call.
  • traits.schedule - implement traits.schedule to schedule a function in a separate goroutine on traits.Init call.
  • traits.finalize - the Finalize function will be set as an object finalizer via runtime.SetFinalizer on traits.Init call.

Examples

type inner struct {
	Arr []bool
}

type test struct {
	traits.Hasher
	traits.Converter
	traits.Stringer
	traits.Validator

	Num   int    `json:"num"`
	Str   string `json:"str" valid:"numeric"`
	Inn   *inner
	pstr  *string
	C     chan interface{} `json:"-"`
	Iface interface{}
}

func (t *test) Bootstrap() {
	fmt.Println("Bootstrap Test struct...")
}

func (t *test) Finalize() {
	fmt.Println("Finalize Test struct...")
}

func main() {
	str := "bar"
	obj := test{
		Num:   1,
		Str:   "abc",
		Inn:   &inner{make([]bool, 2)},
		pstr:  &str,
		C:     make(chan interface{}),
		Iface: "foo",
	}
	traits.Init(&obj)

	fmt.Println(obj.String())
	fmt.Println(obj.ToJSON())
	fmt.Println(obj.Md5Hex())
	fmt.Println(obj.Sha256Hex())
	fmt.Println(obj.HashCode32())
	fmt.Println(obj.Validate())
}

See the examples folder for more.

Contributing

Any proposal or improvement is very welcome.

License

Licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init(obj interface{})

Init is the mandatory function to call in order to initialize traits for the specified object.

Types

type Converter

type Converter struct {
	// contains filtered or unexported fields
}

Converter provides various marshalling methods to an embedding struct.

func (*Converter) Keys

func (conv *Converter) Keys() []string

Keys returns a slice of exported field names of an embedding struct

func (*Converter) ToJSON

func (conv *Converter) ToJSON() (string, error)

ToJSON returns the JSON encoding of an embedding struct.

func (*Converter) ToJSONIndent

func (conv *Converter) ToJSONIndent(prefix, indent string) (string, error)

ToJSONIndent is like ToJSON but applies Indent to format the output. Each JSON element in the output will begin on a new line beginning with prefix followed by one or more copies of indent according to the indentation nesting.

func (*Converter) ToMap

func (conv *Converter) ToMap() map[string]interface{}

ToMap converts an embedding struct to a map. Skips unexported fields.

func (*Converter) ToXML

func (conv *Converter) ToXML() (string, error)

ToXML returns the XML encoding of an embedding struct.

func (*Converter) ToXMLIndent

func (conv *Converter) ToXMLIndent(prefix, indent string) (string, error)

ToXMLIndent works like ToXML, but each XML element begins on a new indented line that starts with prefix and is followed by one or more copies of indent according to the nesting depth.

func (*Converter) ToYAML

func (conv *Converter) ToYAML() (string, error)

ToYAML serializes an embedding struct into a YAML document.

func (*Converter) Values

func (conv *Converter) Values() []interface{}

Values returns a slice of exported field values of an embedding struct

type Default

type Default struct{}

Default adds default initialization to an embedding struct fields. Note that it applies to exported fields only. No public methods exposed for this trait.

type Hasher

type Hasher struct {
	// contains filtered or unexported fields
}

Hasher provides unique hash generators to an embedding struct.

func (*Hasher) HashCode32

func (h *Hasher) HashCode32() uint32

HashCode32 generates a unique uint32 hash of an embedding struct.

func (*Hasher) HashCode64

func (h *Hasher) HashCode64() uint64

HashCode64 generates a unique uint64 hash of an embedding struct.

func (*Hasher) Md5

func (h *Hasher) Md5() [16]byte

Md5 returns the MD5 checksum of an embedding struct.

func (*Hasher) Md5Hex

func (h *Hasher) Md5Hex() string

Md5Hex returns the MD5 checksum hex string representation.

func (*Hasher) Sha256

func (h *Hasher) Sha256() [32]byte

Sha256 returns the SHA256 checksum of an embedding struct.

func (*Hasher) Sha256Hex

func (h *Hasher) Sha256Hex() string

Sha256Hex returns the SHA256 checksum hex string representation.

func (*Hasher) Sha512

func (h *Hasher) Sha512() [64]byte

Sha512 returns the SHA512 checksum of an embedding struct.

func (*Hasher) Sha512Hex

func (h *Hasher) Sha512Hex() string

Sha512Hex returns the SHA512 checksum hex string representation.

type NonComparable

type NonComparable struct {
	// contains filtered or unexported fields
}

NonComparable is a struct to embed when you need to prevent structs comparison.

NonComparable trait doesn't require the `traits.Init` call.

type PreventUnkeyed

type PreventUnkeyed struct {
	// contains filtered or unexported fields
}

PreventUnkeyed is a struct to embed when you need to forbid unkeyed literals usage.

PreventUnkeyed trait doesn't require the `traits.Init` call.

type Stringer

type Stringer struct {
	// contains filtered or unexported fields
}

Stringer implements the `fmt.Stringer` interface. Adds the String method to an embedding struct.

func (*Stringer) String

func (str *Stringer) String() string

String returns a string representation of an embedding struct.

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

Validator adds the Validate function to an embedding struct, which can be used to validate structure fields by tags.

List of available validators for struct fields: "email" "url" "dialstring" "requrl" "requri" "alpha" "utfletter" "alphanum" "utfletternum" "numeric" "utfnumeric" "utfdigit" "hexadecimal" "hexcolor" "rgbcolor" "lowercase" "uppercase" "int" "float" "null" "uuid" "uuidv3" "uuidv4" "uuidv5" "creditcard" "isbn10" "isbn13" "json" "multibyte" "ascii" "printableascii" "fullwidth" "halfwidth" "variablewidth" "base64" "datauri" "ip" "port" "ipv4" "ipv6" "dns" "host" "mac" "latitude" "longitude" "ssn" "semver" "rfc3339" "rfc3339WithoutZone" "ISO3166Alpha2" "ISO3166Alpha3"

Example:

type Example struct {
		Str   string `valid:"numeric"`
		Email string `valid:"email"`
}

func (*Validator) Validate

func (v *Validator) Validate() (bool, error)

Validate uses tags to validate struct fields. The result will be equal to `false` if there are any errors.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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