github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/spec/main.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "fmt" 6 7 "github.com/egonelbre/exp/spec/soap" 8 ) 9 10 var data = ` 11 <Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'> 12 <Body> 13 <GetAllColumnsResponse xmlns='http://replicon.com/'> 14 <GetAllColumnsResult xmlns:i='http://www.w3.org/2001/XMLSchema-instance'> 15 <ListColumnGroup1> 16 <columns> 17 <ListColumn1> 18 <dataType>urn:replicon:list-type:string</dataType> 19 <displayText>Project Name</displayText> 20 <uri>urn:replicon:project-list-column:name</uri> 21 </ListColumn1> 22 <ListColumn1> 23 <dataType>urn:replicon:list-type:string</dataType> 24 <displayText>Code</displayText> 25 <uri>urn:replicon:project-list-column:code</uri> 26 </ListColumn1> 27 <ListColumn1> 28 <dataType>urn:replicon:list-type:object</dataType> 29 <displayText>Status</displayText> 30 <uri>urn:replicon:project-list-column:status</uri> 31 </ListColumn1> 32 <ListColumn1> 33 <dataType>urn:replicon:list-type:object</dataType> 34 <displayText>Client</displayText> 35 <uri>urn:replicon:project-list-column:client</uri> 36 </ListColumn1> 37 <ListColumn1> 38 <dataType>urn:replicon:list-type:object</dataType> 39 <displayText>Program</displayText> 40 <uri>urn:replicon:project-list-column:program</uri> 41 </ListColumn1> 42 </columns> 43 </ListColumnGroup1> 44 </GetAllColumnsResult> 45 </GetAllColumnsResponse> 46 </Body> 47 </Envelope> 48 ` 49 50 type AllColumnsResponse struct { 51 Columns []Column `json:"columns"` 52 } 53 54 type Column struct { 55 DisplayText string `json:"displayText"` 56 DataType string `json:"dataType"` 57 URI string `json:"uri"` 58 } 59 60 func (response *AllColumnsResponse) Spec() soap.Spec { 61 return soap.Tag("Envelope", 62 soap.Tag("Body", 63 soap.Tag("GetAllColumnsResponse", 64 soap.Tag("GetAllColumnsResult", 65 soap.Tag("ListColumnGroup1", 66 soap.TagList("columns", &response.Columns), 67 ), 68 ), 69 ), 70 ), 71 ) 72 } 73 74 func (column *Column) Spec() soap.Spec { 75 return soap.Tag("ListColumn1", 76 soap.String("dataType", &column.DataType), 77 soap.String("displayText", &column.DisplayText), 78 soap.String("uri", &column.URI), 79 ) 80 } 81 82 func main() { 83 node, err := soap.Parse([]byte(data)) 84 if err != nil { 85 panic(err) 86 } 87 88 var response AllColumnsResponse 89 response.Spec().Decode(node) 90 91 data, err := json.MarshalIndent(&response, "", "\t") 92 if err != nil { 93 panic(err) 94 } 95 fmt.Println("-----------------") 96 fmt.Println(string(data)) 97 98 data, err = response.Spec().Encode().Encode() 99 if err != nil { 100 panic(err) 101 } 102 fmt.Println("-----------------") 103 fmt.Println(string(data)) 104 }