Skip to content

Commit

Permalink
add metadata.json functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
amwat committed Aug 7, 2021
1 parent a657e25 commit ac36e32
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
61 changes: 61 additions & 0 deletions pkg/metadata/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package metadata

import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
)

type CustomJSON struct {
data map[string]string
}

func NewCustomJSON(from io.Reader) (*CustomJSON, error) {
meta := &CustomJSON{}
if from != nil {
dataBytes, err := ioutil.ReadAll(from)
if err != nil {
return nil, err
}
if err := json.Unmarshal(dataBytes, &meta.data); err != nil {
return nil, err
}
}
return meta, nil
}

func (m *CustomJSON) Add(key, value string) error {
if m.data == nil {
m.data = map[string]string{}
}
if _, exists := m.data[key]; exists {
return fmt.Errorf("key %s already exists in the metadata", key)
}
m.data[key] = value
return nil
}

func (m *CustomJSON) Write(writer io.Writer) error {
data, err := json.Marshal(m.data)
if err == nil {
_, err = writer.Write(data)
}
return err
}
65 changes: 65 additions & 0 deletions pkg/metadata/metadata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package metadata

import (
"bytes"
"encoding/json"
"reflect"
"strings"
"testing"
)

func TestCustomJSON_AddWrite(t *testing.T) {
meta := CustomJSON{}
if err := meta.Add("foo", "bar"); err != nil {
t.Errorf("did not expect an error, but got: %v", err)
}
if err := meta.Add("baz", "qwe"); err != nil {
t.Errorf("did not expect an error, but got: %v", err)
}

var buff bytes.Buffer
if err := meta.Write(&buff); err != nil {
t.Errorf("did not expect an error, but got: %v", err)
}

actualBytes := buff.Bytes()

expectedBytes, err := json.Marshal(meta.data)
if err != nil {
t.Errorf("did not expect an error, but got: %v", err)
}
if !bytes.Equal(buff.Bytes(), expectedBytes) {
t.Errorf("mismatched metadata bytes, got: %v, want: %v", actualBytes, expectedBytes)
}
}

func TestNewCustomJSON(t *testing.T) {
json := `{"baz":"qwe","foo":"bar"}`
meta, err := NewCustomJSON(strings.NewReader(json))
if err != nil {
t.Errorf("did not expect an error, but got: %v", err)
}
expectedData := map[string]string{
"foo": "bar",
"baz": "qwe",
}
if !reflect.DeepEqual(meta.data, expectedData) {
t.Errorf("mismatched metadata bytes, got: %v, want: %v", meta.data, expectedData)
}
}

0 comments on commit ac36e32

Please sign in to comment.