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

-f, --format <format>

Output format

Default

proto

Options

proto

--metadata, --no-metadata

Include metadata in output

Default

True

--useuris, --metauris

Include metadata in output

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

--mergeimports, --no-mergeimports

Merge imports into source file (default=mergeimports)

Arguments

YAMLFILE

Required argument

Code

class linkml.generators.protogen.ProtoGenerator(schema: Union[str, TextIO, linkml_runtime.linkml_model.meta.SchemaDefinition], **kwargs)[source]

A Generator for creating Protobuf schemas from a linkml schema.

serialize(**kwargs) str

Generate output in the required format

Parameters

kwargs – Generater specific parameters

Returns

Generated output