52 lines
927 B
Go
52 lines
927 B
Go
// 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"
|
|
"strings"
|
|
)
|
|
|
|
//type JSONDecoder json.Decoder
|
|
|
|
type Decoder interface {
|
|
Decode(v any) error
|
|
}
|
|
|
|
func NewDecoder(r io.Reader, format Format) Decoder {
|
|
switch format {
|
|
case FormatYaml:
|
|
return NewYAMLDecoder(r)
|
|
case FormatJson:
|
|
return NewJSONDecoder(r)
|
|
case FormatProtoBuf:
|
|
return NewProtoBufDecoder(r)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func NewJSONDecoder(r io.Reader) Decoder {
|
|
return json.NewDecoder(r)
|
|
}
|
|
|
|
func NewJSONStringDecoder(s string) Decoder {
|
|
return json.NewDecoder(strings.NewReader(s))
|
|
}
|
|
|
|
func NewYAMLDecoder(r io.Reader) Decoder {
|
|
return yaml.NewDecoder(r)
|
|
}
|
|
|
|
func NewYAMLStringDecoder(s string) Decoder {
|
|
return yaml.NewDecoder(strings.NewReader(s))
|
|
}
|
|
|
|
func NewProtoBufDecoder(r io.Reader) Decoder {
|
|
return nil
|
|
}
|