Construct universal modules with decoded options
This commit is contained in:
@@ -31,10 +31,15 @@ var providedCapabilities = []string{
|
||||
|
||||
var _ contracts.InputAdapter = (*Adapter)(nil)
|
||||
|
||||
type Adapter struct{}
|
||||
type Options struct{}
|
||||
|
||||
type Adapter struct {
|
||||
options Options
|
||||
}
|
||||
|
||||
func New() *Adapter {
|
||||
return &Adapter{}
|
||||
options, _ := DecodeOptions(nil)
|
||||
return &Adapter{options: options}
|
||||
}
|
||||
|
||||
func (a *Adapter) Key() string {
|
||||
@@ -96,11 +101,27 @@ func ModuleSpec() pipeline.ModuleSpec {
|
||||
}
|
||||
|
||||
func Register(registry *pipeline.InputAdapterRegistry) error {
|
||||
return registry.RegisterWithSpec(ModuleSpec(), func() (contracts.InputAdapter, error) {
|
||||
return New(), nil
|
||||
return registry.RegisterBuilderWithSpec(ModuleSpec(), validateOptions, func(request pipeline.BuildRequest) (contracts.InputAdapter, error) {
|
||||
options, err := DecodeOptions(request.Options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Adapter{options: options}, nil
|
||||
})
|
||||
}
|
||||
|
||||
func validateOptions(options map[string]any) error {
|
||||
_, err := DecodeOptions(options)
|
||||
return err
|
||||
}
|
||||
|
||||
func DecodeOptions(options map[string]any) (Options, error) {
|
||||
if err := pipeline.RejectUnknownOptions(options); err != nil {
|
||||
return Options{}, inputErrorf("%w", err)
|
||||
}
|
||||
return Options{}, nil
|
||||
}
|
||||
|
||||
func sourceUnit(sourceID string, segment segment, index int, seen map[int]struct{}) (source.SourceUnit, error) {
|
||||
segmentLabel := fmt.Sprintf("segment[%d]", index)
|
||||
if segment.ID <= 0 {
|
||||
|
||||
@@ -56,6 +56,9 @@ func TestRegisterMakesAdapterBuildable(t *testing.T) {
|
||||
if adapter.Key() != Key {
|
||||
t.Fatalf("adapter.Key() = %q, want %q", adapter.Key(), Key)
|
||||
}
|
||||
if err := registry.ValidateOptions(Key, map[string]any{"unexpected": true}); err == nil || !strings.Contains(err.Error(), "unknown option") {
|
||||
t.Fatalf("ValidateOptions() error = %v, want unknown option error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterStoresModuleSpec(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user