func ExampleMap_Struct() {
	type str struct {
		IntVal   int     `json:"int"`
		StrVal   string  `json:"str"`
		FloatVal float64 `json:"float"`
		BoolVal  bool    `json:"bool"`
		private  string
	}

	mapVal := mxj.Map{"int": 4, "str": "now's the time", "float": 3.14159, "bool": true, "private": "Somewhere over the rainbow"}

	var strVal str
	mverr := mapVal.Struct(&strVal)
	if mverr != nil {
		// handle error
	}

	fmt.Printf("mapVal: %#v\n", mapVal)
	fmt.Printf("strVal: %#v\n", strVal)
	// Note: example output is conformed to pass "go test".  "mxj_test" is example_test.go package name.

	// Output:
	// mapVal: mxj.Map{"int":4, "str":"now's the time", "float":3.14159, "bool":true, "private":"Somewhere over the rainbow"}
	// strVal: mxj_test.str{IntVal:4, StrVal:"now's the time", FloatVal:3.14159, BoolVal:true, private:""}
}
Exemple #2
0
func main() {
	// fmt.Println(string(data))
	rdr := bytes.NewReader(data)
	var m mxj.Map
	var err error
	// We read processing docs sequentially.
	// Un-rooted ProcInst or Comments are processed AND just reencoded. (XmlSeqIndent() knows how, now.)
	for m, err = mxj.NewMapXmlSeqReader(rdr); m != nil || err != io.EOF; m, err = mxj.NewMapXmlSeqReader(rdr) {
		if err != nil {
			if err != mxj.NO_ROOT {
				fmt.Println("NewMapXmlSeq err:", err)
				fmt.Println("m:", m)
			} else if m != nil {
				x, _ := m.XmlSeqIndent("", "  ")
				fmt.Println(string(x))
			}
			continue
		}
		// fmt.Println(m.StringIndent())

		// We have Two different arrays of Items in the XML doc, one nested in the other.
		if err = copyCmts(m, "WebTest.Items"); err != nil {
			fmt.Println("err:", err)
		}
		if err = copyCmts(m, "WebTest.Items.TransactionTimer.Items"); err != nil {
			fmt.Println("err:", err)
		}
		// re-encode the map with the Items.Comment[#seq==n].#attr.CommentText
		// values copied to the Items.Request[#seq==n+1].#attr.ReportingName elements.
		b, err := m.XmlSeqIndent("", "  ")
		if err != nil {
			fmt.Println("XmlIndent err:", err)
			return
		}
		fmt.Println(string(b))
	}
}
Exemple #3
0
func parseResponse(m *mxj.Map, response *[]mxj.Map, responsename, idmapname string) error {
	path := fmt.Sprintf("Envelope.Body.%sResponse.%sReturn.-href", responsename, responsename)
	mainid, err := m.ValueForPathString(path)
	if err != nil {
		return err
	}
	value, err := m.ValuesForKey("multiRef", fmt.Sprintf("-id:%s", mainid[1:]))
	if err != nil {
		return err
	}
	if len(value) == 0 {
		return errors.New("Main reponse not found")
	}

	valuemap := mxj.Map(value[0].(map[string]interface{}))

	statuscode, err := valuemap.ValueForPathString("statusCode.#text")
	if err != nil {
		return err
	}

	if statuscode != "SUCCESS" {
		errcode, _ := valuemap.ValueForPathString("errorCode.#text")
		return YFErrors[errcode]
	}

	if idmapname != "" {
		ids, err := valuemap.ValuesForPath(idmapname + "." + idmapname)
		if err != nil {
			return fmt.Errorf("Could not find %s.%s %v", idmapname, idmapname, err)
		}
		for _, id := range ids {
			idmap := mxj.Map(id.(map[string]interface{}))
			idvalue, err := idmap.ValueForPathString("-href")
			if err != nil {
				return err
			}

			multiref, err := m.ValuesForKey("multiRef", "-id:"+idvalue[1:])
			if err != nil {
				return fmt.Errorf("Could not find multiref for id %s, %v", idvalue[1:], err)
			}
			if len(multiref) == 0 {
				return fmt.Errorf("mutilRef element not found for id : %s", idvalue[1:])
			}
			*response = append(*response, mxj.Map(multiref[0].(map[string]interface{})))
		}
	} else {
		// Add the original back, this is to get responses such as sessionid etc.
		*response = append(*response, valuemap)
	}
	return nil

}
Exemple #4
0
// Uncomment the print statements to details of the process here.
func copyCmts(m mxj.Map, path string) error {
	// get the array of Items entries for the 'path'
	vals, err := m.ValuesForPath(path)
	if err != nil {
		return fmt.Errorf("ValuesForPath err: %s", err.Error())
	} else if len(vals) == 0 {
		return fmt.Errorf("no vals for path: %s", path)
	}
	// process each Items entry
	for _, v := range vals {
		vm, ok := v.(map[string]interface{})
		if !ok {
			return fmt.Errorf("assertion failed")
		}
		// get the Comment list
		c, ok := vm["Comment"]
		if !ok { // --> no Items.Comment elements
			continue
		}
		// Don't assume that Comment is an array.
		// There may be just one value, in which case it will decode as map[string]interface{}.
		switch c.(type) {
		case map[string]interface{}:
			c = []interface{}{c}
		}
		cmt := c.([]interface{})
		// get the Request list
		r, ok := vm["Request"]
		if !ok { // --> no Items.Request elements
			continue
		}
		// Don't assume the Request is an array.
		// There may be just one value, in which case it will decode as map[string]interface{}.
		switch r.(type) {
		case map[string]interface{}:
			r = []interface{}{r}
		}
		req := r.([]interface{})

		// fmt.Println("Comment:", cmt)
		// fmt.Println("Request:", req)

		// Comment elements with #seq==n are followed by Request element with #seq==n+1.
		// For each Comment.#seq==n extract the CommentText attribute value and use it to
		// set the ReportingName attribute value in Request.#seq==n+1.
		for _, v := range cmt {
			vmap := v.(map[string]interface{})
			seq := vmap["#seq"].(int) // type is int
			// extract CommentText attr from "#attr"
			acmt, _ := mxj.Map(vmap).ValueForPath("#attr.CommentText.#text")
			if acmt == "" {
				fmt.Println("no CommentText value in Comment attributes")
			}
			// fmt.Println(seq, acmt)
			// find the request with the #seq==seq+1 value
			var r map[string]interface{}
			for _, vv := range req {
				rt := vv.(map[string]interface{})
				if rt["#seq"].(int) == seq+1 {
					r = rt
					break
				}
			}
			if r == nil { // no Request with #seq==seq+1
				continue
			}
			if err := mxj.Map(r).SetValueForPath(acmt, "#attr.ReportingName.#text"); err != nil {
				fmt.Println("SetValueForPath err:", err)
				break
			}
		}
	}
	return nil
}