Subject: [netmod] question about unions

From: David Reid <[email-masked]>
Date: Wed, 01 Apr 2009 15:30:28 -0400

If I have the following union:

   leaf foo {
      type union {
         type int32;
         type string;
      }
   }

and I get the following data on the wire:

  <foo>10</foo>

How do I know if that is the number 10 or the string "10"?
Do I need a discriminator in my data model if I can't tell from the context?

-David Reid

---

From: Andy Bierman <[email-masked]>
Date: Wed, 01 Apr 2009 12:51:24 -0700

> Do I need a discriminator in my data model if I can't tell from the context?

It is really easy to implement.
You test the string "10" as a valid value for
each typedef in the union (recurse for nested unions).
The first typedef to accept the string-to-whatever conversion 'wins'.
They are evaluated in schema order.  You probably want to save
the actual builtin type that won, not just 'union'.

> -David Reid

Andy

---

From: David Reid <[email-masked]>
Date: Wed, 01 Apr 2009 16:09:42 -0400

> the actual builtin type that won, not just 'union'.

If I receive <foo>010</foo> in edit-config, is that a string or int32. Do I
return <foo>10</foo> or <foo>010</foo> in a subsequent get-config?

-David Reid

---

From: Andy Bierman <[email-masked]>
Date: Wed, 01 Apr 2009 13:21:12 -0700

> return <foo>10</foo> or <foo>010</foo> in a subsequent get-config?

int32.
you return <foo>8</foo> in your <get> reply because the original
value was given to you in octal notation.

Andy

---

From: Martin Bjorklund <[email-masked]>
Date: Wed, 01 Apr 2009 22:22:13 +0200 (CEST)

> return <foo>10</foo> or <foo>010</foo> in a subsequent get-config?

The idea is that you match the union's member types in the order they
are specified.  This is consistent with XSD.  Looking at the text, I
realize that this description is missing.  I suggest we add this in
the section about unions:

  When a string representing a union data type is validated, the
  string is validated against each member type, in the order they are
  specified in the type statement, until a match is found.

Can the text be more clear?

/martin

---

From: Martin Bjorklund <[email-masked]>
Date: Wed, 01 Apr 2009 22:23:31 +0200 (CEST)

> > return <foo>10</foo> or <foo>010</foo> in a subsequent get-config?

Yes.

> value was given to you in octal notation.

No, the octal represention is not allowed in the XML encoding, so 010
is interpreted as 10.

/martin

---

From: Andy Bierman <[email-masked]>
Date: Wed, 01 Apr 2009 13:26:27 -0700

> No, the octal represention is not allowed in the XML encoding, so 010

You are right.
I'm sorry.  Just 'default 010;' would be interpreted that way.

Andy

---

From: David Reid <[email-masked]>
Date: Wed, 01 Apr 2009 16:48:00 -0400

>   specified in the type statement, until a match is found.

That text helps considerably. I definitely agree that it should be added.

Let me make sure I understand.

Given the following unions:

    leaf foo {
       type union {
          type int32;
          type string;
       }
    }
    leaf fum {
       type union {
          type string;
          type int32;
       }
    }

and the following values in edit-config:

   <foo>010</foo>
   <fum>010</fum>

A subsequent get-config would return:

   <foo>10</foo>
   <fum>010</fum>

-David Reid

---

From: Martin Bjorklund <[email-masked]>
Date: Wed, 01 Apr 2009 23:01:30 +0200 (CEST)

Yes.

/martin

---

From: David Reid <[email-masked]>
Date: Wed, 08 Apr 2009 17:00:35 -0400

> If I have the following union:

Is it possible in the above example to send the string '010'?
Since '010' will be treated as the integer 10, I'm assuming that
there is no way to pass '010' as a string in union like this.
Is that right?

I'm actually interested in a union like this:

   leaf fum { 
      type union {
         type binary;
         type string;
      }
  }

I think this example has the same type of problem since some strings are
also legal binary encodings.

-David Reid

---

From: Martin Bjorklund <[email-masked]>
Date: Tue, 14 Apr 2009 10:46:48 +0200 (CEST)

> there is no way to pass '010' as a string in union like this.

Yes, that is right.

/martin