From ab46f7d595e8b66e60ea8d9c1690a7cf3a291a4a Mon Sep 17 00:00:00 2001 From: Matthew Rich Date: Tue, 14 May 2024 09:53:47 -0700 Subject: [PATCH] move enc/decoders to separate pkg --- internal/codec/decoder.go | 43 ++++++++++++++++++ internal/codec/decoder_test.go | 80 ++++++++++++++++++++++++++++++++++ internal/codec/encoder.go | 42 ++++++++++++++++++ internal/codec/encoder_test.go | 58 ++++++++++++++++++++++++ 4 files changed, 223 insertions(+) create mode 100644 internal/codec/decoder.go create mode 100644 internal/codec/decoder_test.go create mode 100644 internal/codec/encoder.go create mode 100644 internal/codec/encoder_test.go diff --git a/internal/codec/decoder.go b/internal/codec/decoder.go new file mode 100644 index 0000000..3b375e4 --- /dev/null +++ b/internal/codec/decoder.go @@ -0,0 +1,43 @@ +// Copyright 2024 Matthew Rich . 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 +} diff --git a/internal/codec/decoder_test.go b/internal/codec/decoder_test.go new file mode 100644 index 0000000..7533659 --- /dev/null +++ b/internal/codec/decoder_test.go @@ -0,0 +1,80 @@ +// Copyright 2024 Matthew Rich . 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) +} diff --git a/internal/codec/encoder.go b/internal/codec/encoder.go new file mode 100644 index 0000000..da2fbc6 --- /dev/null +++ b/internal/codec/encoder.go @@ -0,0 +1,42 @@ +// Copyright 2024 Matthew Rich . 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 +} diff --git a/internal/codec/encoder_test.go b/internal/codec/encoder_test.go new file mode 100644 index 0000000..4367db7 --- /dev/null +++ b/internal/codec/encoder_test.go @@ -0,0 +1,58 @@ +// Copyright 2024 Matthew Rich . 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()) +}