subtypes

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: May 16, 2022 License: MPL-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package subtypes provides helpers to work with boundary resource subtypes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AttributeTransformerInterceptor added in v0.8.0

func AttributeTransformerInterceptor(_ context.Context) grpc.UnaryServerInterceptor

AttributeTransformerInterceptor is a grpc server interceptor that will transform subtype attributes for requests and responses. This will only modify requests and responses that adhere to a specific structure and is done to support the use of a oneof for attributes to strongly type the attributes while allowing the JSON API to provide attributes via a single key.

For example with a protobuf message definition like:

message Account {
  string id = 1;
  string type = 2;
  oneof attrs {
    google.protobuf.Struct attributes = 10 [(controller.custom_options.v1.subtype) = "default"];
    PasswordAttributes password_attributes = 20 [(controller.custom_options.v1.subtype) = "password"];
  }
}

message PasswordAttributes {
   string login_name = 1;
}

message AccountCreateRequest {
   Account item = 1;
}

message AccountCreateResponse {
   Account item = 1;
}

And a create request with JSON request body like:

{
   "type": "password",
   "attributes": {
      "login_name": "tim"
   }
}

Will result in a proto request like:

type:"password" attributes:{fields:{key:"login_name" value:{string_value:"tim"}}}

This request will be transformed into:

type:"password" password_attributes:{login_name:"tim"}

func Filterable added in v0.8.0

func Filterable(item proto.Message) (proto.Message, error)

Filterable converts a proto.Message so any subtype attributes fields are structed like the API so filter strings will be correctly applied. If the given proto.Message does not have any subtype attributes, the original proto.Message is returned. To determine if the message needs transformation, it looks for a oneof field named "attrs". It also expects that there is a structpb.Struct field named "attributes" as part of the oneof. Thus the message must be like:

message Foo {
  // other fields
  oneof attrs {
    google.protobuf.Struct attributes = 100;
    // other attribute fields
  }
}

If the message does not conform to this structure, the original message is returned.

func Prefixes added in v0.8.0

func Prefixes(domain string) []string

Prefixes returns the list of all known Prefixes for a domain.

func Register added in v0.8.0

func Register(domain string, subtype Subtype, prefixes ...string) error

Register registers all the prefixes for a provided Subtype. Register returns an error if the subtype has already been registered or if any of the prefixes are associated with another subtype.

func RegisterRequestTransformationFunc added in v0.8.0

func RegisterRequestTransformationFunc(msg proto.Message, transformFn TransformationFunc) error

RegisterRequestTransformationFunc registers a transformation function for the provided message. The message should be used as a request parameter to a service method. The provided callback is guaranteed to only be called with messages of the same type as the provided message.

func RegisterResponseTransformationFunc added in v0.8.0

func RegisterResponseTransformationFunc(msg proto.Message, transformFn TransformationFunc) error

RegisterResponseTransformationFunc registers a transformation function for the provided message. The message should be used as a response parameter to a service method. The provided callback is guaranteed to only be called with messages of the same type as the provided message.

Types

type Registry

type Registry struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Registry stores a collection of boundary resource subtypes along with their prefixes and allows for translating prefixes back to registered subtypes.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new boundary resource subtype registry.

func (*Registry) Prefixes added in v0.7.4

func (r *Registry) Prefixes() []string

Prefixes returns the list of all known Prefixes.

func (*Registry) Register

func (r *Registry) Register(subtype Subtype, prefixes ...string) error

Register registers all the prefixes for a provided Subtype. Register returns an error if the subtype has already been registered or if any of the prefixes are associated with another subtype.

func (*Registry) SubtypeFromId

func (r *Registry) SubtypeFromId(id string) Subtype

SubtypeFromId returns the Subtype from the provided id if the id's prefix was registered with a Subtype. Otherwise Unknown is returned.

func (*Registry) SubtypeFromType

func (r *Registry) SubtypeFromType(t string) Subtype

SubtypeFromType returns the Subtype from the provided string or if no Subtype was registered with that string Unknown is returned.

type Subtype

type Subtype string

Subtype variables identify a boundary resource subtype.

const (
	UnknownSubtype Subtype = "unknown"
)

func SubtypeFromId added in v0.8.0

func SubtypeFromId(domain, id string) Subtype

SubtypeFromId returns the Subtype from the provided id if the id's prefix was registered with a Subtype. Otherwise Unknown is returned.

func SubtypeFromType added in v0.8.0

func SubtypeFromType(domain, t string) Subtype

SubtypeFromType returns the Subtype from the provided string or if no Subtype was registered with that string Unknown is returned.

func (Subtype) String

func (t Subtype) String() string

String returns the string representation of a Subtype

type TransformationFunc added in v0.8.0

type TransformationFunc func(proto.Message) error

TransformationFunc defines the signature used to transform protobuf message attributes. The proto.Message is mutated in place.

Jump to

Keyboard shortcuts

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