本文整理汇总了Golang中kego/io/system.Packed.Type方法的典型用法代码示例。如果您正苦于以下问题:Golang Packed.Type方法的具体用法?Golang Packed.Type怎么用?Golang Packed.Type使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类kego/io/system.Packed
的用法示例。
在下文中一共展示了Packed.Type方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Unpack
func (v *Rectangle) Unpack(ctx context.Context, in system.Packed, iface bool) error {
if in == nil || in.Type() == system.J_NULL {
return nil
}
if v.Object == nil {
v.Object = new(system.Object)
}
if err := v.Object.Unpack(ctx, in, false); err != nil {
return err
}
if err := v.Object.InitializeType("kego.io/demo/common/units", "rectangle"); err != nil {
return err
}
if field, ok := in.Map()["height"]; ok && field.Type() != system.J_NULL {
ob0 := new(system.Int)
if err := ob0.Unpack(ctx, field, false); err != nil {
return err
}
v.Height = ob0
}
if field, ok := in.Map()["width"]; ok && field.Type() != system.J_NULL {
ob0 := new(system.Int)
if err := ob0.Unpack(ctx, field, false); err != nil {
return err
}
v.Width = ob0
}
return nil
}
示例2: Unpack
func (v *CRule) Unpack(ctx context.Context, in system.Packed, iface bool) error {
if in == nil || in.Type() == system.J_NULL {
return nil
}
if v.Object == nil {
v.Object = new(system.Object)
}
if err := v.Object.Unpack(ctx, in, false); err != nil {
return err
}
if err := v.Object.InitializeType("kego.io/process/validate/tests", "@c"); err != nil {
return err
}
if v.Rule == nil {
v.Rule = new(system.Rule)
}
if err := v.Rule.Unpack(ctx, in, false); err != nil {
return err
}
if field, ok := in.Map()["fail"]; ok && field.Type() != system.J_NULL {
ob0 := new(system.Bool)
if err := ob0.Unpack(ctx, field, false); err != nil {
return err
}
v.Fail = ob0
}
return nil
}
示例3: Unpack
func (v *Person) Unpack(ctx context.Context, in system.Packed, iface bool) error {
if in == nil || in.Type() == system.J_NULL {
return nil
}
if v.Object == nil {
v.Object = new(system.Object)
}
if err := v.Object.Unpack(ctx, in, false); err != nil {
return err
}
if err := v.Object.InitializeType("kego.io/demo/demo6", "person"); err != nil {
return err
}
if field, ok := in.Map()["age"]; ok && field.Type() != system.J_NULL {
ob0 := new(system.Int)
if err := ob0.Unpack(ctx, field, false); err != nil {
return err
}
v.Age = ob0
}
if field, ok := in.Map()["name"]; ok && field.Type() != system.J_NULL {
ob0 := new(system.String)
if err := ob0.Unpack(ctx, field, false); err != nil {
return err
}
v.Name = ob0
}
return nil
}
示例4: UnpackLocalized
func UnpackLocalized(ctx context.Context, in system.Packed) (Localized, error) {
switch in.Type() {
case system.J_MAP:
i, err := system.UnpackUnknownType(ctx, in, true, "kego.io/demo/demo5/translation", "localized")
if err != nil {
return nil, err
}
ob, ok := i.(Localized)
if !ok {
return nil, fmt.Errorf("%T does not implement Localized", i)
}
return ob, nil
default:
return nil, fmt.Errorf("Unsupported json type %s when unpacking into Localized.", in.Type())
}
}
示例5: UnpackSection
func UnpackSection(ctx context.Context, in system.Packed) (Section, error) {
switch in.Type() {
case system.J_MAP:
i, err := system.UnpackUnknownType(ctx, in, true, "kego.io/demo/site", "section")
if err != nil {
return nil, err
}
ob, ok := i.(Section)
if !ok {
return nil, fmt.Errorf("%T does not implement Section", i)
}
return ob, nil
default:
return nil, fmt.Errorf("Unsupported json type %s when unpacking into Section.", in.Type())
}
}
示例6: UnpackPageInterface
func UnpackPageInterface(ctx context.Context, in system.Packed) (PageInterface, error) {
switch in.Type() {
case system.J_MAP:
i, err := system.UnpackUnknownType(ctx, in, true, "kego.io/demo/demo2", "page")
if err != nil {
return nil, err
}
ob, ok := i.(PageInterface)
if !ok {
return nil, fmt.Errorf("%T does not implement PageInterface", i)
}
return ob, nil
default:
return nil, fmt.Errorf("Unsupported json type %s when unpacking into PageInterface.", in.Type())
}
}
示例7: UnpackFInterface
func UnpackFInterface(ctx context.Context, in system.Packed) (FInterface, error) {
switch in.Type() {
case system.J_MAP:
i, err := system.UnpackUnknownType(ctx, in, true, "kego.io/process/validate/tests", "f")
if err != nil {
return nil, err
}
ob, ok := i.(FInterface)
if !ok {
return nil, fmt.Errorf("%T does not implement FInterface", i)
}
return ob, nil
default:
return nil, fmt.Errorf("Unsupported json type %s when unpacking into FInterface.", in.Type())
}
}
示例8: Unpack
func (v *MainRule) Unpack(ctx context.Context, in system.Packed, iface bool) error {
if in == nil || in.Type() == system.J_NULL {
return nil
}
if v.Object == nil {
v.Object = new(system.Object)
}
if err := v.Object.Unpack(ctx, in, false); err != nil {
return err
}
if v.Rule == nil {
v.Rule = new(system.Rule)
}
if err := v.Rule.Unpack(ctx, in, false); err != nil {
return err
}
return nil
}
示例9: Unpack
func (v *Photo) Unpack(ctx context.Context, in system.Packed, iface bool) error {
if in == nil || in.Type() == system.J_NULL {
return nil
}
if v.Object == nil {
v.Object = new(system.Object)
}
if err := v.Object.Unpack(ctx, in, false); err != nil {
return err
}
if err := v.Object.InitializeType("kego.io/demo/demo3/images", "photo"); err != nil {
return err
}
if field, ok := in.Map()["url"]; ok && field.Type() != system.J_NULL {
ob0 := new(system.String)
if err := ob0.Unpack(ctx, field, false); err != nil {
return err
}
v.Url = ob0
}
return nil
}
示例10: Unpack
func (v *Simple) Unpack(ctx context.Context, in system.Packed, iface bool) error {
if in == nil || in.Type() == system.J_NULL {
return nil
}
if v.Object == nil {
v.Object = new(system.Object)
}
if err := v.Object.Unpack(ctx, in, false); err != nil {
return err
}
if err := v.Object.InitializeType("kego.io/demo/demo5/translation", "simple"); err != nil {
return err
}
if field, ok := in.Map()["text"]; ok && field.Type() != system.J_NULL {
ob0 := new(system.String)
if err := ob0.Unpack(ctx, field, false); err != nil {
return err
}
v.Text = ob0
}
return nil
}
示例11: initialiseFields
func (n *Node) initialiseFields(ctx context.Context, in system.Packed, updateVal bool) error {
valueFields := map[string]system.Packed{}
if in != nil && in.Type() != system.J_NULL {
valueFields = in.Map()
}
typeFields := map[string]*system.Field{}
if err := extractFields(ctx, typeFields, n.Type); err != nil {
return kerr.Wrap("LPWTOSATQE", err)
}
for name, typeField := range typeFields {
rule := system.WrapRule(ctx, typeField.Rule)
valueField, valueExists := valueFields[name]
childNode := NewNode()
if err := childNode.initialiseObjectField(ctx, n, rule, name, typeField.Origin); err != nil {
return kerr.Wrap("ILIHBXGROP", err)
}
if err := childNode.AddToObject(ctx, n, rule, name, updateVal); err != nil {
return kerr.Wrap("LJUGPMWNPD", err)
}
if valueExists {
if err := childNode.setValue(ctx, valueField, false); err != nil {
return kerr.Wrap("UWOTRJJVNK", err)
}
}
}
for name, _ := range valueFields {
_, ok := typeFields[name]
if !ok {
return kerr.New("SRANLETJRS", "Extra field %s, %s", name, n.Path())
}
}
return nil
}
示例12: setValue
func (n *Node) setValue(ctx context.Context, in system.Packed, unpack bool) error {
n.Missing = false
objectType, err := extractType(ctx, in, n.Rule)
if err != nil {
return kerr.Wrap("MKMNOOYQJY", err)
}
if err := n.setType(ctx, objectType); err != nil {
return kerr.Wrap("BCMOTEMUJE", err)
}
if n.Rule == nil && objectType != nil {
n.Rule = system.WrapEmptyRule(ctx, objectType)
}
if in.Type() == system.J_MAP && n.Type.IsNativeObject() {
// for objects and maps, Type() from the system.Packed is always J_MAP,
// so we correct it for object types here.
n.JsonType = system.J_OBJECT
} else {
n.JsonType = in.Type()
}
n.Null = in.Type() == system.J_NULL
// validate json type
// if !n.Null && n.Type.NativeJsonType() != n.JsonType {
// return kerr.New("VEPLUIJXSN", "json type is %s but object type is %s", n.JsonType, n.Type.NativeJsonType())
// }
if unpack {
if n.Rule.Struct == nil {
if err := system.Unpack(ctx, in, &n.Value); err != nil {
return kerr.Wrap("CQMWGPLYIJ", err)
}
} else {
t, err := n.Rule.GetReflectType()
if err != nil {
return kerr.Wrap("DQJDYPIANO", err)
}
var val reflect.Value
if t.Kind() == reflect.Ptr {
val = reflect.New(t.Elem())
} else {
val = reflect.New(t).Elem()
}
if err := system.UnpackRefelctValue(ctx, in, val); err != nil {
return kerr.Wrap("PEVKGFFHLL", err)
}
n.Value = val.Interface()
}
n.setVal(reflect.ValueOf(n.Value))
}
switch n.Type.NativeJsonType(ctx) {
case system.J_STRING:
if in.Type() == system.J_MAP {
n.ValueString = in.Map()["value"].String()
} else {
n.ValueString = in.String()
}
case system.J_NUMBER:
if in.Type() == system.J_MAP {
n.ValueNumber = in.Map()["value"].Number()
} else {
n.ValueNumber = in.Number()
}
case system.J_BOOL:
if in.Type() == system.J_MAP {
n.ValueBool = in.Map()["value"].Bool()
} else {
n.ValueBool = in.Bool()
}
case system.J_ARRAY:
children := in.Array()
for i, child := range children {
childNode := NewNode()
if err := childNode.InitialiseArrayItem(ctx, n, i); err != nil {
return kerr.Wrap("XHQKQTNRJV", err)
}
if err := childNode.AddToArray(ctx, n, i, false); err != nil {
return kerr.Wrap("VWWYPDIJKP", err)
}
if err := childNode.setValue(ctx, child, false); err != nil {
return kerr.Wrap("KUCBPFFJNT", err)
}
}
case system.J_MAP:
n.Map = map[string]*Node{}
children := in.Map()
for name, child := range children {
childNode := NewNode()
if err := childNode.InitialiseMapItem(ctx, n, name); err != nil {
return kerr.Wrap("TBNWBMJDIE", err)
}
if err := childNode.AddToMap(ctx, n, name, false); err != nil {
return kerr.Wrap("HTOPDOKPRE", err)
//.........这里部分代码省略.........
示例13: extractType
func extractType(ctx context.Context, in system.Packed, rule *system.RuleWrapper) (*system.Type, error) {
parentInterface := rule != nil && rule.Parent != nil && rule.Parent.Interface
ruleInterface := rule != nil && rule.Struct != nil && rule.Struct.Interface
if parentInterface && ruleInterface {
return nil, kerr.New("TDXTPGVFAK", "Can't have interface type and interface rule at the same time")
}
if rule != nil && rule.Parent != nil && !parentInterface && !ruleInterface {
// If we have a rule with the parent, and it's not an interface, then
// we just return the parent type of the rule.
return rule.Parent, nil
}
// if the rule is nil (e.g. unpacking into an unknown type) or the type is
// an interface, we ensure the input is a map
if rule == nil || parentInterface {
if in == nil {
return nil, nil
}
switch in.Type() {
case system.J_NULL:
// item is nil, so we don't know the concrete type yet.
return nil, nil
case system.J_MAP:
break
default:
return nil, kerr.New("DLSQRFLINL", "Input %s should be J_MAP if rule is nil or an interface type", in.Type())
}
}
// if the rule is an interface rule, we ensure the input is a map or a native value
if ruleInterface {
if in == nil {
return nil, nil
}
switch in.Type() {
case system.J_NULL:
// item is nil, so we don't know the concrete type yet.
return nil, nil
case system.J_MAP:
break
case system.J_STRING, system.J_NUMBER, system.J_BOOL:
// if the input value is a native value, we will be unpacking into
// the parent type of the rule
return rule.Parent, nil
default:
return nil, kerr.New("SNYLGBJYTM", "Input %s should be J_MAP, J_STRING, J_NUMBER or J_BOOL if rule is interface rule", in.Type())
}
}
ob := in.Map()
typeField, ok := ob["type"]
if !ok {
return nil, kerr.New("HBJVDKAKBJ", "Input must have type field if rule is nil or an interface type")
}
var r system.Reference
if err := r.Unpack(ctx, typeField, false); err != nil {
return nil, kerr.Wrap("YXHGIBXCOC", err)
}
t, ok := r.GetType(ctx)
if !ok {
return nil, kerr.New("IJFMJJWVCA", "Could not find type %s", r.Value())
}
return t, nil
}