|
1 | 1 | package fantasy |
2 | 2 |
|
| 3 | +import "encoding/json" |
| 4 | + |
3 | 5 | // ProviderOptionsData is an interface for provider-specific options data. |
| 6 | +// All implementations MUST also implement encoding/json.Marshaler and |
| 7 | +// encoding/json.Unmarshaler interfaces to ensure proper JSON serialization |
| 8 | +// with the provider registry system. |
| 9 | +// |
| 10 | +// Recommended implementation pattern using generic helpers: |
| 11 | +// |
| 12 | +// // Define type constants at the top of your file |
| 13 | +// const TypeMyProviderOptions = "myprovider.options" |
| 14 | +// |
| 15 | +// type MyProviderOptions struct { |
| 16 | +// Field string `json:"field"` |
| 17 | +// } |
| 18 | +// |
| 19 | +// // Register the type in init() - place at top of file after constants |
| 20 | +// func init() { |
| 21 | +// fantasy.RegisterProviderType(TypeMyProviderOptions, func(data []byte) (fantasy.ProviderOptionsData, error) { |
| 22 | +// var opts MyProviderOptions |
| 23 | +// if err := json.Unmarshal(data, &opts); err != nil { |
| 24 | +// return nil, err |
| 25 | +// } |
| 26 | +// return &opts, nil |
| 27 | +// }) |
| 28 | +// } |
| 29 | +// |
| 30 | +// // Implement ProviderOptionsData interface |
| 31 | +// func (*MyProviderOptions) Options() {} |
| 32 | +// |
| 33 | +// // Implement json.Marshaler using the generic helper |
| 34 | +// func (m MyProviderOptions) MarshalJSON() ([]byte, error) { |
| 35 | +// type plain MyProviderOptions |
| 36 | +// return fantasy.MarshalProviderType(TypeMyProviderOptions, plain(m)) |
| 37 | +// } |
| 38 | +// |
| 39 | +// // Implement json.Unmarshaler using the generic helper |
| 40 | +// // Note: Receives inner data after type routing by the registry. |
| 41 | +// func (m *MyProviderOptions) UnmarshalJSON(data []byte) error { |
| 42 | +// type plain MyProviderOptions |
| 43 | +// var p plain |
| 44 | +// if err := fantasy.UnmarshalProviderType(data, &p); err != nil { |
| 45 | +// return err |
| 46 | +// } |
| 47 | +// *m = MyProviderOptions(p) |
| 48 | +// return nil |
| 49 | +// } |
4 | 50 | type ProviderOptionsData interface { |
| 51 | + // Options is a marker method that identifies types implementing this interface. |
5 | 52 | Options() |
| 53 | + json.Marshaler |
| 54 | + json.Unmarshaler |
6 | 55 | } |
7 | 56 |
|
8 | 57 | // ProviderMetadata represents additional provider-specific metadata. |
|
0 commit comments