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

inline tag with one attribute #80

Open
SchweinchenFuntik opened this issue Jan 21, 2022 · 1 comment
Open

inline tag with one attribute #80

SchweinchenFuntik opened this issue Jan 21, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@SchweinchenFuntik
Copy link

SchweinchenFuntik commented Jan 21, 2022

I have this xml structure:

...
<Header>
  <ModelInfo value="WorkspaceSettings" />
  <ModelVersion value="1.3.0.0" />
  <PersistencyVersion value="1.0.0.0" />
</Header>
...

As a result, you need to create as many as three classes:

@Serializable
class Header(
    val modelInfo: ModelInfo,
    val modelVersion: ModelVersion,
    val persistencyVersion: PersistencyVersion,
)

@Serializable class ModelInfo(val value: String)
@Serializable class ModelVersion(val value: String)
@Serializable class PersistencyVersion(val value: String)

I would like to simplify to:

annotation class XmlValueFromTagAttribute(val tag: String, val attribute: String = "value")

@Serializable
class Header(
    @XmlValueFromTagAttribute("ModelInfo", "value") val modelInfo: String,
    @XmlValueFromTagAttribute("ModelVersion", "value") val modelVersion: String,
    @XmlValueFromTagAttribute("PersistencyVersion", "value") val persistencyVersion: String,
)

this is an example of such a place, actually weight xml-file has this structure

PS: @XmlValueFromTagAttribute - long name, i would like it to be shorter

@pdvrieze pdvrieze added the enhancement New feature or request label Jan 26, 2022
@pdvrieze
Copy link
Owner

Interesting one. Clearly not in the "philosophy" of XML. The use of "value" like attributes isn't really intended, and just using element content itself is possible. Another one is that tag names are sort-of expected to relate to type names. Note that you could use a single wrapper object as follows (I'll add it as a feature request anyway, but I'm not sure how best to go about achieving it):

@Serializable
data class StringValue(@XmlElement(false)val value: String)

@Serializable
class Header(
    @XmlSerialName("ModelInfo", "", "") val modelInfo: StringValue,
    @XmlSerialName("ModelVersion", "", "") val modelVersion: String,
    @XmlSerialName("PercistencyVersion", "", "")  val persistencyVersion: String,
)

Instead of StringValue you could also use a parameterised container.

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

No branches or pull requests

2 participants