fieldextractors

package module
v0.0.0-...-1258b4e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 15, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type B

type B = BasicField

type BasicField

type BasicField string

func (BasicField) Extract

func (f BasicField) Extract(in proto.Message) (interface{}, error)

func (BasicField) ExtractPath

func (f BasicField) ExtractPath(in proto.Message) (interface{}, error)

type ExtractorWithDefault

type ExtractorWithDefault struct {
	DefaultValue interface{}
	Extractor    FieldExtractor
}

func (ExtractorWithDefault) Extract

func (f ExtractorWithDefault) Extract(in proto.Message) (interface{}, error)

type FieldExtractor

type FieldExtractor interface {
	Extract(in proto.Message) (interface{}, error)
}

type Object

type Object = ObjectExtractor

type ObjectExtractor

type ObjectExtractor map[string]FieldExtractor

func (ObjectExtractor) Extract

func (f ObjectExtractor) Extract(in proto.Message) (interface{}, error)

type OneOfExtractor

type OneOfExtractor struct {
	// contains filtered or unexported fields
}

func OneOf

func OneOf(oneOfPath string, itemExtractor FieldExtractor) OneOfExtractor

func (OneOfExtractor) Extract

func (f OneOfExtractor) Extract(in proto.Message) (interface{}, error)

type RepeatedItem

type RepeatedItem struct {
	RepeatedFieldPath string
	ItemExtractor     FieldExtractor
}

func (RepeatedItem) Extract

func (f RepeatedItem) Extract(in proto.Message) (interface{}, error)

type StructField

type StructField string

func (StructField) Extract

func (sf StructField) Extract(in proto.Message) (interface{}, error)

type Template

type Template struct {
	// contains filtered or unexported fields
}

func Tmpl

func Tmpl(format string, args ...FieldExtractor) Template

func (Template) Extract

func (f Template) Extract(in proto.Message) (interface{}, error)

type V

type V = Value

type Value

type Value string

func (Value) Extract

func (v Value) Extract(in proto.Message) (interface{}, error)

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL