generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor type registry (#1403)
- separate go-runtime/typeregistry and internal schema/typeregistry; add schema type lookups for ingress encoding - fixes for deriving schema types for sum type variants
- Loading branch information
Showing
13 changed files
with
788 additions
and
298 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package schema | ||
|
||
import ( | ||
"context" | ||
schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema" | ||
) | ||
|
||
type contextKeyTypeRegistry struct{} | ||
|
||
// ContextWithTypeRegistry adds a type registry to the given context. | ||
func ContextWithTypeRegistry(ctx context.Context, r *schemapb.TypeRegistry) context.Context { | ||
return context.WithValue(ctx, contextKeyTypeRegistry{}, r) | ||
} | ||
|
||
// TypeRegistry is a registry of types that can be resolved to a schema type at runtime. | ||
// It also records sum types and their variants, for use in encoding and decoding. | ||
type TypeRegistry struct { | ||
// SchemaTypes associates a type name with a schema type. | ||
SchemaTypes map[string]Type `protobuf:"1"` | ||
// SumTypes associates a sum type discriminator type name with its variant type names. | ||
SumTypes map[string]SumTypeVariants `protobuf:"2"` | ||
} | ||
|
||
// NewTypeRegistry creates a new type registry. | ||
// The type registry is used to instantiate types by their qualified name at runtime. | ||
func NewTypeRegistry() *TypeRegistry { | ||
return &TypeRegistry{ | ||
SchemaTypes: make(map[string]Type), | ||
SumTypes: make(map[string]SumTypeVariants), | ||
} | ||
} | ||
|
||
// RegisterSumType registers a Go sum type with the type registry. Sum types are represented as enums in the | ||
// FTL schema. | ||
func (t *TypeRegistry) RegisterSumType(discriminator string, variants map[string]Type) { | ||
var values []string | ||
for name, vt := range variants { | ||
values = append(values, name) | ||
t.SchemaTypes[name] = vt | ||
} | ||
t.SumTypes[discriminator] = SumTypeVariants{Value: values} | ||
} | ||
|
||
func (t *TypeRegistry) ToProto() *schemapb.TypeRegistry { | ||
typespb := make(map[string]*schemapb.Type, len(t.SchemaTypes)) | ||
for k, v := range t.SchemaTypes { | ||
typespb[k] = typeToProto(v) | ||
} | ||
|
||
return &schemapb.TypeRegistry{ | ||
SumTypes: sumTypeVariantsToProto(t.SumTypes), | ||
SchemaTypes: typespb, | ||
} | ||
} | ||
|
||
type SumTypeVariants struct { | ||
// Value is a list of variant names for the sum type. | ||
Value []string `protobuf:"1"` | ||
} | ||
|
||
func (s *SumTypeVariants) ToProto() *schemapb.SumTypeVariants { | ||
return &schemapb.SumTypeVariants{Value: s.Value} | ||
} | ||
|
||
func sumTypeVariantsToProto(v map[string]SumTypeVariants) map[string]*schemapb.SumTypeVariants { | ||
out := make(map[string]*schemapb.SumTypeVariants, len(v)) | ||
for k, v := range v { | ||
out[k] = v.ToProto() | ||
} | ||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.