When building a custom provider, you must implement the ProviderOptionsData interface to handle provider-specific configuration. This interface requires implementing Options(), json.Marshaler, and json.Unmarshaler.
To ensure proper serialization within the provider registry, use the recommended pattern of defining a type constant and registering the type in an init() function using fantasy.RegisterProviderType. Use the provided generic helpers fantasy.MarshalProviderType and fantasy.UnmarshalProviderType inside your JSON methods to handle the type-routing logic.
// 1. Define type constant
const TypeMyProviderOptions = "myprovider.options"
type MyProviderOptions struct {
Field string `json:"field"`
}
// 2. Register in init()
func init() {
fantasy.RegisterProviderType(TypeMyProviderOptions, func(data []byte) (fantasy.ProviderOptionsData, error) {
var opts MyProviderOptions
if err := json.Unmarshal(data, &opts); err != nil {
return nil, err
}
return &opts, nil
})
}
// 3. Implement interface methods
func (*MyProviderOptions) Options() {}
func (m MyProviderOptions) MarshalJSON() ([]byte, error) {
type plain MyProviderOptions
return fantasy.MarshalProviderType(TypeMyProviderOptions, plain(m))
}
func (m *MyProviderOptions) UnmarshalJSON(data []byte) error {
type plain MyProviderOptions
var p plain
if err := fantasy.UnmarshalProviderType(data, &p); err != nil {
return err
}
*m = MyProviderOptions(p)
return nil
}