Documentation ¶
Overview ¶
Example (Dereference) ¶
_, c := dereference(jen.Index().String(), jen.Id("object")) fmt.Printf("%s", c.GoString())
Output: var value []string if val, ok := object.(*[]string); ok { value = *val } else { value = object.([]string) }
Index ¶
- func CaseType(t, src Code, fn func(value, t Code) Statement) Code
- func Cases(types Statement, src Code, fn func(value, t Code) Statement, ...) Statement
- func DeepCopy(t, src, dst, recursiveFunc Code) Statement
- func Foreach(src Code, keyAndValue ...string) *Statement
- func InterfaceCopy(t, src, dst, recursiveFunc Code) Statement
- func ShallowCopy(t, src, dst Code) Statement
- func SwitchType(src, dst Code, fn func(value Code) Statement) *Statement
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CaseType ¶
func CaseType(t, src Code, fn func(value, t Code) Statement) Code
CaseType renders a case for the given type and its pointer. The closure renders a list of statements for this case.
If the switch value is a pointer, it will be dereferenced.
t is the type to check, its pointer will also be checked src is the variable to dereference fn is a closure taking as parameter the dereferenced value and returning a statement.
Example ¶
switchValue := jen.Id("t") c := jen.Switch(jen.Add(switchValue).Op(":=").Id("object").Assert(jen.Type())).Block( CaseType(jen.Map(jen.String()).Interface(), switchValue, func(value, t jen.Code) jen.Statement { return jen.Statement{jen.Line(), jen.Return(value)} }), ) fmt.Printf("%#v", c)
Output: switch t := object.(type) { case map[string]interface{}, *map[string]interface{}: var value map[string]interface{} if val, ok := t.(*map[string]interface{}); ok { value = *val } else { value = t.(map[string]interface{}) } return value }
func Cases ¶
func Cases(types Statement, src Code, fn func(value, t Code) Statement, baseType ...*Statement) Statement
Cases is a wrapper that renders a set of CaseType for each given type.
Example ¶
switchValue := jen.Id("t") c := jen.Switch(jen.Add(switchValue).Op(":=").Id("object").Assert(jen.Type())).Block( Cases(jen.Statement{jen.Bool(), jen.Int(), jen.String()}, switchValue, func(value, t jen.Code) jen.Statement { return jen.Statement{jen.Line(), jen.Return(value)} }, jen.Index())..., ) fmt.Printf("%#v", c)
Output: switch t := object.(type) { case []bool, *[]bool: var value []bool if val, ok := t.(*[]bool); ok { value = *val } else { value = t.([]bool) } return value case []int, *[]int: var value []int if val, ok := t.(*[]int); ok { value = *val } else { value = t.([]int) } return value case []string, *[]string: var value []string if val, ok := t.(*[]string); ok { value = *val } else { value = t.([]string) } return value }
Example (NotBaseType) ¶
switchValue := jen.Id("t") c := jen.Switch(jen.Add(switchValue).Op(":=").Id("object").Assert(jen.Type())).Block( Cases(jen.Statement{jen.Bool(), jen.Int(), jen.String()}, switchValue, func(value, t jen.Code) jen.Statement { return jen.Statement{jen.Line(), jen.Return(value)} })..., ) fmt.Printf("%#v", c)
Output: switch t := object.(type) { case bool, *bool: var value bool if val, ok := t.(*bool); ok { value = *val } else { value = t.(bool) } return value case int, *int: var value int if val, ok := t.(*int); ok { value = *val } else { value = t.(int) } return value case string, *string: var value string if val, ok := t.(*string); ok { value = *val } else { value = t.(string) } return value }
func DeepCopy ¶
func DeepCopy(t, src, dst, recursiveFunc Code) Statement
Example ¶
c := DeepCopy(jen.Map(jen.String()).Interface(), jen.Id("origin"), jen.Id("clone"), jen.Id("deepCopy")) fmt.Printf("%s", c.GoString())
Output: clone := make(map[string]interface{}, len(origin)) for k, v := range origin { clone[k] = deepCopy(v).(interface{}) }
func Foreach ¶
func Foreach(src Code, keyAndValue ...string) *Statement
Example ¶
c := Foreach(jen.Id("object")).Block( jen.Id("newObject").Index(jen.Id("k")).Op("=").Id("v"), ) fmt.Printf("%#v", c)
Output: for k, v := range object { newObject[k] = v }
Example (CustomKeyValue) ¶
c := Foreach(jen.Id("object"), "key", "value").Block( jen.Id("newObject").Index(jen.Id("key")).Op("=").Id("value"), ) fmt.Printf("%#v", c)
Output: for key, value := range object { newObject[key] = value }
Example (NotKey) ¶
c := Foreach(jen.Id("object"), "_").Block( jen.Id("newObject").Op("=").Append(jen.Id("newObject"), jen.Id("v")), ) fmt.Printf("%#v", c)
Output: for _, v := range object { newObject = append(newObject, v) }
func InterfaceCopy ¶ added in v1.0.1
func InterfaceCopy(t, src, dst, recursiveFunc Code) Statement
Example ¶
c := InterfaceCopy(jen.Map(jen.String()).Interface(), jen.Id("origin"), jen.Id("clone"), jen.Id("deepCopy")) fmt.Printf("%s", c.GoString())
Output: clone := make(map[string]interface{}, len(origin)) for k, v := range origin { clone[k] = deepCopy(v) }
func ShallowCopy ¶
func ShallowCopy(t, src, dst Code) Statement
Example ¶
c := ShallowCopy(jen.String(), jen.Id("origin"), jen.Id("clone")) fmt.Printf("%s", c.GoString())
Output: clone := make(string, len(origin)) for k, v := range origin { clone[k] = v }
func SwitchType ¶
func SwitchType(src, dst Code, fn func(value Code) Statement) *Statement
Example ¶
c := SwitchType(jen.Id("object"), jen.Id("t"), func(value jen.Code) jen.Statement { return jen.Statement{jen.Case(jen.String()).Block(jen.Return(value))} }) fmt.Printf("%#v", c)
Output: switch t := object.(type) { case string: return t }
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.