Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion datacontract/export/protobuf_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ def _get_logical_type_option(prop: SchemaProperty, key: str):
return prop.logicalTypeOptions.get(key)


def _get_proto_package_name(data_contract: OpenDataContractStandard) -> str:
"""
Returns the Protobuf package name from the contract's description customProperties
("proto_package_name"), falling back to "example".
"""
description = data_contract.description
if description is None or description.customProperties is None:
return "example"
for cp in description.customProperties:
if cp.property == "proto_package_name" and cp.value:
return cp.value
return "example"


def to_protobuf(data_contract: OpenDataContractStandard) -> str:
"""
Generates a Protobuf file from the data contract specification.
Expand Down Expand Up @@ -58,7 +72,7 @@ def to_protobuf(data_contract: OpenDataContractStandard) -> str:

# Build header with syntax and package declarations.
header = 'syntax = "proto3";\n\n'
package = "example" # Default package, can be customized
package: str = _get_proto_package_name(data_contract)
header += f"package {package};\n\n"

# Append enum definitions before messages.
Expand Down
6 changes: 5 additions & 1 deletion tests/test_export_protobuf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def test_to_protobuf():
kind: DataContract
apiVersion: v3.1.0
id: test_protobuf
description:
customProperties:
- property: proto_package_name
value: com.example.product
Comment thread
Schokuroff marked this conversation as resolved.
Outdated
schema:
- name: Product
description: Details of Product.
Expand Down Expand Up @@ -83,7 +87,7 @@ def test_to_protobuf():
expected_protobuf = """
syntax = "proto3";

package example;
package com.example.product;

// Enum for Category
enum Category {
Expand Down
Loading