Discussion:
[Yaml-core] The same tag for different kinds
Andrey Somov
2016-03-10 15:35:17 UTC
Permalink
Hi all,
does the specification put any restriction on how the same tag can be used
for
different node kinds (scalar, sequence, mapping) ?
For instance, must the parser provide a possibility to define 3 different
ways to construct a node for the document below ?
Is this a valid document ?

%YAML 1.1
---
scalar: !foo bar
seq: !foo [bar]
map: !foo {bar: null}

Cheers,
Andrey
Oren Ben-Kiki
2016-03-10 16:39:11 UTC
Permalink
Section 3.2.1.2 Tags:
"In particular, each tag must specify the expected node kind
<http://www.yaml.org/spec/1.2/spec.html#kind//> (scalar
<http://www.yaml.org/spec/1.2/spec.html#scalar//>, sequence
<http://www.yaml.org/spec/1.2/spec.html#sequence//>, or mapping
<http://www.yaml.org/spec/1.2/spec.html#mapping//>)".
Post by Andrey Somov
Hi all,
does the specification put any restriction on how the same tag can be used
for
different node kinds (scalar, sequence, mapping) ?
For instance, must the parser provide a possibility to define 3 different
ways to construct a node for the document below ?
Is this a valid document ?
%YAML 1.1
---
scalar: !foo bar
seq: !foo [bar]
map: !foo {bar: null}
Cheers,
Andrey
------------------------------------------------------------------------------
Transform Data into Opportunity.
Accelerate data analysis in your applications with
Intel Data Analytics Acceleration Library.
Click to learn more.
http://pubads.g.doubleclick.net/gampad/clk?id=278785111&iu=/4140
_______________________________________________
Yaml-core mailing list
https://lists.sourceforge.net/lists/listinfo/yaml-core
Andrey Somov
2016-03-10 17:10:01 UTC
Permalink
Post by Oren Ben-Kiki
"In particular, each tag must specify the expected node kind
<http://www.yaml.org/spec/1.2/spec.html#kind//> (scalar
<http://www.yaml.org/spec/1.2/spec.html#scalar//>, sequence
<http://www.yaml.org/spec/1.2/spec.html#sequence//>, or mapping
<http://www.yaml.org/spec/1.2/spec.html#mapping//>)".
Yes, I have read that and that is why I ask.

The tag itself does not give any indication which node kind it needs.
Or "each tag must specify the expected node kind" must be read as "each tag
can only be applied to the node kind it is used with" ?
My question is: should the parser be prepared to get a custom tag for
different kinds ?
Should the parser allow to configure to call different custom
implementations for different kinds for the same tag ?

Andrey
Post by Oren Ben-Kiki
Post by Andrey Somov
Hi all,
does the specification put any restriction on how the same tag can be
used for
different node kinds (scalar, sequence, mapping) ?
For instance, must the parser provide a possibility to define 3 different
ways to construct a node for the document below ?
Is this a valid document ?
%YAML 1.1
---
scalar: !foo bar
seq: !foo [bar]
map: !foo {bar: null}
Cheers,
Andrey
Oren Ben-Kiki
2016-03-10 19:21:42 UTC
Permalink
Post by Andrey Somov
The tag itself does not give any indication which node kind it needs.
What do you mean? The spec requires certain things from each tag, this is
one.
Post by Andrey Somov
Or "each tag must specify the expected node kind" must be read as "each
tag can only be applied to the node kind it is used with" ?
Yes.
Post by Andrey Somov
My question is: should the parser be prepared to get a custom tag for
different kinds ?
Should the parser allow to configure to call different custom
implementations for different kinds for the same tag ?
I'm assuming you are misusing the word parser here. A YAML processor that
constructs native application data needs to (1) associate tags with all
nodes and (2) map the tags to native data types and (3) then construct them.

There's no requirement that different tags map to different native data
types (e.g., in JavaScript, !!int and !!float map to the same native number
type).

