Skip to content

Commit c999bfd

Browse files
feat: make structs serializable (#34)
1 parent 0b5edf4 commit c999bfd

File tree

13 files changed

+2836
-11
lines changed

13 files changed

+2836
-11
lines changed

content.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,57 @@
11
package fantasy
22

3+
import "encoding/json"
4+
35
// 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+
// }
450
type ProviderOptionsData interface {
51+
// Options is a marker method that identifies types implementing this interface.
552
Options()
53+
json.Marshaler
54+
json.Unmarshaler
655
}
756

857
// ProviderMetadata represents additional provider-specific metadata.

0 commit comments

Comments
 (0)