Skip to content

zendesk/proto-field-extractors-go

Repository files navigation

What

Field extractors provides a mechanism to transparently extract arbitrarily nested fields from protobuf messages in Go, only requiring the path to the desired value.

It can be used for abstracting protobuf message traversal, and for creating arbitrary transformations on protobuf encoded data without unnecessary boilerplate.

Basic usage

Simple value extraction

message Account {
    google.protobuf.Int64Value   id = 1;
    google.protobuf.BoolValue    is_active = 2;
    google.protobuf.StringValue  name = 3;
    google.protobuf.StringValue  subdomain = 4;
    google.protobuf.StringValue  time_zone = 5;
    google.protobuf.Timestamp    created_at = 6;
    google.protobuf.Timestamp    updated_at = 7;
}
type Account struct {
  ID   int64
  Name string
}


account := &Account{}
accountID, _ := extractors.BasicField("id").Extract(accountMessage)
// an alias 'B' is provided for readability of basicField extractions
accountName, _ := extractors.B("name").Extract(accountMessage)
account.ID =  accountID.(int64)
account.Name = accountName.(string)

Exract a field from a nested oneof

message UserEvent {
  oneof event {
    UserCreated user_created = 1;
    UserDeactivatd user_deactivated = 2;
  }
}

message UserCreated {
  google.protobuf.StringValue id = 1;
}

message UserDeactivated {
 google.protobuf.Timestamp occurred_at = 1;
}
deactivatedAt, err := extractors.OneOf("event.user_deactivated", extractors.BasicField("occurred_at")).Extract(userEventMessage).(time.Time)

Installation

go get github.com/zendesk/field-extractors-go

Running tests

go test -v .

Updating example protobuf messages

Ensure the Go protobuf plugin is available in PATH. Instructions for installing the Go protobuf plugin.

Make any required changes to example_proto/example.proto

protoc --go_out=generated example_proto/example.proto

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages