Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions idr/jsonnode.go
Original file line number Diff line number Diff line change
@@ -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
}
45 changes: 45 additions & 0 deletions idr/jsonnode_test.go
Original file line number Diff line number Diff line change
@@ -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{})))
}