Skip to content

Commit

Permalink
gm: export JSONVerifier and make it handle []byte
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Shvedunov committed Apr 17, 2018
1 parent 318faff commit 9e01a48
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions tests/gm/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,21 @@ func (v textVerifier) Marshal() ([]byte, error) {
return []byte(v), nil
}

type jsonVerifier struct {
type JSONVerifier struct {
data interface{}
}

var _ Verifier = jsonVerifier{}
var _ Verifier = JSONVerifier{}

func (v jsonVerifier) Suffix() string {
func NewJSONVerifier(data interface{}) JSONVerifier {
return JSONVerifier{data}
}

func (v JSONVerifier) Suffix() string {
return ".json"
}

func (v jsonVerifier) Verify(content []byte) (bool, error) {
func (v JSONVerifier) Verify(content []byte) (bool, error) {
var curData interface{}
if err := json.Unmarshal(content, &curData); err != nil {
glog.Warningf("Failed to unmarshal to JSON: %v:\n%s", err, content)
Expand All @@ -98,13 +102,20 @@ func (v jsonVerifier) Verify(content []byte) (bool, error) {
return reflect.DeepEqual(curData, newData), nil
}

func (v jsonVerifier) Marshal() ([]byte, error) {
out, err := json.MarshalIndent(v.data, "", jsonDataIndent)
if err != nil {
return nil, fmt.Errorf("failed to marshal json data: %v. Input:\n%s",
err, spew.Sdump(v.data))
func (v JSONVerifier) Marshal() ([]byte, error) {
switch d := v.data.(type) {
case []byte:
return d, nil
case string:
return []byte(d), nil
default:
out, err := json.MarshalIndent(v.data, "", jsonDataIndent)
if err != nil {
return nil, fmt.Errorf("failed to marshal json data: %v. Input:\n%s",
err, spew.Sdump(v.data))
}
return out, nil
}
return out, nil
}

// YamlVerifier verifies the data using YAML representation.
Expand Down Expand Up @@ -172,7 +183,7 @@ func getVerifier(data interface{}) Verifier {
case []byte:
return textVerifier(string(v))
default:
return jsonVerifier{v}
return NewJSONVerifier(v)
}
}

Expand Down

0 comments on commit 9e01a48

Please sign in to comment.