Documentation
¶
Overview ¶
Package subtypes provides helpers to work with boundary resource subtypes.
Index ¶
- func AttributeTransformerInterceptor(_ context.Context) grpc.UnaryServerInterceptor
- func Filterable(item proto.Message) (proto.Message, error)
- func Prefixes(domain string) []string
- func Register(domain string, subtype Subtype, prefixes ...string) error
- func RegisterRequestTransformationFunc(msg proto.Message, transformFn TransformationFunc) error
- func RegisterResponseTransformationFunc(msg proto.Message, transformFn TransformationFunc) error
- type Registry
- type Subtype
- type TransformationFunc
- type UnknownSubtypeIDError
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
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 Register ¶ added in v0.8.0
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 ¶
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) Register ¶
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 ¶
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 ¶
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
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
SubtypeFromType returns the Subtype from the provided string or if no Subtype was registered with that string Unknown is returned.
type TransformationFunc ¶ added in v0.8.0
TransformationFunc defines the signature used to transform protobuf message attributes. The proto.Message is mutated in place.
type UnknownSubtypeIDError ¶ added in v0.9.0
type UnknownSubtypeIDError struct { // ID is the resource identifier ID string }
UnknownSubtypeIDError is an error type that describes an invalid resource sub-type identifer. For example, this authentication sub-type ID "ampwd_1234567890" is an error because the prefix "ampwd" is invalid.
func (*UnknownSubtypeIDError) Error ¶ added in v0.9.0
func (e *UnknownSubtypeIDError) Error() string
Error returns a string describing an unknown subtype based on a given resource ID Example: "unknown subtype in ID: ampwd_1234567890"