move enc/decoders to separate pkg
This commit is contained in:
parent
6d816a17bc
commit
ab46f7d595
43
internal/codec/decoder.go
Normal file
43
internal/codec/decoder.go
Normal file
@ -0,0 +1,43 @@
|
||||
// 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() *Decoder {
|
||||
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
|
||||
}
|
80
internal/codec/decoder_test.go
Normal file
80
internal/codec/decoder_test.go
Normal file
@ -0,0 +1,80 @@
|
||||
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
||||
|
||||
package codec
|
||||
|
||||
import (
|
||||
_ "fmt"
|
||||
"github.com/stretchr/testify/assert"
|
||||
_ "log"
|
||||
"strings"
|
||||
"testing"
|
||||
"github.com/xeipuuv/gojsonschema"
|
||||
)
|
||||
|
||||
type TestUser struct {
|
||||
Name string `json:"name" yaml:"name"`
|
||||
Uid string `json:"uid" yaml:"uid"`
|
||||
Group string `json:"group" yaml:"group"`
|
||||
Home string `json:"home" yaml:"home"`
|
||||
State string `json:"state" yaml:"state"`
|
||||
}
|
||||
|
||||
func TestNewYAMLDecoder(t *testing.T) {
|
||||
e := NewYAMLDecoder(strings.NewReader(""))
|
||||
assert.NotNil(t, e)
|
||||
}
|
||||
|
||||
func TestNewDecoderDecodeJSON(t *testing.T) {
|
||||
schema:=`
|
||||
{
|
||||
"$id": "user",
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"title": "user",
|
||||
"type": "object",
|
||||
"required": [ "name" ],
|
||||
"properties": {
|
||||
"path": {
|
||||
"type": "string",
|
||||
"description": "user name",
|
||||
"minLength": 1
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
decl := `{
|
||||
"name": "testuser",
|
||||
"uid": "12001",
|
||||
"group": "12001",
|
||||
"home": "/home/testuser",
|
||||
"state": "present"
|
||||
}`
|
||||
|
||||
jsonReader := strings.NewReader(decl)
|
||||
user := &TestUser{}
|
||||
|
||||
e := NewJSONDecoder(jsonReader)
|
||||
assert.NotNil(t, e)
|
||||
docErr := e.Decode(user)
|
||||
assert.Nil(t, docErr)
|
||||
|
||||
schemaLoader := gojsonschema.NewStringLoader(schema)
|
||||
loader := gojsonschema.NewStringLoader(decl)
|
||||
result, validateErr := gojsonschema.Validate(schemaLoader, loader)
|
||||
assert.True(t, result.Valid())
|
||||
assert.Nil(t, validateErr)
|
||||
}
|
||||
|
||||
func TestNewJSONStringDecoder(t *testing.T) {
|
||||
decl := `{
|
||||
"name": "testuser",
|
||||
"uid": "12001",
|
||||
"group": "12001",
|
||||
"home": "/home/testuser",
|
||||
"state": "present"
|
||||
}`
|
||||
|
||||
e := NewJSONStringDecoder(decl)
|
||||
assert.NotNil(t, e)
|
||||
docErr := e.Decode(&TestUser{})
|
||||
assert.Nil(t, docErr)
|
||||
}
|
42
internal/codec/encoder.go
Normal file
42
internal/codec/encoder.go
Normal file
@ -0,0 +1,42 @@
|
||||
// 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() *Encoder {
|
||||
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
|
||||
}
|
58
internal/codec/encoder_test.go
Normal file
58
internal/codec/encoder_test.go
Normal file
@ -0,0 +1,58 @@
|
||||
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
||||
|
||||
package codec
|
||||
|
||||
import (
|
||||
_ "fmt"
|
||||
"github.com/stretchr/testify/assert"
|
||||
_ "log"
|
||||
"strings"
|
||||
"testing"
|
||||
"github.com/xeipuuv/gojsonschema"
|
||||
)
|
||||
|
||||
type TestFile struct {
|
||||
Path string `json:"path" yaml:"path"`
|
||||
}
|
||||
|
||||
func TestNewYAMLEncoder(t *testing.T) {
|
||||
var yamlDoc strings.Builder
|
||||
e := NewYAMLEncoder(&yamlDoc)
|
||||
assert.NotNil(t, e)
|
||||
}
|
||||
|
||||
func TestNewEncoderEncodeJSON(t *testing.T) {
|
||||
schema:=`
|
||||
{
|
||||
"$id": "file",
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"title": "file",
|
||||
"type": "object",
|
||||
"required": [ "path" ],
|
||||
"properties": {
|
||||
"path": {
|
||||
"type": "string",
|
||||
"description": "file path",
|
||||
"minLength": 1
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
var jsonDoc strings.Builder
|
||||
file := &TestFile{}
|
||||
file.Path = "foo"
|
||||
|
||||
e := NewJSONEncoder(&jsonDoc)
|
||||
assert.NotNil(t, e)
|
||||
docErr := e.Encode(file)
|
||||
assert.Nil(t, docErr)
|
||||
|
||||
schemaLoader := gojsonschema.NewStringLoader(schema)
|
||||
loader := gojsonschema.NewStringLoader(jsonDoc.String())
|
||||
result, err := gojsonschema.Validate(schemaLoader, loader)
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.True(t, result.Valid())
|
||||
}
|
Loading…
Reference in New Issue
Block a user