Example #1
0
// UnmarshalXML implements the xml.Unmarshaler interface.
func (r *repos) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	var payload struct {
		Data []struct {
			ID        string `xml:"id"`
			Name      string `xml:"name"`
			Type      string `xml:"repoType"`
			Policy    string `xml:"repoPolicy"`
			Format    string `xml:"format"`
			RemoteURI string `xml:"remoteUri"`
		} `xml:"data>repositories-item"`
	}

	if err := d.DecodeElement(&payload, &start); err != nil {
		return err
	}

	for _, repo := range payload.Data {
		newRepo := &Repository{
			ID:        repo.ID,
			Name:      repo.Name,
			Type:      repo.Type,
			Format:    repo.Format,
			Policy:    repo.Policy,
			RemoteURI: repo.RemoteURI,
		}

		*r = append(*r, newRepo)
	}

	return nil
}
Example #2
0
func (r *SolvedRecord) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	rr := struct {
		RunId     string `xml:"run_id"`
		UserId    string `xml:"user_id"`
		ProblemId string `xml:"problem_id"`
		Date      string `xml:"date"`
		Language  string `xml:"language"`
		CpuTime   string `xml:"cputime"`
		Memory    string `xml:"memory"`
		CodeSize  string `xml:"code_size"`
	}{}
	if err := d.DecodeElement(&rr, &start); err != nil {
		return err
	}

	date, err := strconv.ParseInt(strings.Trim(rr.Date, "\n"), 10, 0)
	cpuTime, err := strconv.Atoi(strings.Trim(rr.CpuTime, "\n"))
	memory, err := strconv.Atoi(strings.Trim(rr.Memory, "\n"))
	codeSize, err := strconv.Atoi(strings.Trim(rr.CodeSize, "\n"))
	if err != nil {
		return err
	}

	*r = SolvedRecord{
		RunId:     strings.Trim(rr.RunId, "\n"),
		UserId:    strings.Trim(rr.UserId, "\n"),
		ProblemId: strings.Trim(rr.ProblemId, "\n"),
		Date:      date,
		Language:  strings.Trim(rr.Language, "\n"),
		CpuTime:   cpuTime,
		Memory:    memory,
		CodeSize:  codeSize,
	}
	return nil
}
Example #3
0
func (g *QueryGroup) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error {
	var v struct {
		XMLName xml.Name  `xml:"querygroup"`
		ID      string    `xml:"id,attr"`
		Targets *string   `xml:"targets,attr"`
		Query   []*Query  `xml:"query"`
		Any     *xml.Name `xml:",any"`
	}
	if err := dec.DecodeElement(&v, &start); err != nil {
		return err
	}
	if any := v.Any; any != nil {
		return fmt.Errorf("querygroup %s: invalid element: %s", g.ID, any.Local)
	}
	var targets []string
	switch p := v.Targets; {
	case p != nil && *p != "":
		targets = strings.Split(*p, ",")
	case p != nil:
		targets = []string{}
	}
	*g = QueryGroup{
		ID:      v.ID,
		Targets: targets,
		Query:   v.Query,
	}
	return nil
}
Example #4
0
// UnmarshalXML unmarshals UserData XML.
func (u *UserData) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	// Assume that UserData has the same general key-value structure as
	// EventData does.
	in := struct {
		Pairs []KeyValue `xml:",any"`
	}{}

	// Read tokens until we find the first StartElement then unmarshal it.
	for {
		t, err := d.Token()
		if err != nil {
			return err
		}

		if se, ok := t.(xml.StartElement); ok {
			err = d.DecodeElement(&in, &se)
			if err != nil {
				return err
			}

			u.Name = se.Name
			u.Pairs = in.Pairs
			d.Skip()
			break
		}
	}

	return nil
}
Example #5
0
// UnmarshalXML implements xml.Unmarshaler
func (u *UUID) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	var v []byte
	d.DecodeElement(&v, &start)
	b := u[:]

	return decodeUUID(b, v)
}
Example #6
0
func (m *MSystem) InitFromXml(p *xml.Decoder, t xml.StartElement) error {
	err := p.DecodeElement(m, &t)
	if err != nil {
		return err
	}
	return nil
}
Example #7
0
func (el *Element) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	// prevent recursion
	type Elem Element

	type element struct {
		Elem
		MaxOccursStr string `xml:"maxOccurs,attr"`
	}

	item := element{
		MaxOccursStr: "1",
		Elem: Elem{
			MinOccurs: 1,
		},
	}

	if err := d.DecodeElement(&item, &start); err != nil {
		return err
	}

	if item.MaxOccursStr == "unbounded" {
		el.MaxOccurs = int(math.MaxInt64)
	} else {
		el.MaxOccurs, _ = strconv.Atoi(item.MaxOccursStr)
	}

	el.Name = item.Name
	el.XMLName = item.XMLName
	el.Type = item.Type
	el.MinOccurs = item.MinOccurs

	return nil
}
func (nagc *NegativeAdGroupCriterion) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error {
	for token, err := dec.Token(); err == nil; token, err = dec.Token() {
		if err != nil {
			return err
		}
		switch start := token.(type) {
		case xml.StartElement:
			tag := start.Name.Local
			switch tag {
			case "adGroupId":
				if err := dec.DecodeElement(&nagc.AdGroupId, &start); err != nil {
					return err
				}
			case "criterionUse":
				if err := dec.DecodeElement(&nagc.CriterionUse, &start); err != nil {
					return err
				}
			case "criterion":
				criterion, err := criterionUnmarshalXML(dec, start)
				if err != nil {
					return err
				}
				nagc.Criterion = criterion
			case "AdGroupCriterion.Type":
				break
			default:
				return fmt.Errorf("unknown NegativeAdGroupCriterion field %s", tag)
			}
		}
	}
	return nil
}
Example #9
0
func ParseMetadataCompactDecoded(start xml.StartElement, parser *xml.Decoder, delim string) (*CompactData, error) {
	type XmlMetadataElement struct {
		Resource string `xml:"Resource,attr"`
		/* only valid for table */
		Class string `xml:"Class,attr"`
		/* only valid for lookup_type */
		Lookup  string   `xml:"Lookup,attr"`
		Version string   `xml:"Version,attr"`
		Date    string   `xml:"Date,attr"`
		Columns string   `xml:"COLUMNS"`
		Data    []string `xml:"DATA"`
	}
	xme := XmlMetadataElement{}
	err := parser.DecodeElement(&xme, &start)
	if err != nil {
		fmt.Println("failed to decode: ", err)
		return nil, err
	}
	if xme.Columns == "" {
		return nil, nil
	}
	data := *extractMap(xme.Columns, xme.Data, delim)
	data.Date = xme.Date
	data.Version = xme.Version
	data.Id = xme.Resource
	if xme.Class != "" {
		data.Id = xme.Resource + ":" + xme.Class
	}
	if xme.Lookup != "" {
		data.Id = xme.Resource + ":" + xme.Lookup
	}

	return &data, nil
}
Example #10
0
// UnmarshalXML function unmarshal an <album-list> XML fragment to a Map[string]*Album
func (am *AlbumMap) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	// result
	m := map[string]*Album{}

