danielgtaylor / danielgtaylor/python-betterproto

OneOf Enforcement?

Aperta
#189 3 commenti 2 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Python
Stelle
1.8k
Fork
234
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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?

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.