2024-05-14 16:53:47 +00:00
|
|
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
|
|
|
|
|
|
|
package codec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
_ "fmt"
|
|
|
|
_ "github.com/xeipuuv/gojsonschema"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
"io"
|
|
|
|
_ "log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type JSONEncoder json.Encoder
|
|
|
|
|
|
|
|
type Encoder interface {
|
|
|
|
Encode(v any) error
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2024-07-01 07:16:55 +00:00
|
|
|
func NewEncoder(w io.Writer, format Format) Encoder {
|
|
|
|
switch format {
|
|
|
|
case FormatYaml:
|
|
|
|
return NewYAMLEncoder(w)
|
|
|
|
case FormatJson:
|
|
|
|
return NewJSONEncoder(w)
|
|
|
|
case FormatProtoBuf:
|
|
|
|
return NewProtoBufEncoder(w)
|
|
|
|
}
|
2024-05-14 16:53:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewJSONEncoder(w io.Writer) Encoder {
|
|
|
|
return (*JSONEncoder)(json.NewEncoder(w))
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewYAMLEncoder(w io.Writer) Encoder {
|
|
|
|
return yaml.NewEncoder(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProtoBufEncoder(w io.Writer) Encoder {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *JSONEncoder) Encode(v any) error {
|
|
|
|
return (*json.Encoder)(j).Encode(v)
|
|
|
|
}
|
|
|
|
func (j *JSONEncoder) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|