YAML requires that tags-that-are-associated-with-scalar-nodes be different
than tags-that-are-associated-with-map-nodes which must be different than
tags-that-are-associated-with-sequence-nodes.

BTW, since tags are URIs, one can do things like !!foo#scalar vs. !!foo#map
vs. !!foo#seq to communicate they are related. Or use `/` or `::` or `-` or
whatever.

Oren.
Andrey Somov
2016-03-10 21:22:39 UTC
Permalink
Post by Oren Ben-Kiki
Post by Andrey Somov
The tag itself does not give any indication which node kind it needs.
What do you mean? The spec requires certain things from each tag, this is
one.
I mean that !foo is not related to any kind. It can be used with any node.
Post by Oren Ben-Kiki
Post by Andrey Somov
Or "each tag must specify the expected node kind" must be read as "each
tag can only be applied to the node kind it is used with" ?
Yes.
Post by Andrey Somov
My question is: should the parser be prepared to get a custom tag for
different kinds ?
Should the parser allow to configure to call different custom
implementations for different kinds for the same tag ?
I'm assuming you are misusing the word parser here. A YAML processor that
constructs native application data needs to (1) associate tags with all
nodes and (2) map the tags to native data types and (3) then construct them.
There's no requirement that different tags map to different native data
types (e.g., in JavaScript, !!int and !!float map to the same native number
type).
YAML requires that tags-that-are-associated-with-scalar-nodes be different
than tags-that-are-associated-with-map-nodes which must be different than
tags-that-are-associated-with-sequence-nodes.
I think this is the answer to my question. It means that users have to
provide a separate implementation for every kind for the same tag.

(1) associate tags with all nodes => this is done by the processor
(2) map the tags to native data types => this has to be implemented by
users
(3) then construct them => this has to be implemented by users
Post by Oren Ben-Kiki
BTW, since tags are URIs, one can do things like !!foo#scalar vs.
!!foo#map vs. !!foo#seq to communicate they are related. Or use `/` or `::`
or `-` or whatever.
Oren.
What should the processor do in this case ?

%YAML 1.1
---
scalar: !foo#map bar
map: !foo#scalar {a: 1}
Oren Ben-Kiki
2016-03-11 00:30:36 UTC
Permalink
Presumably complain. What would it do with !!int {} ?
Post by Andrey Somov
Post by Oren Ben-Kiki
Post by Andrey Somov
The tag itself does not give any indication which node kind it needs.
What do you mean? The spec requires certain things from each tag, this is
one.
I mean that !foo is not related to any kind. It can be used with any node.
Post by Oren Ben-Kiki
Post by Andrey Somov
Or "each tag must specify the expected node kind" must be read as "each
tag can only be applied to the node kind it is used with" ?
Yes.
Post by Andrey Somov
My question is: should the parser be prepared to get a custom tag for
different kinds ?
Should the parser allow to configure to call different custom
implementations for different kinds for the same tag ?
I'm assuming you are misusing the word parser here. A YAML processor that
constructs native application data needs to (1) associate tags with all
nodes and (2) map the tags to native data types and (3) then construct them.
There's no requirement that different tags map to different native data
types (e.g., in JavaScript, !!int and !!float map to the same native number
type).
YAML requires that tags-that-are-associated-with-scalar-nodes be
different than tags-that-are-associated-with-map-nodes which must be
different than tags-that-are-associated-with-sequence-nodes.
I think this is the answer to my question. It means that users have to
provide a separate implementation for every kind for the same tag.
(1) associate tags with all nodes => this is done by the processor
(2) map the tags to native data types => this has to be implemented by
users
(3) then construct them => this has to be implemented by users
Post by Oren Ben-Kiki
BTW, since tags are URIs, one can do things like !!foo#scalar vs.
!!foo#map vs. !!foo#seq to communicate they are related. Or use `/` or `::`
or `-` or whatever.
Oren.
What should the processor do in this case ?
%YAML 1.1
---
scalar: !foo#map bar
map: !foo#scalar {a: 1}
Andrey Somov
2016-03-11 08:18:29 UTC
Permalink
Post by Oren Ben-Kiki
Presumably complain. What would it do with !!int {} ?
Yes, thank you. This is what I would like.
With the standard tags there is no question, it is a matter of consistency.
But for the _custom tags_ this is unclear in the specification (at least
for me).

