danielgtaylor / danielgtaylor/python-betterproto

OneOf Enforcement?

Offen
#189 3 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Python
Sterne
1.8k
Forks
234
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

Thanks for all of your work on betterproto, it's been immensely helpful.

What is betterproto's canonical way of enforcing Protobuf `oneof`s are only set to one variant?

From this demo-protobuf:

```proto
syntax = "proto3";
package demo;

message Thing1 {
string name = 1;
}
message Thing2 {
string name = 1;
}
message Thing3 {
string name = 1;
}
message Thing4 {
string name = 1;
}

message OneThing {
oneof t {
Thing1 t1 = 1;
Thing2 t2 = 2;
Thing3 t3 = 3;
Thing4 t4 = 4;
}
}
```

Creating a `OneThing` appears produce default values of non-used `oneof` variants.

```
In [2]: OneThing(t1=Thing1(name='cy'))
Out[2]: OneThing(t1=Thing1(name='cy'), t2=Thing2(name=''), t3=Thing3(name=''), t4=Thing4(name=''))
```

Moreover it'll initialize more than one of them:

```
In [5]: o = OneThing(t1=Thing1(name='cy'), t2=Thing2(name='huh'))
In [6]: o
Out[6]: OneThing(t1=Thing1(name='cy'), t2=Thing2(name='huh'), t3=Thing3(name=''), t4=Thing4(name=''))
```

And that combination passes a round-trip through serialization and de-serialization:

```
In [7]: OneThing.FromString(bytes(o))
Out[7]: OneThing(t1=Thing1(name=''), t2=Thing2(name='huh'), t3=Thing3(name=''), t4=Thing4(name=''))
```

What's the intent as to where this *should* be enforced? Outside of betterproto?

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.