danielgtaylor / danielgtaylor/python-betterproto

Cast "oneOf" value to a single value when converting to dict/JSON

オープン
#388 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
Python
スター
1.8k
フォーク
234
PR マージ指標
30日以内にマージされた PR はありません

説明

I am currently consuming metrics exposed by an OpenTelemetry agent, but when I try to convert the Protobuf metrics to JSON, the "oneOf" occurences are being translated to a dict instead of a single value. Is there any way to change this behavior to only display a single string value?

An example of what is happening:

The proto definitions (the original *.proto* files can be found [here](https://github.com/open-telemetry/opentelemetry-proto/blob/main/opentelemetry/proto/common/v1/common.proto)):
```proto
syntax = "proto3";

message AnyValue {
oneof value {
string string_value = 1;
bool bool_value = 2;
int64 int_value = 3;
double double_value = 4;
ArrayValue array_value = 5;
KeyValueList kvlist_value = 6;
bytes bytes_value = 7;
}
}

message ArrayValue {
repeated AnyValue values = 1;
}

message KeyValueList {
repeated KeyValue values = 1;
}

message KeyValue {
string key = 1;
AnyValue value = 2;
}
```

The dataclasses generated:
```python
@dataclass
class KeyValue(betterproto.Message):
key: str = betterproto.string_field(1)
value: "AnyValue" = betterproto.message_field(2)

@dataclass
class AnyValue(betterproto.Message):
string_value: str = betterproto.string_field(1, group="value")
bool_value: bool = betterproto.bool_field(2, group="value")
int_value: int = betterproto.int64_field(3, group="value")
double_value: float = betterproto.double_field(4, group="value")
array_value: "ArrayValue" = betterproto.message_field(5, group="value")
kvlist_value: "KeyValueList" = betterproto.message_field(6, group="value")
bytes_value: bytes = betterproto.bytes_field(7, group="value")
```

The current conversion:
```json
{"key": "process.pid", "value": {"intValue": "275"}}
```

What I would like to have:
```json
{"key": "process.pid", "value": "275"}
```

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

リポジトリのファイルもテストも指定されていません。まず、生成された AnyValue クラスと KeyValue クラスの dict/JSON 変換を再現し、次に oneof グループがどのようにシリアライズされるかを追跡します。完了の条件は、スカラーの oneof 値が要求された単一の値を生成し、同時に message、array、map、bytes の各バリアントが適切に扱われることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
tooling
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。