github.com/urso/go-structform@v0.0.2/gotype/unfold_primitive.yml (about)

     1  import:
     2    - unfold_templates.yml
     3  
     4  main: |
     5    package gotype
     6  
     7    var (
     8    {{/* defined 'lifted' pointer primitive unfolders into reflection based unfolders */}}
     9      unfolderReflIfc = liftGoUnfolder(newUnfolderIfc())
    10      {{ range data.primitiveTypes }}
    11      {{ $t := capitalize . }}
    12        unfolderRefl{{ $t }} = liftGoUnfolder(newUnfolder{{ $t }}())
    13      {{ end }}
    14    )
    15  
    16    {{/* define pointer based unfolder types */}}
    17    {{ invoke "makeTypeWithName" "name" "ifc" "type" "interface{}" }}
    18    {{ template "makeType" "bool" }}
    19    {{ template "makeType" "string" }}
    20    {{ range .numTypes }} 
    21      {{ template "makeType" . }}
    22    {{ end }}
    23  
    24    {{/* create value visitor callbacks */}}
    25    {{ invoke "onIfcFns" "name" "unfolderIfc" "fn" "assign" }}
    26    {{ invoke "onBoolFns" "name" "unfolderBool" "fn" "assign" }}
    27    {{ invoke "onStringFns" "name" "unfolderString" "fn" "assign" }}
    28    {{ range .numTypes }}
    29      {{ $type := . }}
    30      {{ $name := capitalize . | printf "unfolder%v" }}
    31      {{ invoke "onNumberFns" "name" $name "type" $type "fn" "assign" }}
    32    {{ end }}
    33  
    34    /*
    35    func (*unfolderIfc) OnArrayStart(ctx *unfoldCtx, l int, bt structform.BaseType) error {
    36      return unfoldIfcStartSubArray(ctx, l, bt)
    37    }
    38  
    39    func (u *unfolderIfc) OnChildArrayDone(ctx *unfoldCtx) error {
    40      v, err := unfoldIfcFinishSubArray(ctx)
    41      if err == nil {
    42        err = u.assign(ctx, v)
    43      }
    44      return err
    45    }
    46    */
    47  
    48  # makeTypeWithName(name, type, [base])
    49  templates.makeTypeWithName: |
    50    {{ $type := .type }}
    51    {{ $name := capitalize .name | printf "unfolder%v" }}
    52    {{ invoke "makeUnfoldType" "type" $type "name" $name "base" .base }}
    53  
    54    func (u *{{ $name }} ) initState(ctx *unfoldCtx, ptr unsafe.Pointer) {
    55      ctx.unfolder.push(u)
    56      ctx.ptr.push(ptr)
    57    }
    58  
    59    func (u *{{ $name }} ) cleanup(ctx *unfoldCtx) {
    60      ctx.unfolder.pop()
    61      ctx.ptr.pop()
    62    }
    63  
    64    func (u *{{ $name }} ) ptr(ctx *unfoldCtx) *{{ $type }} {
    65      return (*{{ $type }})(ctx.ptr.current)
    66    }
    67  
    68    func (u *{{ $name }}) assign(ctx *unfoldCtx, v {{ $type }}) error {
    69      *u.ptr(ctx) = v
    70      u.cleanup(ctx)
    71      return nil
    72    }