Skip to content

Commit

Permalink
Add xml format
Browse files Browse the repository at this point in the history
  • Loading branch information
Petya Koleva authored and tatiNo5 committed Nov 17, 2020
1 parent 9a2837a commit 19814c8
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 9 deletions.
42 changes: 35 additions & 7 deletions format.go
Expand Up @@ -6,15 +6,17 @@ import (
"encoding/pem"
"encoding/xml"
"fmt"
"github.com/xeipuuv/gojsonschema"
"golang.org/x/net/html"
"regexp"
"strings"

"github.com/xeipuuv/gojsonschema"
"golang.org/x/net/html"
)

func init() {
// annoyingly the format checker list is a global variable
gojsonschema.FormatCheckers.Add(newXMLFormatChecker("xml-template"))
gojsonschema.FormatCheckers.Add(newXMLFormatChecker("xml"))
gojsonschema.FormatCheckers.Add(newXMLTemplateFormatChecker("xml-template"))
gojsonschema.FormatCheckers.Add(newHTMLFormatChecker("html-template"))
gojsonschema.FormatCheckers.Add(newRegexFormatChecker("regex"))
gojsonschema.FormatCheckers.Add(newCryptoFormatChecker("pkcs1-private-key", pkcs1PrivateKey))
Expand Down Expand Up @@ -49,16 +51,42 @@ func (errs formatErrors) key(name interface{}, value interface{}) string {

// ----------------

type xmlFormatChecker struct {
type xmlFormatChecker struct{ name string }

func newXMLFormatChecker(name string) (string, gojsonschema.FormatChecker) {
return name, xmlFormatChecker{name: name}
}

func (f xmlFormatChecker) IsFormat(input interface{}) bool {
var err error

if s, ok := input.(string); ok {
err = xml.Unmarshal([]byte(s), new(interface{}))
err = wrapError(err, "Failed to parse xml")
} else {
err = makeError("The value is not a string")
}

if err != nil {
formatErrs.add(f.name, input, err)
return false
}

return true
}

// ----------------

type xmlTemplateFormatChecker struct {
tags *regexp.Regexp
name string
}

func newXMLFormatChecker(name string) (string, gojsonschema.FormatChecker) {
return name, xmlFormatChecker{name: name, tags: regexp.MustCompile(`{{[^{}]*}}`)}
func newXMLTemplateFormatChecker(name string) (string, gojsonschema.FormatChecker) {
return name, xmlTemplateFormatChecker{name: name, tags: regexp.MustCompile(`{{[^{}]*}}`)}
}

func (f xmlFormatChecker) IsFormat(input interface{}) bool {
func (f xmlTemplateFormatChecker) IsFormat(input interface{}) bool {
var err error

if s, ok := input.(string); ok {
Expand Down
48 changes: 46 additions & 2 deletions format_test.go
@@ -1,8 +1,9 @@
package conflate

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestFormatErrors_Get(t *testing.T) {
Expand Down Expand Up @@ -48,7 +49,7 @@ func TestXmlFormatCheckerIsFormat_NotString(t *testing.T) {

func TestXmlFormatCheckerIsFormat_Valid(t *testing.T) {
givenName := "xml"
givenValue := "<test>{{.Value}}</test>"
givenValue := "<test>Value</test>"
formatErrs.clear()
defer func() { formatErrs.clear() }()
name, checker := newXMLFormatChecker(givenName)
Expand All @@ -75,6 +76,49 @@ func TestXmlFormatCheckerIsFormat_NotValid(t *testing.T) {

// --------

func TestXmlTemplateFormatCheckerIsFormat_NotString(t *testing.T) {
givenName := "xml"
givenValue := 1
formatErrs.clear()
defer func() { formatErrs.clear() }()
name, checker := newXMLTemplateFormatChecker(givenName)
assert.Equal(t, givenName, name)
result := checker.IsFormat(givenValue)
assert.False(t, result)
err := formatErrs.get(name, givenValue)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "The value is not a string")
}

func TestXmlTemplateFormatCheckerIsFormat_Valid(t *testing.T) {
givenName := "xml"
givenValue := "<test>{{.Value}}</test>"
formatErrs.clear()
defer func() { formatErrs.clear() }()
name, checker := newXMLTemplateFormatChecker(givenName)
assert.Equal(t, givenName, name)
result := checker.IsFormat(givenValue)
assert.True(t, result)
err := formatErrs.get(name, givenValue)
assert.Nil(t, err)
}

func TestXmlTemplateFormatCheckerIsFormat_NotValid(t *testing.T) {
givenName := "xml"
givenValue := "<test>"
formatErrs.clear()
defer func() { formatErrs.clear() }()
name, checker := newXMLTemplateFormatChecker(givenName)
assert.Equal(t, givenName, name)
result := checker.IsFormat(givenValue)
assert.False(t, result)
err := formatErrs.get(name, givenValue)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Failed to parse xml")
}

// --------

func TestHtmlFormatCheckerIsFormat_NotString(t *testing.T) {
givenName := "html"
givenValue := 1
Expand Down

0 comments on commit 19814c8

Please sign in to comment.