-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
proposal: encoding/xml: context-aware XML decoders #22290
Comments
We don't normally use Interrupting XML parsing based on a deadline doesn't sound like a very useful feature to me. It would be reasonable for the reader to implement some sort of deadline, but that doesn't require passing a context through the XML code. |
I see. How about adding just an publicly accessible |
Can you give an example of where that would be used? |
I'm implementing a custom XML parser, and I want to support "strict" and "non-strict" modes. Strict mode being disallowing cdata and unknown elements, and a few other tweaks. |
Seems like if you are implementing a custom XML parser, it can do whatever you like. The question here is what do with the standard encoding/xml parser. encoding/xml doesn't take any options that an XML unmarshaler could reasonably change, so I don't see a reason to give the unmarshaling methods a mechanism for changing options. |
Well, I have phrased incorrectly. I'm not building a custom parser, but custom |
It's likely that I still misunderstand, but the current XML parser is not designed to be extended, and that is not a feature we are going to add. We've had enough trouble with it. And of course one doesn't normally pass multiple types to an XML decoder, so I'm still having some trouble envisioning what you want. |
This is a concrete example: type Decoder struct {
...
CustomState interface{}
} However if it's been problematic with |
More explaination: |
I suppose you could do that by writing
All the various custom unmarshal types would point to the same state. Afterward you would extract the values. |
With that approach I kind of have to have a copy of the type hierarchy with every type wrapped with |
I agree it's not great. I wouldn't go so far as to say that Go is idiomatically against that kind of thing. In any case I don't think the standard library encoding/xml package is going to provide the kind of flexibility you are looking for. It seems very special purpose to me, and will tend to be a maintenance burden on the standard library. I recommend just copying the standard library package and modifying it to serve your needs. |
I am strongly opposed to using the context type in this fashion. At best context is used for handling the lifetime of a request, it’s use as an arbitrary bag of values in this proposal is antithetical to its intended use.
… On 17 Oct 2017, at 00:16, MOZGIII ***@***.***> wrote:
I think it would be a good idea to add context.Context to xml.Decoder.
Addition of context will allow passing various options to UnmarshalerXML implementations per decoding procedure, rather that by maintaining some state on the side.
There are xml.HTMLAutoClose and xml.HTMLEntity that could be also stored in the context.Context of xml.Decoder instead of having them as global values. To maintaing backward compatibility those variables can remain to be consulted if no values is set on the context.
As a bonus, it will allow both internal and custom unmarshaling logic to abort parsing on deadline.
What do you think?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
[Repeating answer on very similar #23059] Contexts are not for per-operation options. It's for timeouts and more global things like authorization credentials, not fine-grained per-call options. We pulled sql.TxOption out of context before Go 1.9 for the same reason. Think about "would this be appropriate to set and force into all operations during a given client RPC", even nested operations calling into unrelated different APIs in back ends (to gather information for the response). Unfortunately I don't know of a good post explaining this nuance with context. More generally, the xml package is almost completely done. If you need significantly extended semantics (and it seems you do), then it would be fine to copy the current one and make your own modifications, creating a new package customized to your use case. |
Makes sense. I agree with your point @rsc. By the way, is it a good practice to copy the parts of the standard library? |
You'll get a better answer to this question over on golang-nuts or some other forum (https://golang.org/wiki/Questions). Personally I see no problem with it but others may have different perspectives. |
I think it would be a good idea to add
context.Context
toxml.Decoder
.Addition of context will allow passing various options to
UnmarshalerXML
implementations per decoding procedure, rather that by maintaining some state on the side.There are
xml.HTMLAutoClose
andxml.HTMLEntity
that could be also stored in thecontext.Context
ofxml.Decoder
instead of having them as global values. To maintain backward compatibility those variables can remain to be consulted if no values is set on the context.As a bonus, it will allow both internal and custom unmarshaling logic to abort parsing on deadline.
What do you think?
The text was updated successfully, but these errors were encountered: