File: unmarshal.go

package info (click to toggle)
golang-github-russellhaering-goxmldsig 1.1.0-1%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 276 kB
  • sloc: sh: 11; makefile: 2
file content (43 lines) | stat: -rw-r--r-- 985 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package etreeutils

import (
	"encoding/xml"

	"github.com/beevik/etree"
)

// NSUnmarshalElement unmarshals the passed etree Element into the value pointed to by
// v using encoding/xml in the context of the passed NSContext. If v implements
// ElementKeeper, SetUnderlyingElement will be called on v with a reference to el.
func NSUnmarshalElement(ctx NSContext, el *etree.Element, v interface{}) error {
	detatched, err := NSDetatch(ctx, el)
	if err != nil {
		return err
	}

	doc := etree.NewDocument()
	doc.AddChild(detatched)
	data, err := doc.WriteToBytes()
	if err != nil {
		return err
	}

	err = xml.Unmarshal(data, v)
	if err != nil {
		return err
	}

	switch v := v.(type) {
	case ElementKeeper:
		v.SetUnderlyingElement(el)
	}

	return nil
}

// ElementKeeper should be implemented by types which will be passed to
// UnmarshalElement, but wish to keep a reference
type ElementKeeper interface {
	SetUnderlyingElement(*etree.Element)
	UnderlyingElement() *etree.Element
}