LOOP:
	for {
		token, err := d.Token()
		if err != nil {
			return err
		}

		switch t := token.(type) {
		case xml.StartElement:

			if t.Name.Local == "album" {
				a := Album{}
				elmt := xml.StartElement(t)
				d.DecodeElement(&a, &elmt)
				m[a.ID] = &a
			}
		case xml.EndElement:
			if t.Name.Local == "album-list" {
				break LOOP
			}
		}
	}

	*am = AlbumMap(m)
	return nil
}
Example #11
0
func (t *Tag) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	t.Name = start.Name
	t.Attr = start.Attr
	for {
		token, err := d.Token()
		if err != nil {
			if err == io.EOF {
				return nil
			}
			return err
		}
		switch token.(type) {
		case xml.StartElement:
			tok := token.(xml.StartElement)
			var data *Tag
			if err := d.DecodeElement(&data, &tok); err != nil {
				return err
			}
			t.Children = append(t.Children, data)
		case xml.CharData:
			t.Children = append(t.Children, token.(xml.CharData).Copy())
		case xml.Comment:
			t.Children = append(t.Children, token.(xml.Comment).Copy())
		}
	}
	return nil
}
Example #12
0
func (p *Problem) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	pp := struct {
		Id                 string       `xml:"id"`
		Name               string       `xml:"name"`
		Available          string       `xml:"available"`
		ProblemTimeLimit   string       `xml:"problemtimelimit"`
		ProblemMemoryLimit string       `xml:"problemmemorylimit"`
		Status             Status       `xml:"status"`
		SolvedList         []SolvedUser `xml:"solved_list>user"`
	}{}
	if err := d.DecodeElement(&pp, &start); err != nil {
		return err
	}

	available, err := strconv.Atoi(strings.Trim(pp.Available, "\n"))
	problemTimeLimit, err := strconv.Atoi(strings.Trim(pp.ProblemTimeLimit, "\n"))
	problemMemoryLimit, err := strconv.Atoi(strings.Trim(pp.ProblemMemoryLimit, "\n"))
	if err != nil {
		return err
	}

	*p = Problem{
		Id:                 strings.Trim(pp.Id, "\n"),
		Name:               strings.Trim(pp.Name, "\n"),
		Available:          available,
		ProblemTimeLimit:   problemTimeLimit,
		ProblemMemoryLimit: problemMemoryLimit,
		Status:             pp.Status,
		SolvedList:         pp.SolvedList,
	}
	return nil
}
Example #13
0
func (c *choice) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error {
	for {
		tok, err := dec.Token()
		if err != nil && err == io.EOF {
			return nil
		}
		if err != nil {
			return err
		}
		switch v := tok.(type) {
		case xml.StartElement:
			switch v.Name.Local {
			case "description":
				d := &description{}
				d.commands = c.commands
				dec.DecodeElement(d, &v)
				switch d.Lang {
				case "en":
					c.DescriptionEn = d
				case "de":
					c.DescriptionDe = d
				}
			}
		}
	}
	return nil
}
Example #14
0
func (freq *Freq) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	var s string
	if err := d.DecodeElement(&s, &start); err != nil {
		return err
	}
	switch s {
	case "":
		*freq = Freq{0}
	case "always":
		// Use second as the minimum unit of change frequence
		*freq = Freq{time.Second}
	case "hourly":
		*freq = Freq{time.Hour}
	case "daily":
		*freq = Freq{time.Hour * 24}
	case "weekly":
		*freq = Freq{time.Hour * 24 * 7}
	case "monthly":
		*freq = Freq{time.Hour * 24 * 30}
	case "yearly":
		*freq = Freq{time.Hour * 24 * 365}
	case "never":
		// time.Duration is int64
		*freq = Freq{1<<63 - 1}
	default:
		return errors.New("invalid frequence: " + s)
	}
	return nil
}
Example #15
0
func (agcs *AdGroupCriterions) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error {
	adGroupCriterionType, err := findAttr(start.Attr, xml.Name{
		Space: "http://www.w3.org/2001/XMLSchema-instance", Local: "type"})
	if err != nil {
		return err
	}
	switch adGroupCriterionType {
	case "BiddableAdGroupCriterion":
		bagc := BiddableAdGroupCriterion{}
		err := dec.DecodeElement(&bagc, &start)
		if err != nil {
			return err
		}
		*agcs = append(*agcs, bagc)
	case "NegativeAdGroupCriterion":
		nagc := NegativeAdGroupCriterion{}
		err := dec.DecodeElement(&nagc, &start)
		if err != nil {
			return err
		}
		*agcs = append(*agcs, nagc)
	default:
		if StrictMode {
			return fmt.Errorf("unknown AdGroupCriterion -> %#v", adGroupCriterionType)
		}
	}
	return nil
}
Example #16
0
func (ccs *CampaignCriterions) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error {
	cc := NegativeCampaignCriterion{}
	for token, err := dec.Token(); err == nil; token, err = dec.Token() {
		if err != nil {
			return err
		}
		switch start := token.(type) {
		case xml.StartElement:
			switch start.Name.Local {
			case "campaignId":
				if err := dec.DecodeElement(&cc.CampaignId, &start); err != nil {
					return err
				}
			case "criterion":
				criterion, err := criterionUnmarshalXML(dec, start)
				if err != nil {
					return err
				}
				cc.Criterion = criterion
			case "BidModifier":
				if err := dec.DecodeElement(&cc.BidModifier, &start); err != nil {
					return err
				}
			}
		}
	}
	*ccs = append(*ccs, cc)
	return nil
}
Example #17
0
// UnmarshalXML appends the property names and values enclosed within start
// to ps.
//
// An xml:lang attribute that is defined either on the DAV:prop or property
// name XML element is propagated to the property's Lang field.
//
// UnmarshalXML returns an error if start does not contain any properties or if
// property values contain syntactically incorrect XML.
func (ps *proppatchProps) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	lang := xmlLang(start, "")
	for {
		t, err := next(d)
		if err != nil {
			return err
		}
		switch elem := t.(type) {
		case xml.EndElement:
			if len(*ps) == 0 {
				return fmt.Errorf("%s must not be empty", start.Name.Local)
			}
			return nil
		case xml.StartElement:
			p := Property{
				XMLName: t.(xml.StartElement).Name,
				Lang:    xmlLang(t.(xml.StartElement), lang),
			}
			err = d.DecodeElement(((*xmlValue)(&p.InnerXML)), &elem)
			if err != nil {
				return err
			}
			*ps = append(*ps, p)
		}
	}
}
Example #18
0
// UnmarshalXML implements the XML Unmarshaler interface
func (s *Show) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	nfo := showFields{}
	if err := d.DecodeElement(&nfo, &start); err != nil {
		return err
	}

	s.Title = nfo.Title
	s.Rating = nfo.Rating
	s.Plot = nfo.Plot
	s.URL = nfo.URL
	s.TvdbID = nfo.TvdbID
	s.ImdbID = nfo.ImdbID
	s.Year = nfo.Year

	if nfo.Premiered != "" {
		firstAired, err := time.Parse("2006-01-02", nfo.Premiered)
		if err != nil {
			return err
		}

		s.FirstAired = &firstAired
	}

	return nil
}
// SEE http://play.golang.org/p/EFXZNsjE4a and
// http://stackoverflow.com/questions/17301149/golang-xml-unmarshal-and-time-time-fields
func (c *customTime) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	var v string
	d.DecodeElement(&v, &start)
	parse, _ := time.Parse(TimeFormat, v)
	*c = customTime{parse}
	return nil
}
Example #20
0
func (s *SolvedUser) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	ss := struct {
		Id             string `xml:"id"`
		SubmissionDate string `xml:"submissiondate"`
		Language       string `xml:"language"`
		CpuTime        string `xml:"cputime"`
		Memory         string `xml:"memory"`
		CodeSize       string `xml:"code_size"`
	}{}

	if err := d.DecodeElement(&ss, &start); err != nil {
		return err
	}

	submissionDate, _ := strconv.ParseInt(strings.Trim(ss.SubmissionDate, "\n "), 10, 64)
	cpuTime, _ := strconv.Atoi(strings.Trim(ss.CpuTime, "\n "))
	memory, _ := strconv.Atoi(strings.Trim(ss.Memory, "\n "))
	codeSize, _ := strconv.Atoi(strings.Trim(ss.CodeSize, "\n "))
	*s = SolvedUser{
		Id:             strings.Trim(ss.Id, "\n "),
		SubmissionDate: submissionDate,
		Language:       strings.Trim(ss.Language, "\n "),
		CpuTime:        cpuTime,
		Memory:         memory,
		CodeSize:       codeSize,
	}
	return nil
}
Example #21
0
func (dv *DateVal) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	var content string
	if err := d.DecodeElement(&content, &start); err != nil {
		return err
	}
	return dv.FromString(content)
}
Example #22
0
func (self *Definitions) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
	err = d.DecodeElement(&self.InnerDefinitions, &start)
	if err != nil {
		return
	}

	self.XMLName = start.Name
	self.Aliases = map[string]string{}

	self.Types.Schemas = xsd.SchemaMap{}
	for _, schema := range self.Types.Schemata {
		self.Types.Schemas[schema.TargetNamespace] = schema
	}

	for _, attr := range start.Attr {
		if _, ok := self.Aliases[attr.Name.Local]; !ok {
			self.Aliases[attr.Name.Local] = attr.Value
		}

		for k := range self.Types.Schemas {
			if _, ok := self.Types.Schemas[k].Aliases[attr.Name.Local]; !ok {
				self.Types.Schemas[k].Aliases[attr.Name.Local] = attr.Value
			}
		}
	}

	return
}
Example #23
0
// An <xs:annotation> element may contain zero or more <xs:documentation>
// children.  The xsd package joins the content of these children, separated
// with blank lines.
func (doc *annotation) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	buf := make([][]byte, 1)
	var (
		tok xml.Token
		err error
	)

