github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/spec/soap/basic.go (about) 1 package soap 2 3 type TagSpec struct { 4 Name string 5 Children []Speced 6 } 7 8 func Tag(name string, children ...Speced) Spec { 9 return &TagSpec{ 10 Name: name, 11 Children: children, 12 } 13 } 14 15 func (spec *TagSpec) Spec() Spec { return spec } 16 17 func (spec *TagSpec) Encode() *Node { 18 node := &Node{} 19 node.XMLName.Local = spec.Name 20 for _, child := range spec.Children { 21 node.Nodes = append(node.Nodes, *child.Spec().Encode()) 22 } 23 return node 24 } 25 26 func (spec *TagSpec) Decode(node *Node) { 27 if spec.Name != node.XMLName.Local { 28 panic("invalid name expected " + spec.Name + " got " + node.XMLName.Local) 29 } 30 if len(spec.Children) != len(node.Nodes) { 31 panic("invalid number of children") 32 } 33 for i, child := range spec.Children { 34 child.Spec().Decode(&node.Nodes[i]) 35 } 36 } 37 38 type StringSpec struct { 39 Name string 40 Value *string 41 } 42 43 func String(name string, value *string) Spec { 44 return &StringSpec{ 45 Name: name, 46 Value: value, 47 } 48 } 49 50 func (spec *StringSpec) Spec() Spec { return spec } 51 52 func (spec *StringSpec) Encode() *Node { 53 node := &Node{} 54 node.XMLName.Local = spec.Name 55 node.Content = []byte(*spec.Value) 56 return node 57 } 58 59 func (spec *StringSpec) Decode(node *Node) { 60 if spec.Name != node.XMLName.Local { 61 panic("invalid name expected " + spec.Name + " got " + node.XMLName.Local) 62 } 63 if len(node.Nodes) != 0 { 64 panic("expected no children for child spec") 65 } 66 *spec.Value = string(node.Content) 67 }