Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create multiple structs instead of merging same-name elements? #19

Open
xeoncross opened this issue Apr 24, 2017 · 2 comments
Open

Create multiple structs instead of merging same-name elements? #19

xeoncross opened this issue Apr 24, 2017 · 2 comments

Comments

@xeoncross
Copy link

Right now Chidley merges all the child elements / attributes of each tag into a struct of the same name. Is there a way to tell it to keep separate XML elements instead of appending all the keys into a single master struct named the shared element name?

	<root>
		<children>
			<sub>
				<children>
					<foo>...</foo>
				</children>
			</sub>
		</children>
	</root>

Creates a struct that looks like this:

	type Children struct {
		Sub ...
		Foo ...
	}

Instead of multiple structs

type Children struct {
	Sub
}

type SubChildren struct {
	Foo
}
@gnewton
Copy link
Owner

gnewton commented Apr 25, 2017

chidley behaves differently from what you describe.
Given:

<doc>
  <children>
    <sub>
      <children>
	<foo>
	</foo>
      </children>
    </sub>
  </children>
</doc>

chidley produces:

type ChiChildren struct {
	ChiFoo *ChiFoo `xml:" foo,omitempty" json:"foo,omitempty"`
	ChiSub *ChiSub `xml:" sub,omitempty" json:"sub,omitempty"`
}

type ChiDoc struct {
	ChiChildren *ChiChildren `xml:" children,omitempty" json:"children,omitempty"`
}

type ChiFoo struct {
}

type ChiRoot struct {
	ChiDoc *ChiDoc `xml:" doc,omitempty" json:"doc,omitempty"`
}

type ChiSub struct {
	ChiChildren *ChiChildren `xml:" children,omitempty" json:"children,omitempty"`
}

Some important differences: chidley makes a union for each tag. That is, it looks at all instances of the tag and does a union of all of these examples, no matter where they occur. So in your example, where the tag children shows up in different places, it looks at its members and creates a "union" struct for all of these examples. So there is no SubChildren struct. There is just a Children struct that is true for all of the encountered examples of the children tag.

Getting back to your question :-)
chidley does not put everything into one big struct (as show above and in the examples in the docs). So, I think perhaps I am not understanding your request. Maybe tell me a little more? :-)

Thanks,
Glen

@xeoncross
Copy link
Author

xeoncross commented Apr 25, 2017

As you say, a "union" struct is created by Chidley.

chidley produces:

type ChiChildren struct {
ChiFoo *ChiFoo xml:" foo,omitempty" json:"foo,omitempty"
ChiSub *ChiSub xml:" sub,omitempty" json:"sub,omitempty"
}

Which is what I was saying:

	type Children struct {
		Sub ...
		Foo ...
	}

As I'm sure you know, building structs by merging all possible child element names is messy for some XML which re-uses the same element name repeatedly in different contexts.

Is there a way to turn off "union" mode and instead create multiple structs named by something like element path? For example:

type DocChildren struct {
	Sub
}

type DocChildrenSubChildren struct {
	Foo
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants