github.com/urso/go-structform@v0.0.2/sftest/util.go (about) 1 package sftest 2 3 import structform "github.com/urso/go-structform" 4 5 func Arr(l int, t structform.BaseType, elems ...interface{}) []Record { 6 a := []Record{ArrayStartRec{l, t}} 7 for _, elem := range elems { 8 switch v := elem.(type) { 9 case Record: 10 a = append(a, v) 11 case []Record: 12 a = append(a, v...) 13 case Recording: 14 a = append(a, v...) 15 default: 16 panic("invalid key type") 17 } 18 } 19 20 return append(a, ArrayFinishRec{}) 21 } 22 23 func Obj(l int, t structform.BaseType, kv ...interface{}) []Record { 24 if len(kv)%2 != 0 { 25 panic("invalid object") 26 } 27 28 a := []Record{ObjectStartRec{l, t}} 29 for i := 0; i < len(kv); i += 2 { 30 k := kv[i].(string) 31 a = append(a, ObjectKeyRec{k}) 32 33 switch v := kv[i+1].(type) { 34 case Record: 35 a = append(a, v) 36 case []Record: 37 a = append(a, v...) 38 case Recording: 39 a = append(a, v...) 40 default: 41 panic("invalid key type") 42 } 43 } 44 45 return append(a, ObjectFinishRec{}) 46 }