Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Implement
write_serializable_content
on element writer #508New 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
Implement
write_serializable_content
on element writer #508Changes from all commits
12fee25
8d833d8
0178623
541aae3
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, it would be valuable to add a test as doc example here. Pay particular attention to which tag name the type will be serialized with
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would expect
here. This also means that you need to test that the attribute lists will be merged correctly. It also would be valuable to move the entire test to doctests for
write_serializable_content
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Mingun I would expect it to produce the same XML it would if you were to serialize it in a different context, but I don't think that would be the case if it produced the XML you suggest?
If you were to just serialize a
Foo
, then I would expect<foo>...inner_contents...</foo>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I asked myself this question and I can see both use cases being valid. Maybe we should leave this as an option for that function? Although I'm not sure how to tell the serializer to not produce the
<foo>
tags if we don't want them.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assuming it works as it does in your test case and in my description, it just feels extremely niche. The
ElementWriter
construct only allows you to write one inner value wrapped in a pair of tags, and I can't think of many XML structures where you'd want 2 levels of nesting like that for only one possible value inner value. The construct is basically just too limiting to make it broadly useful.Wheras with the other approach of only supporting it on the writer, you can still accomplish the same thing, and it would be more flexible - if slightly more verbose for that one strange case.
If it were to work the way @Mingun describes then none of this necessarily applies. I'm just not sure I understand why the outer tags wouldn't be included when on the deserialization side you would expect some struct fields to potentially be sourced from attributes. Shouldn't it work the same in the opposite direction?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dralley, I proceed from the following considerations:
<foo>...</foo>
foo
could be mapped to a field of a struct. This is a basic expectation.<foo>...</foo>
, so we could in the result a type which content is mapped to the...
in our XML. This is a second basic expectation???
let's expand our example:bar
content we can use pathfoo.bar
. That can be naturally expressed with nested structs:bar
should have aBarType
similar toFooType
. Our rules should be recursive.BarType
and how it is presented in XML:BarType
is a<bar>
element. Remember, that this XML can be described with following XSD:BarType
can be mapped to XSD'sBarType
.bar
? XML responds by saying that thebar
is part of the document. For example, XmlBeans implements this concept by generation a classBarDocument
, which is different from classBarType
that represents type of<bar>
element and contains actual data.I think, now it is obvious why I think, that serialization of a Rust type should not generate an element name -- because that name is not part of an xml-fragment (a type). There is, however, one significant semi-exception to this rule.
enum
at the top level is convenient to represent as a document. But note that even in that case the (root) element name mapped to the variant name, not theenum
's type name.I'll plan to write a doc about that in #369.
Although I must say that there are several subtle moments that may not be quite obvious. For example, serialization of sequences and
enum
s. It is best to create tests and imagine what result we expect in each case? Will the selected mapping option be consistent?This depends on whether
Foo
is represent the root element type or not. In the latter case, you most likely expect that the name of the element will have a name of struct field of typeFoo
as I shown in the beginning of that comment.The
Deserializer
andSerializer
are worked as I described. For convenience, theFoo
type can simultaneously represent both the data type and the document type -- the root tag can be derived from the type name, this is the only place in the serializer where the type name is used. The deserializer never uses a type name.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Mingun So let's that
Foo
had some fields mapped to attributes, how does that work? If theFoo
element is erased and only its fields are serialized, any fields mapped to attributes will have nowhere to be serialized to.I can appreciate that there are limitations with the serde model which may limit our ability to achieve certain outcomes, but I would rather not do something than to do it in a way that is incorrect or illogical.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would expect (indentation depends on settings):