At the moment SnakeYAML, PyYAML (and probably other processors) let users
define custom tags by providing a function Node => Object which is invoked
when the
custom tag is detected.
The interface is approx. like this:
register(<tag>, function<node => Object>)

Then this interface is incomplete. Registering the function _must_ specify
the node kind
register(<kind>, <tag>, function<node => Object>)

It also means that user may specify 3 different functions for each tag.

I think it is worth to mention it in the YAML 2.0 spec for clarity.

Andrey
Oren Ben-Kiki
2016-03-11 08:36:19 UTC
Permalink
Technically the library should reject an attempt to register the same tag
with different kinds...

Something like resolvers[tag] = { required-kind: ..., condition-on-path:
..., condition-on-content: ..., construction-function: ... } would provide
the correct data model.

Oren.
Post by Andrey Somov
Post by Oren Ben-Kiki
Presumably complain. What would it do with !!int {} ?
Yes, thank you. This is what I would like.
With the standard tags there is no question, it is a matter of consistency.
But for the _custom tags_ this is unclear in the specification (at least
for me).
At the moment SnakeYAML, PyYAML (and probably other processors) let users
define custom tags by providing a function Node => Object which is invoked
when the
custom tag is detected.
register(<tag>, function<node => Object>)
Then this interface is incomplete. Registering the function _must_ specify
the node kind
register(<kind>, <tag>, function<node => Object>)
It also means that user may specify 3 different functions for each tag.
I think it is worth to mention it in the YAML 2.0 spec for clarity.
Andrey
------------------------------------------------------------------------------
Transform Data into Opportunity.
Accelerate data analysis in your applications with
Intel Data Analytics Acceleration Library.
Click to learn more.
http://pubads.g.doubleclick.net/gampad/clk?id=278785111&iu=/4140
_______________________________________________
Yaml-core mailing list
https://lists.sourceforge.net/lists/listinfo/yaml-core
Andrey Somov
2016-03-11 08:58:23 UTC
Permalink
Post by Oren Ben-Kiki
Technically the library should reject an attempt to register the same tag
with different kinds...
It means that this YAML is invalid (or not ?):
---
scalar: !foo bar
seq: !foo [bar]
...

As far as I understand there is no restriction now to use the very same
custom tag for different kinds.

Andrey
Post by Oren Ben-Kiki
..., condition-on-content: ..., construction-function: ... } would provide
the correct data model.
Oren Ben-Kiki
2016-03-11 09:13:26 UTC
Permalink
Technically it isn't valid.
Post by Andrey Somov
Post by Oren Ben-Kiki
Technically the library should reject an attempt to register the same tag
with different kinds...
---
scalar: !foo bar
seq: !foo [bar]
...
As far as I understand there is no restriction now to use the very same
custom tag for different kinds.
Andrey
Post by Oren Ben-Kiki
..., condition-on-content: ..., construction-function: ... } would provide
the correct data model.
Andrey Somov
2016-03-11 09:27:24 UTC
Permalink
Thank you Oren.
Post by Oren Ben-Kiki
Technically it isn't valid.
Post by Andrey Somov
Post by Oren Ben-Kiki
Technically the library should reject an attempt to register the same
tag with different kinds...
---
scalar: !foo bar
seq: !foo [bar]
...
As far as I understand there is no restriction now to use the very same
custom tag for different kinds.
Andrey
Post by Oren Ben-Kiki
..., condition-on-content: ..., construction-function: ... } would provide
the correct data model.
Loading...