I have 160+ .xsd schemas and for each file I generated xsd.go files with xsdgen, but each document has common Header of CommonType.
namespace some_document
type Document struct {
Header CommonType `xml:"header"`
// Other fields
}
type CommonType struct {
A A `xml:"a"`
B B `xml:"b"`
C1 C `xml:"c1"`
C2 C `xml:"c2"`
D D `xml:"d"`
}
type A string
type B string
type C string
type D string
namespace any_document
type Document struct {
Header CommonType `xml:"header"`
// Other fields
}
type CommonType struct {
A A `xml:"a"`
B B `xml:"b"`
C1 C `xml:"c1"`
C2 C `xml:"c2"`
D D `xml:"d"`
}
type A string
type B string
type C string
type D string
Also, for each schema I create a function for building it. I want to create a function BuildHeader and reuse it in builder's functions. Is there any way to create common namespace with this type and use it instead any_document.CommonType and some_document.CommonType? I don't want to edit code generated files.
namespace common
type CommonType struct {
A A `xml:"a"`
B B `xml:"b"`
C1 C `xml:"c1"`
C2 C `xml:"c2"`
D D `xml:"d"`
}
type A string
type B string
type C string
type D string
My thoughts:
- extract Header in package 'common' and change CommonType to common.CommonType for each document
- ...?
If you are dealing with generated structures, there is no easy way of doing this. You can, however, write one function to initialize the common header and use it for all generated types, provided they are structurally identical.
Then you can use: