From 8dea479d441a50d6c2ac86b2940a35954ca9c8ee Mon Sep 17 00:00:00 2001 From: jf-tech Date: Tue, 6 Oct 2020 10:00:34 +1300 Subject: [PATCH] Add JSON node related flags and helpers - in prep for new JSONStreamReader --- idr/jsonnode.go | 35 ++++++++++++++++++++++++++++++++++ idr/jsonnode_test.go | 45 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 idr/jsonnode.go create mode 100644 idr/jsonnode_test.go diff --git a/idr/jsonnode.go b/idr/jsonnode.go new file mode 100644 index 0000000..965a8bc --- /dev/null +++ b/idr/jsonnode.go @@ -0,0 +1,35 @@ +package idr + +type JSONType uint + +const ( + JSONRoot JSONType = 1 << iota + JSONObj + JSONArr + JSONProp + JSONValue +) + +func IsJSON(n *Node) bool { + _, ok := n.FormatSpecific.(JSONType) + return ok +} + +func JSONTypeOf(n *Node) JSONType { + if !IsJSON(n) { + panic("node is not JSON") + } + return n.FormatSpecific.(JSONType) +} + +func IsJSONRoot(n *Node) bool { return IsJSON(n) && JSONTypeOf(n)&JSONRoot != 0 } +func IsJSONObj(n *Node) bool { return IsJSON(n) && JSONTypeOf(n)&JSONObj != 0 } +func IsJSONArr(n *Node) bool { return IsJSON(n) && JSONTypeOf(n)&JSONArr != 0 } +func IsJSONProp(n *Node) bool { return IsJSON(n) && JSONTypeOf(n)&JSONProp != 0 } +func IsJSONValue(n *Node) bool { return IsJSON(n) && JSONTypeOf(n)&JSONValue != 0 } + +func CreateJSONNode(ntype NodeType, data string, jtype JSONType) *Node { + n := CreateNode(ntype, data) + n.FormatSpecific = jtype + return n +} diff --git a/idr/jsonnode_test.go b/idr/jsonnode_test.go new file mode 100644 index 0000000..31fb6b4 --- /dev/null +++ b/idr/jsonnode_test.go @@ -0,0 +1,45 @@ +package idr + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestIsJSON(t *testing.T) { + assert.True(t, IsJSON(CreateJSONNode(DocumentNode, "", JSONRoot))) + assert.True(t, IsJSON(CreateJSONNode(ElementNode, "a", JSONProp|JSONObj))) + assert.True(t, IsJSON(CreateJSONNode(TextNode, "text", JSONValue))) + assert.False(t, IsJSON(CreateNode(ElementNode, "a"))) + assert.False(t, IsJSON(CreateXMLNode(AttributeNode, "a", XMLSpecific{}))) +} + +func TestJSONTypeOf(t *testing.T) { + assert.Equal(t, JSONRoot, JSONTypeOf(CreateJSONNode(DocumentNode, "", JSONRoot))) + assert.Equal(t, JSONProp|JSONObj, JSONTypeOf(CreateJSONNode(ElementNode, "a", JSONProp|JSONObj))) + assert.PanicsWithValue(t, "node is not JSON", func() { + JSONTypeOf(CreateNode(ElementNode, "a")) + }) +} + +func TestIsJSONType(t *testing.T) { + assert.True(t, IsJSONRoot(CreateJSONNode(DocumentNode, "", JSONObj|JSONRoot))) + assert.False(t, IsJSONRoot(CreateJSONNode(ElementNode, "a", JSONProp|JSONObj))) + assert.False(t, IsJSONRoot(CreateXMLNode(AttributeNode, "a", XMLSpecific{}))) + + assert.True(t, IsJSONObj(CreateJSONNode(DocumentNode, "", JSONObj|JSONRoot))) + assert.False(t, IsJSONObj(CreateJSONNode(ElementNode, "a", JSONProp|JSONArr))) + assert.False(t, IsJSONObj(CreateXMLNode(AttributeNode, "a", XMLSpecific{}))) + + assert.True(t, IsJSONArr(CreateJSONNode(ElementNode, "a", JSONProp|JSONArr))) + assert.False(t, IsJSONArr(CreateJSONNode(TextNode, "a", JSONValue))) + assert.False(t, IsJSONArr(CreateXMLNode(TextNode, "a", XMLSpecific{}))) + + assert.True(t, IsJSONProp(CreateJSONNode(ElementNode, "a", JSONProp|JSONArr))) + assert.False(t, IsJSONProp(CreateJSONNode(DocumentNode, "", JSONRoot|JSONObj))) + assert.False(t, IsJSONProp(CreateXMLNode(ElementNode, "a", XMLSpecific{}))) + + assert.True(t, IsJSONValue(CreateJSONNode(DocumentNode, "a", JSONRoot|JSONValue))) + assert.False(t, IsJSONValue(CreateJSONNode(ElementNode, "a", JSONProp|JSONObj))) + assert.False(t, IsJSONValue(CreateXMLNode(ElementNode, "a", XMLSpecific{}))) +}