github.com/urso/go-structform@v0.0.2/gotype/unfold_refl.yml (about) 1 import: 2 - unfold_templates.yml 3 4 main: | 5 package gotype 6 7 func (u *unfolderReflSlice) OnNil(ctx *unfoldCtx) error { 8 u.prepare(ctx) 9 return nil 10 } 11 12 {{ invoke "makeReflPrimitives" "type" "unfolderReflSlice" }} 13 {{ invoke "makeReflChildArrays" "type" "unfolderReflSlice" }} 14 {{ invoke "makeReflChildObjects" "type" "unfolderReflSlice" }} 15 16 func (u *unfolderReflMapOnElem) OnNil(ctx *unfoldCtx) error { 17 ptr := ctx.value.current 18 m := ptr.Elem() 19 v := reflect.Zero(m.Type().Elem()) 20 m.SetMapIndex(reflect.ValueOf(ctx.key.pop()), v) 21 22 ctx.unfolder.current = u.shared.waitKey 23 return nil 24 } 25 26 {{ invoke "makeReflPrimitives" "type" "unfolderReflMapOnElem" "process" "process" }} 27 {{ invoke "makeReflChildArrays" "type" "unfolderReflMapOnElem" "process" "process" }} 28 {{ invoke "makeReflChildObjects" "type" "unfolderReflMapOnElem" "process" "process" "error" "errExpectedObjectValue" }} 29 30 func (u *unfolderReflPtr) OnNil(ctx *unfoldCtx) error { 31 ptr := ctx.value.current 32 v := ptr.Elem() 33 v.Set(reflect.Zero(v.Type())) 34 u.cleanup(ctx) 35 return nil 36 } 37 38 {{ invoke "makeReflPrimitives" "type" "unfolderReflPtr" "process" "process" }} 39 {{ invoke "makeReflChildArrays" "type" "unfolderReflPtr" "process" "process" }} 40 {{ invoke "makeReflChildObjects" "type" "unfolderReflPtr" "process" "process" }} 41 42 # makeReflPrimitiveCallbacks(type, [process]) 43 templates.makeReflPrimitives: | 44 {{ $type := .type}} 45 {{ $process := .process }} 46 47 func (u *{{ $type }}) OnByte(ctx *unfoldCtx, v byte) error { 48 elem := u.prepare(ctx) 49 u.elem.initState(ctx, elem) 50 err := ctx.unfolder.current.OnByte(ctx, v) 51 {{ if $process }} 52 if err == nil { 53 u.{{ $process }}(ctx) 54 } 55 {{ end }} 56 return err 57 } 58 59 func (u *{{ $type }}) OnStringRef(ctx *unfoldCtx, v []byte) error { 60 return u.OnString(ctx, string(v)) 61 } 62 63 {{ range data.primitiveTypes }} 64 func (u *{{ $type }}) On{{ . | capitalize}}(ctx *unfoldCtx, v {{ . }}) error { 65 elem := u.prepare(ctx) 66 u.elem.initState(ctx, elem) 67 err := ctx.unfolder.current.On{{ . | capitalize }}(ctx, v) 68 {{ if $process }} 69 if err == nil { 70 u.{{ $process }}(ctx) 71 } 72 {{ end }} 73 return err 74 } 75 {{ end }} 76 77 # makeReflChildArrays(type, [process]) 78 templates.makeReflChildArrays: | 79 {{ $type := .type}} 80 {{ $process := .process }} 81 82 func (u *{{ $type }}) OnArrayStart(ctx *unfoldCtx, l int, bt structform.BaseType) error { 83 elem := u.prepare(ctx) 84 u.elem.initState(ctx, elem) 85 return ctx.unfolder.current.OnArrayStart(ctx, l, bt) 86 } 87 88 func (u *{{ $type }}) OnChildArrayDone(ctx *unfoldCtx) error { 89 {{ if $process }} 90 u.{{ $process }}(ctx) 91 {{ end }} 92 return nil 93 } 94 95 # makeReflChildObjects(type, [process], [error]) 96 templates.makeReflChildObjects: | 97 {{ $type := .type}} 98 {{ $process := .process }} 99 {{ $error := default "errUnsupported" .error }} 100 101 func (u *{{ $type }}) OnObjectStart(ctx *unfoldCtx, l int, bt structform.BaseType) error { 102 elem := u.prepare(ctx) 103 u.elem.initState(ctx, elem) 104 return ctx.unfolder.current.OnObjectStart(ctx, l, bt) 105 } 106 107 func (u *{{ $type }}) OnKey(_ *unfoldCtx, _ string) error { 108 return {{ $error }} 109 } 110 111 func (u *{{ $type }}) OnKeyRef(_ *unfoldCtx, _ []byte) error { 112 return {{ $error }} 113 } 114 115 func (u *{{ $type }}) OnChildObjectDone(ctx *unfoldCtx) error { 116 {{ if $process }} 117 u.{{ $process }}(ctx) 118 {{ end }} 119 return nil 120 } 121