structs

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

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

Go to latest
Published: Apr 24, 2024 License: MIT Imports: 5 Imported by: 0

README

tenntenn/structs

pkg.go.dev

type A { N int `json:"n"`}
type B { S string `json:"s"`}

/*
struct{
     N int	`json:"n"`
     S string	`json:"s"`
}{
	N: 100,
	S: "sample",
}
*/
structs.Merge(A{N:100}, B{S:"sample"})
/*
struct{
     N int
     S string
}{
	N: 100,
	S: "sample",
}
*/
structs.Of(structs.F("N", 100), structs.F("S", "sample"))

/*
struct{
     N int	`json:"n"`
     S string	`json:"s"`
}{
	N: 100,
	S: "sample",
}
*/
structs.Of(structs.F("N", 100, structs.Tag(`json:"n"`)), structs.F("S", "sample", structs.Tag(`json:"s"`)))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Merge

func Merge(xs ...any) any

Merge make new struct value with the exported fields of the given structs. If some have a same name of field, the field of the last struct in the parameters is used. If non struct value is in the parameters, it would be ignored.

type A { N int `json:"n"`}
type B { S string `json:"s"`}

/*
struct{
     N int	`json:"n"`
     S string	`json:"s"`
}{
	N: 100,
	S: "sample",
}
*/
Merge(A{N:100}, B{S:"sample"})

func Of

func Of(fields ...*Field) any

Of make new struct value with the given names, values and tags as fields.

/*
struct{
     N int
     S string
}{
	N: 100,
	S: "sample",
}
*/
Of(F("N", 100), F("S", "sample"))

/*
struct{
     N int	`json:"n"`
     S string	`json:"s"`
}{
	N: 100,
	S: "sample",
}
*/
Of(F("N", 100, Tag(`json:"n"`)), F("S", "sample", Tag(`json:"s"`)))

The tags can be ommited.

Types

type Field

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

func F

func F(nameAndValue ...any) *Field

type FieldOption

type FieldOption func(*Field)

func Tag

func Tag[Tag string | reflect.StructTag](tag Tag) FieldOption

Jump to

Keyboard shortcuts

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