Skip to content

Commit

Permalink
fix marshaler/unmarshaler
Browse files Browse the repository at this point in the history
  • Loading branch information
mkideal committed Aug 12, 2024
1 parent b5cd537 commit 57c619e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 22 deletions.
20 changes: 20 additions & 0 deletions component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,26 @@ func (r *Reference[T]) UnmarshalJSON(data []byte) error {
return r.validate()
}

// MarshalTOML marshals the referenced component UUID to TOML.
func (r Reference[T]) MarshalTOML() ([]byte, error) {
return r.Marshal()
}

// UnmarshalTOML unmarshals the referenced component UUID from TOML.
func (r *Reference[T]) UnmarshalTOML(data []byte) error {
return r.Unmarshal(data)
}

// MarshalYAML marshals the referenced component UUID to YAML.
func (r Reference[T]) MarshalYAML() ([]byte, error) {
return r.Marshal()
}

// UnmarshalYAML unmarshals the referenced component UUID from YAML.
func (r *Reference[T]) UnmarshalYAML(data []byte) error {
return r.Unmarshal(data)
}

// Marshal marshals the referenced component UUID to quoted bytes.
func (r Reference[T]) Marshal() ([]byte, error) {
var buf = make([]byte, 0, len(r.uuid)+len(`""`))
Expand Down
32 changes: 26 additions & 6 deletions encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,34 @@ type Encoder func(any) ([]byte, error)
// Decoder is a function type that decodes bytes into a provided value.
type Decoder func([]byte, any) error

// Marshaler is an interface for types that can marshal themselves into bytes.
type Marshaler interface {
Marshal() ([]byte, error)
// JSONMarshaler is an interface for types that can marshal themselves to JSON.
type JSONMarshaler interface {
MarshalJSON() ([]byte, error)
}

// Unmarshaler is an interface for types that can unmarshal bytes into themselves.
type Unmarshaler interface {
Unmarshal([]byte) error
// JSONUnmarshaler is an interface for types that can unmarshal themselves from JSON.
type JSONUnmarshaler interface {
UnmarshalJSON([]byte) error
}

// TOMLMarshaler is an interface for types that can marshal themselves to TOML.
type TOMLMarshaler interface {
MarshalTOML() ([]byte, error)
}

// TOMLUnmarshaler is an interface for types that can unmarshal themselves from TOML.
type TOMLUnmarshaler interface {
UnmarshalTOML([]byte) error
}

// YAMLMarshaler is an interface for types that can marshal themselves to YAML.
type YAMLMarshaler interface {
MarshalYAML() ([]byte, error)
}

// YAMLUnmarshaler is an interface for types that can unmarshal themselves from YAML.
type YAMLUnmarshaler interface {
UnmarshalYAML([]byte) error
}

// UnmarshalString decodes a string from byte slice data.
Expand Down
46 changes: 30 additions & 16 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ func (o *RawObject) SetBytes(b []byte) {
*o = RawObject(b)
}

// Marshal implements the encoding.Marshaler interface.
func (o RawObject) Marshal() ([]byte, error) {
func (o RawObject) marshal(nilValue []byte) ([]byte, error) {
if o == nil {
return nilValue, nil
}
return o, nil
}

// Unmarshal implements the encoding.RawUnmarshaler interface.
func (o *RawObject) Unmarshal(data []byte) error {
func (o *RawObject) unmarshal(name string, data []byte) error {
if o == nil {
return errors.New("types.RawObject: UnmarshalRaw on nil pointer")
return fmt.Errorf("types.RawObject: %s on nil pointer", name)
}
*o = append((*o)[0:0], data...)
return nil
Expand All @@ -60,20 +61,33 @@ func (o *RawObject) Unmarshal(data []byte) error {
// MarshalJSON implements the json.Marshaler interface.
// It returns the raw JSON encoding of the Object.
func (o RawObject) MarshalJSON() ([]byte, error) {
if o == nil {
return []byte("null"), nil
}
return o, nil
return o.marshal([]byte("null"))
}

// UnmarshalJSON implements the json.Unmarshaler interface.
// It sets the Object's data to a copy of the input JSON data.
func (o *RawObject) UnmarshalJSON(data []byte) error {
if o == nil {
return errors.New("types.RawObject: UnmarshalJSON on nil pointer")
}
*o = append((*o)[0:0], data...)
return nil
return o.unmarshal("UnmarshalJSON", data)
}

// MarshalTOML implements the encoding.TOMLMarshaler interface.
func (o RawObject) MarshalTOML() ([]byte, error) {
return o.marshal([]byte("{}"))
}

// UnmarshalTOML implements the encoding.TOMLUnmarshaler interface.
func (o *RawObject) UnmarshalTOML(data []byte) error {
return o.unmarshal("UnmarshalTOML", data)
}

// MarshalYAML implements the encoding.YAMLMarshaler interface.
func (o RawObject) MarshalYAML() ([]byte, error) {
return o.marshal([]byte("null"))
}

// UnmarshalYAML implements the encoding.YAMLUnmarshaler interface.
func (o *RawObject) UnmarshalYAML(data []byte) error {
return o.unmarshal("UnmarshalYAML", data)
}

// MarshalText implements the encoding.TextMarshaler interface.
Expand All @@ -100,12 +114,12 @@ func (o *RawObject) UnmarshalText(data []byte) error {

// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (o RawObject) MarshalBinary() ([]byte, error) {
return o.Marshal()
return o.marshal(nil)
}

// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (o *RawObject) UnmarshalBinary(data []byte) error {
return o.Unmarshal(data)
return o.unmarshal("UnmarshalBinary", data)
}

// Decode decodes the Object's data using the provided decoder.
Expand Down

0 comments on commit 57c619e

Please sign in to comment.