forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanifest_to_json.py
31 lines (24 loc) · 1.01 KB
/
manifest_to_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import json
import pathlib
import sys
from google.protobuf import json_format
from envoy.base.utils import ProtobufValidator
from tools.protodoc import manifest_pb2
from tools.protodoc.protodoc_manifest_untyped import data as protodoc_manifest_untyped
def main(descriptor, output):
# Load as YAML, emit as JSON and then parse as proto to provide type
# checking.
proto_set = ProtobufValidator(descriptor)
manifest = json_format.Parse(json.dumps(protodoc_manifest_untyped), manifest_pb2.Manifest())
result = {}
for field_name in manifest.fields:
field = manifest.fields[field_name]
example = json_format.MessageToDict(field.edge_config.example)
parts = field_name.split(".")
name = ".".join(parts[:-1])
part = parts[-1]
proto_set.validate_fragment({part: example}, name)
result[field_name] = dict(note=field.edge_config.note, example=example)
pathlib.Path(output).write_text(json.dumps(result))
if __name__ == "__main__":
main(*sys.argv[1:])