Documentation
¶
Overview ¶
Package retro provides retrocompatibility support for struct serialization across different versions. It allows structs to be marshaled and unmarshaled with version-specific field inclusion/exclusion based on semantic versioning constraints defined in struct tags.
The package supports multiple serialization formats (JSON, YAML, TOML) and enables backward compatibility by controlling which fields are included based on the struct's version field.
Example usage:
type MyStruct struct {
Version string `json:"version"`
Name string `json:"name" retro:">=v1.0.0"`
OldField string `json:"old_field" retro:"<v2.0.0"`
}
model := retro.Model[MyStruct]{
Struct: MyStruct{Version: "v1.5.0", Name: "test"},
}
data, _ := model.MarshalJSON()
See also:
- encoding/json for JSON marshaling
- gopkg.in/yaml.v3 for YAML marshaling
- github.com/pelletier/go-toml for TOML marshaling
Index ¶
- Variables
- type Format
- type Model
- func (m Model[T]) MarshalJSON() ([]byte, error)
- func (m Model[T]) MarshalTOML() ([]byte, error)
- func (m Model[T]) MarshalYAML() (interface{}, error)
- func (m *Model[T]) UnmarshalJSON(data []byte) error
- func (m *Model[T]) UnmarshalTOML(data interface{}) (err error)
- func (m *Model[T]) UnmarshalYAML(unmarshal func(interface{}) error) error
Constants ¶
This section is empty.
Variables ¶
var SupportedFormats = []Format{FormatJSON, FormatYAML, FormatTOML}
SupportedFormats is the list of all serialization formats supported by this package.
Functions ¶
This section is empty.
Types ¶
type Format ¶
type Format string
Format represents a serialization format supported by the retro package.
const ( // FormatJSON represents JSON serialization format. // Uses encoding/json for marshaling and unmarshaling. FormatJSON Format = "json" // FormatYAML represents YAML serialization format. // Uses gopkg.in/yaml.v3 for marshaling and unmarshaling. FormatYAML Format = "yaml" // FormatTOML represents TOML serialization format. // Uses github.com/pelletier/go-toml for marshaling and unmarshaling. FormatTOML Format = "toml" )
type Model ¶
Model is a generic wrapper that provides version-aware serialization for any struct type T. It supports both standard serialization (using default marshaling) and retrocompatibility mode where fields are included/excluded based on version constraints in struct tags.
Fields:
- Struct: The wrapped struct instance of type T
- Standard: If true, uses standard marshaling without version filtering. If false, applies version-based field filtering using "retro" tags.
The wrapped struct must have a "Version" field (string type) to enable version-based filtering. Version constraints in "retro" tags follow semantic versioning with operators:
- ">=v1.0.0" - Include field for versions >= v1.0.0
- "<v2.0.0" - Include field for versions < v2.0.0
- "v1.0.0-v2.0.0" - Include field for versions in range [v1.0.0, v2.0.0]
Example:
type Config struct {
Version string `json:"version"`
NewField string `json:"new_field" retro:">=v2.0.0"`
OldField string `json:"old_field" retro:"<v2.0.0"`
}
model := Model[Config]{
Struct: Config{Version: "v1.5.0", OldField: "value"},
Standard: false,
}
data, _ := model.MarshalJSON() // Only includes OldField, not NewField
func (Model[T]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface for Model. It serializes the wrapped struct to JSON format with version-aware field filtering.
If Standard is false, only fields matching the version constraints in "retro" tags are included in the output. The "Version" field in the struct determines which fields are included.
Returns:
- []byte: JSON-encoded data
- error: Error if marshaling fails
Example:
model := Model[MyStruct]{Struct: MyStruct{Version: "v1.0.0"}}
data, err := json.Marshal(model) // Uses MarshalJSON
See also: encoding/json.Marshaler
func (Model[T]) MarshalTOML ¶
MarshalTOML implements the toml.Marshaler interface for Model. It serializes the wrapped struct to TOML format with version-aware field filtering.
If Standard is false, only fields matching version constraints in "retro" tags are included in the output.
Returns:
- []byte: TOML-encoded data
- error: Error if marshaling fails
Example:
model := Model[MyStruct]{Struct: MyStruct{Version: "v1.0.0"}}
data, err := toml.Marshal(model) // Uses MarshalTOML
See also: github.com/pelletier/go-toml.Marshaler
func (Model[T]) MarshalYAML ¶
MarshalYAML implements the yaml.Marshaler interface for Model. It serializes the wrapped struct to YAML format with version-aware field filtering.
The method returns an interface{} that can be marshaled by the YAML encoder. If Standard is false, only fields matching version constraints are included.
Returns:
- interface{}: YAML-compatible data structure
- error: Error if marshaling fails
Example:
model := Model[MyStruct]{Struct: MyStruct{Version: "v1.0.0"}}
data, err := yaml.Marshal(model) // Uses MarshalYAML
See also: gopkg.in/yaml.v3.Marshaler
func (*Model[T]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface for Model. It deserializes JSON data into the wrapped struct with version-aware field filtering.
If Standard is false, only fields matching the version constraints in "retro" tags are populated. The version is extracted from the JSON data.
Parameters:
- data: JSON-encoded data to unmarshal
Returns:
- error: Error if unmarshaling fails or data is invalid
Example:
var model Model[MyStruct] err := json.Unmarshal(data, &model) // Uses UnmarshalJSON
See also: encoding/json.Unmarshaler
func (*Model[T]) UnmarshalTOML ¶
UnmarshalTOML implements the toml.Unmarshaler interface for Model. It deserializes TOML data into the wrapped struct with version-aware field filtering.
The method expects data to be a map[string]interface{} as provided by the TOML decoder. If Standard is false, only fields matching version constraints are populated.
Parameters:
- data: TOML data as map[string]interface{}
Returns:
- error: Error if unmarshaling fails, data is invalid, or type assertion fails
Example:
var model Model[MyStruct] err := toml.Unmarshal(data, &model) // Uses UnmarshalTOML
See also: github.com/pelletier/go-toml.Unmarshaler
func (*Model[T]) UnmarshalYAML ¶
UnmarshalYAML implements the yaml.Unmarshaler interface for Model. It deserializes YAML data into the wrapped struct with version-aware field filtering.
The method receives an unmarshal function from the YAML decoder and uses it to extract the data before applying version filtering.
Parameters:
- unmarshal: Function provided by YAML decoder to unmarshal data
Returns:
- error: Error if unmarshaling fails or data is invalid
Example:
var model Model[MyStruct] err := yaml.Unmarshal(data, &model) // Uses UnmarshalYAML
See also: gopkg.in/yaml.v3.Unmarshaler