Loop:
	for {
		tok, err = d.Token()
		if err != nil {
			break
		}

		switch tok := tok.(type) {
		case xml.EndElement:
			break Loop
		case xml.StartElement:
			if (tok.Name != xml.Name{schemaNS, "documentation"}) {
				if err := d.Skip(); err != nil {
					return err
				}
			}
			var frag []byte
			if err := d.DecodeElement(&frag, &tok); err != nil {
				return err
			}
			buf = append(buf, bytes.TrimSpace(frag))
		}
	}
	*doc = annotation(bytes.TrimSpace(bytes.Join(buf, []byte("\n\n"))))
	return err
}
Example #24
0
// Stupid hack to get innerxml without defining a new struct with a single
// member due to the fact that Abstract field has <p> HTML element
func (a *Abstract) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	temp := struct {
		Text string `xml:",innerxml"`
	}{}
	d.DecodeElement(&temp, &start)
	*a = Abstract(temp.Text)
	return nil
}
Example #25
0
// Custom Unmarshaller in order to interpret time formats in the azure expected
// format
func (c *azureTimeFormat) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	const shortForm = "Mon, 2 Jan 2006 15:04:05 MST" // date format of azure xml responses
	var v string
	d.DecodeElement(&v, &start)
	parse, err := time.Parse(shortForm, v)
	*c = azureTimeFormat{parse}
	return err
}
Example #26
0
// UnmarshalXML implements a custom XML unmarshaler that ignores time parsing errors.
// http://stackoverflow.com/a/25015260
func (t *Time) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	var v string
	d.DecodeElement(&v, &start)
	if tt, err := time.Parse(time.RFC3339, v); err == nil {
		*t = Time{tt}
	}
	return nil
}
Example #27
0
// UnmarshalXML реализует интерфейс xml.Unmarshaler для объекта Date
// десериализация происходит с учётом шаблона, заданного в свойстве Layout
func (d *Date) UnmarshalXML(decoder *xml.Decoder, start xml.StartElement) error {
	var content string
	if err := decoder.DecodeElement(&content, &start); err != nil {
		return err
	}
	d.fixLayout()
	return parse(d, content)
}
Example #28
0
func (cl *CommaList) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	var content string
	if err := d.DecodeElement(&content, &start); err != nil {
		return err
	}
	cl.FromString(content)
	return nil
}
Example #29
0
File: xml.go Project: jkeiser/tvdb
func (array *PipeDelimitedString) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	var content string
	if err := d.DecodeElement(&content, &start); err != nil {
		return err
	}
	*array = strings.Split(content, "|")
	return nil
}
Example #30
0
// UnmarshalXML unmarshals an int properly, as well as marshaling an empty string to nil.
func (n *NullInt) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	var v int
	err := d.DecodeElement(&v, &start)
	if err == nil {
		*n = NullInt{Int: v, Valid: true}
	}

	return nil
}