jx/internal/codec/encoder.go
Matthew Rich 52c083a3d9
Some checks failed
Lint / golangci-lint (push) Failing after 9m44s
Declarative Tests / test (push) Failing after 14s
Declarative Tests / build-fedora (push) Failing after 2m50s
Declarative Tests / build-ubuntu-focal (push) Failing after 1m3s
add source for containers, packages
2024-07-01 00:16:55 -07:00

51 lines
891 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"
)
type JSONEncoder json.Encoder
type Encoder interface {
Encode(v any) error
Close() error
}
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)
}
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
}