ProtoBuf#

Example Output#

personinfo.schema.proto

Overview#

Protocol Buffers is a protocol for seriaizing data developed by Google.

Protobuf can be generated from a LinkML schema.

To run:

gen-proto personinfo.yaml > personinfo.proto

Inheritance#

Because Protobuf does not support inheritance hierarchy, slots are “rolled down” from parent.

For example, in the personinfo schema, slots such as id and name are inherited from NamedThing, and aliases are inherited from a mixin:

NamedThing:
  slots:
    - id
    - name

HasAliases:
  mixin: true
  attributes:
    aliases:
      multivalued: true

Person:
  is_a: NamedThing
  mixins:
    - HasAliases
  slots:
    - birth_date
    - age_in_years
    - gender

(some parts truncated for brevity)

This would generate the following Protobuf:

// A generic grouping for any identifiable entity
message NamedThing
 {
  id String = 1
  optional name String = 2
  optional description String = 3
  optional image String = 4
 }

// A person (alive, dead, undead, or fictional).
message Person
 {
  id String = 1
  optional name String = 2
  optional description String = 3
  optional image String = 4
  optional primaryEmail String = 5
  optional birthDate String = 6
  optional ageInYears Integer = 7
  optional gender GenderType = 8
  optional currentAddress Address = 9
  repeated hasEmploymentHistory EmploymentEvent = 10
  repeated hasFamilialRelationships FamilialRelationship = 11
  repeated hasMedicalHistory MedicalEvent = 12
  repeated aliases String = 13
 }

Docs#

Command Line#

gen-proto#

Generate proto representation of LinkML model

gen-proto [OPTIONS] YAMLFILE

Options

-V, --version#

Show the version and exit.

-f, --format <format>#

Output format

Default:

proto

Options:

proto

--metadata, --no-metadata#

Include metadata in output

Default:

True

--useuris, --metauris#

Use class and slot URIs over model uris

Default:

True

-im, --importmap <importmap>#

Import mapping file

--log_level <log_level>#

Logging level

Default:

WARNING

Options:

CRITICAL | ERROR | WARNING | INFO | DEBUG

-v, --verbose#

Verbosity. Takes precedence over –log_level.

--mergeimports, --no-mergeimports#

Merge imports into source file (default=mergeimports)

--stacktrace, --no-stacktrace#

Print a stack trace when an error occurs

Default:

False

Arguments

YAMLFILE#

Required argument

Code#

class linkml.generators.protogen.ProtoGenerator(schema: str | ~typing.TextIO | ~linkml_runtime.linkml_model.meta.SchemaDefinition | ~linkml.utils.generator.Generator | ~pathlib.Path, schemaview: ~linkml_runtime.utils.schemaview.SchemaView | None = None, format: str | None = None, metadata: bool = True, useuris: bool | None = None, log_level: int | None = 30, mergeimports: bool | None = True, source_file_date: str | None = None, source_file_size: int | None = None, logger: ~logging.Logger | None = None, verbose: bool | None = None, output: str | None = None, namespaces: ~linkml_runtime.utils.namespaces.Namespaces | None = None, directory_output: bool = False, base_dir: str | None = None, metamodel_name_map: ~typing.Dict[str, str] | None = None, importmap: str | ~typing.Mapping[str, str] | None = None, emit_prefixes: ~typing.Set[str] = <factory>, metamodel: ~linkml.utils.schemaloader.SchemaLoader | None = None, stacktrace: bool = False, relative_slot_num: int = 0, **_kwargs)[source]#

A Generator for creating Protobuf schemas from a linkml schema.

serialize(**kwargs) str#

Generate output in the required format

Parameters:

kwargs – Generator specific parameters

Returns:

Generated output