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 }
func (v *ImageRule) 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/images", "@image"); 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()["secure"]; ok && field.Type() != system.J_NULL { ob0 := new(system.Bool) if err := ob0.Unpack(ctx, field, false); err != nil { return err } v.Secure = ob0 } return nil }
func (v *Columns) 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/site", "columns"); err != nil { return err } if field, ok := in.Map()["columns"]; ok && field.Type() != system.J_NULL { if field.Type() != system.J_ARRAY { return fmt.Errorf("Unsupported json type %s found while unpacking into an array.", field.Type()) } ob0 := []Section{} for i0 := range field.Array() { ob1, err := UnpackSection(ctx, field.Array()[i0]) if err != nil { return err } ob0 = append(ob0, ob1) } v.Columns = ob0 } return nil }
func (v *F) 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", "f"); err != nil { return err } if field, ok := in.Map()["a"]; ok && field.Type() != system.J_NULL { ob0 := new(A) if err := ob0.Unpack(ctx, field, false); err != nil { return err } v.A = ob0 } if field, ok := in.Map()["b"]; ok && field.Type() != system.J_NULL { if field.Type() != system.J_ARRAY { return fmt.Errorf("Unsupported json type %s found while unpacking into an array.", field.Type()) } ob0 := []*A{} for i0 := range field.Array() { ob1 := new(A) if err := ob1.Unpack(ctx, field.Array()[i0], false); err != nil { return err } ob0 = append(ob0, ob1) } v.B = ob0 } if field, ok := in.Map()["c"]; ok && field.Type() != system.J_NULL { if field.Type() != system.J_MAP { return fmt.Errorf("Unsupported json type %s found while unpacking into a map.", field.Type()) } ob0 := map[string]*A{} for k0 := range field.Map() { ob1 := new(A) if err := ob1.Unpack(ctx, field.Map()[k0], false); err != nil { return err } ob0[k0] = ob1 } v.C = ob0 } if field, ok := in.Map()["d"]; ok && field.Type() != system.J_NULL { ob0 := new(system.String) if err := ob0.Unpack(ctx, field, false); err != nil { return err } v.D = ob0 } return nil }
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/common/images", "photo"); err != nil { return err } if field, ok := in.Map()["path"]; ok && field.Type() != system.J_NULL { ob0 := new(system.String) if err := ob0.Unpack(ctx, field, false); err != nil { return err } v.Path = ob0 } if field, ok := in.Map()["protocol"]; ok && field.Type() != system.J_NULL { ob0 := new(system.String) if err := ob0.Unpack(ctx, field, false); err != nil { return err } v.Protocol = ob0 } else { ob0 := new(system.String) if err := ob0.Unpack(ctx, system.Pack("http"), false); err != nil { return err } v.Protocol = ob0 } if field, ok := in.Map()["server"]; ok && field.Type() != system.J_NULL { ob0 := new(system.String) if err := ob0.Unpack(ctx, field, false); err != nil { return err } v.Server = ob0 } if field, ok := in.Map()["size"]; ok && field.Type() != system.J_NULL { ob0 := new(units.Rectangle) if err := ob0.Unpack(ctx, field, false); err != nil { return err } v.Size = ob0 } return nil }
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()) } }
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()) } }
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()) } }
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()) } }
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 }
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 field, ok := in.Map()["js"]; ok && field.Type() != system.J_NULL { ob0, err := system.UnpackString(ctx, field) if err != nil { return err } v.Js = ob0 } return nil }
func (v *Main) 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 field, ok := in.Map()["a"]; ok && field.Type() != system.J_NULL { ob0 := *new(Alms) if err := ob0.Unpack(ctx, field, false); err != nil { return err } v.A = ob0 } return nil }
func (v *BRule) 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", "@b"); 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 }
func (v *PageRule) 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/demo2", "@page"); 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 }
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 }
func (v *Page) 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/demo2", "page"); err != nil { return err } if field, ok := in.Map()["hero"]; ok && field.Type() != system.J_NULL { ob0 := new(images.Photo) if err := ob0.Unpack(ctx, field, false); err != nil { return err } v.Hero = ob0 } if field, ok := in.Map()["title"]; ok && field.Type() != system.J_NULL { ob0 := new(system.String) if err := ob0.Unpack(ctx, field, false); err != nil { return err } v.Title = ob0 } return nil }
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 }
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 }
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 }
func (v *D) 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", "d"); err != nil { return err } if field, ok := in.Map()["a"]; ok && field.Type() != system.J_NULL { ob0, err := UnpackC(ctx, field) if err != nil { return err } v.A = ob0 } return nil }
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 }
func (v *Body) 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/site", "body"); err != nil { return err } if field, ok := in.Map()["align"]; ok && field.Type() != system.J_NULL { ob0 := new(system.String) if err := ob0.Unpack(ctx, field, false); err != nil { return err } v.Align = ob0 } if field, ok := in.Map()["copy"]; ok && field.Type() != system.J_NULL { ob0, err := system.UnpackStringInterface(ctx, field) if err != nil { return err } v.Copy = ob0 } if field, ok := in.Map()["title"]; ok && field.Type() != system.J_NULL { ob0, err := system.UnpackStringInterface(ctx, field) if err != nil { return err } v.Title = ob0 } return nil }
func (v *Hero) 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/site", "hero"); err != nil { return err } if field, ok := in.Map()["head"]; ok && field.Type() != system.J_NULL { ob0, err := system.UnpackStringInterface(ctx, field) if err != nil { return err } v.Head = ob0 } if field, ok := in.Map()["image"]; ok && field.Type() != system.J_NULL { ob0, err := images.UnpackImage(ctx, field) if err != nil { return err } v.Image = ob0 } if field, ok := in.Map()["subhead"]; ok && field.Type() != system.J_NULL { ob0, err := system.UnpackStringInterface(ctx, field) if err != nil { return err } v.Subhead = ob0 } return nil }
func (v *Page) 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", "page"); err != nil { return err } if field, ok := in.Map()["heading"]; ok && field.Type() != system.J_NULL { ob0 := new(system.String) if err := ob0.Unpack(ctx, field, false); err != nil { return err } v.Heading = ob0 } if field, ok := in.Map()["people"]; ok && field.Type() != system.J_NULL { if field.Type() != system.J_ARRAY { return fmt.Errorf("Unsupported json type %s found while unpacking into an array.", field.Type()) } ob0 := []*Person{} for i0 := range field.Array() { ob1 := new(Person) if err := ob1.Unpack(ctx, field.Array()[i0], false); err != nil { return err } ob0 = append(ob0, ob1) } v.People = ob0 } return nil }
func (v *Smartling) 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", "smartling"); err != nil { return err } if field, ok := in.Map()["english"]; ok && field.Type() != system.J_NULL { ob0 := new(system.String) if err := ob0.Unpack(ctx, field, false); err != nil { return err } v.English = ob0 } if field, ok := in.Map()["translations"]; ok && field.Type() != system.J_NULL { if field.Type() != system.J_MAP { return fmt.Errorf("Unsupported json type %s found while unpacking into a map.", field.Type()) } ob0 := map[string]*system.String{} for k0 := range field.Map() { ob1 := new(system.String) if err := ob1.Unpack(ctx, field.Map()[k0], false); err != nil { return err } ob0[k0] = ob1 } v.Translations = ob0 } return nil }
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) } if err := childNode.setValue(ctx, child, false); err != nil { return kerr.Wrap("LWCSAHSBDF", err) } } case system.J_OBJECT: if err := n.initialiseFields(ctx, in, false); err != nil { return kerr.Wrap("XCRYJWKPKP", err) } } return nil }
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 }
func (v *Alms) Unpack(ctx context.Context, in system.Packed, iface bool) error { if in == nil || in.Type() == system.J_NULL { return nil } if iface { if in.Type() != system.J_MAP { return fmt.Errorf("Invalid type %s while unpacking a map.", in.Type()) } in = in.Map()["value"] } if in.Type() != system.J_MAP { return fmt.Errorf("Invalid type %s while unpacking an array.", in.Type()) } if in.Type() != system.J_MAP { return fmt.Errorf("Unsupported json type %s found while unpacking into a map.", in.Type()) } ob0 := map[string]*Simple{} for k0 := range in.Map() { ob1 := new(Simple) if err := ob1.Unpack(ctx, in.Map()[k0], false); err != nil { return err } ob0[k0] = ob1 } *v = ob0 return nil }