mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-06 04:20:11 +00:00
Utilities ported from vacuum.
This commit is contained in:
1061
test_specs/petstorev2.json
Normal file
1061
test_specs/petstorev2.json
Normal file
File diff suppressed because it is too large
Load Diff
1225
test_specs/petstorev3.json
Normal file
1225
test_specs/petstorev3.json
Normal file
File diff suppressed because it is too large
Load Diff
441
utils/utils.go
Normal file
441
utils/utils.go
Normal file
@@ -0,0 +1,441 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/vmware-labs/yaml-jsonpath/pkg/yamlpath"
|
||||
"gopkg.in/yaml.v3"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
// OpenApi3 is used by all OpenAPI 3+ docs
|
||||
OpenApi3 = "openapi"
|
||||
|
||||
// OpenApi2 is used by all OpenAPI 2 docs, formerly known as swagger.
|
||||
OpenApi2 = "swagger"
|
||||
|
||||
// AsyncApi is used by akk AsyncAPI docs, all versions.
|
||||
AsyncApi = "asyncapi"
|
||||
)
|
||||
|
||||
// FindNodes will find a node based on JSONPath, it accepts raw yaml/json as input.
|
||||
func FindNodes(yamlData []byte, jsonPath string) ([]*yaml.Node, error) {
|
||||
jsonPath = FixContext(jsonPath)
|
||||
|
||||
var node yaml.Node
|
||||
yaml.Unmarshal(yamlData, &node)
|
||||
|
||||
path, err := yamlpath.NewPath(jsonPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results, _ := path.Find(&node)
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func FindLastChildNode(node *yaml.Node) *yaml.Node {
|
||||
s := len(node.Content) - 1
|
||||
if s < 0 {
|
||||
s = 0
|
||||
}
|
||||
if len(node.Content) > 0 && len(node.Content[s].Content) > 0 {
|
||||
return FindLastChildNode(node.Content[s])
|
||||
} else {
|
||||
if len(node.Content) > 0 {
|
||||
return node.Content[s]
|
||||
}
|
||||
return node
|
||||
}
|
||||
}
|
||||
|
||||
// BuildPath will construct a JSONPath from a base and an array of strings.
|
||||
func BuildPath(basePath string, segs []string) string {
|
||||
|
||||
path := strings.Join(segs, ".")
|
||||
|
||||
// trim that last period.
|
||||
if len(path) > 0 && path[len(path)-1] == '.' {
|
||||
path = path[:len(path)-1]
|
||||
}
|
||||
return fmt.Sprintf("%s.%s", basePath, path)
|
||||
}
|
||||
|
||||
// FindNodesWithoutDeserializing will find a node based on JSONPath, without deserializing from yaml/json
|
||||
func FindNodesWithoutDeserializing(node *yaml.Node, jsonPath string) ([]*yaml.Node, error) {
|
||||
jsonPath = FixContext(jsonPath)
|
||||
|
||||
path, err := yamlpath.NewPath(jsonPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results, _ := path.Find(node)
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// ConvertInterfaceIntoStringMap will convert an unknown input into a string map.
|
||||
func ConvertInterfaceIntoStringMap(context interface{}) map[string]string {
|
||||
converted := make(map[string]string)
|
||||
if context != nil {
|
||||
if v, ok := context.(map[string]interface{}); ok {
|
||||
for k, n := range v {
|
||||
if s, okB := n.(string); okB {
|
||||
converted[k] = s
|
||||
}
|
||||
}
|
||||
}
|
||||
if v, ok := context.(map[string]string); ok {
|
||||
for k, n := range v {
|
||||
converted[k] = n
|
||||
}
|
||||
}
|
||||
}
|
||||
return converted
|
||||
}
|
||||
|
||||
// ConvertInterfaceToStringArray will convert an unknown input map type into a string array/slice
|
||||
func ConvertInterfaceToStringArray(raw interface{}) []string {
|
||||
if vals, ok := raw.(map[string]interface{}); ok {
|
||||
var s []string
|
||||
for _, v := range vals {
|
||||
if v, ok := v.([]interface{}); ok {
|
||||
for _, q := range v {
|
||||
s = append(s, fmt.Sprint(q))
|
||||
}
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
if vals, ok := raw.(map[string][]string); ok {
|
||||
var s []string
|
||||
for _, v := range vals {
|
||||
s = append(s, v...)
|
||||
}
|
||||
return s
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ConvertInterfaceArrayToStringArray will convert an unknown interface array type, into a string slice
|
||||
func ConvertInterfaceArrayToStringArray(raw interface{}) []string {
|
||||
if vals, ok := raw.([]interface{}); ok {
|
||||
s := make([]string, len(vals))
|
||||
for i, v := range vals {
|
||||
s[i] = fmt.Sprint(v)
|
||||
}
|
||||
return s
|
||||
}
|
||||
if vals, ok := raw.([]string); ok {
|
||||
return vals
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ExtractValueFromInterfaceMap pulls out an unknown value from a map using a string key
|
||||
func ExtractValueFromInterfaceMap(name string, raw interface{}) interface{} {
|
||||
|
||||
if propMap, ok := raw.(map[string]interface{}); ok {
|
||||
if props, okn := propMap[name].([]interface{}); okn {
|
||||
return props
|
||||
} else {
|
||||
return propMap[name]
|
||||
}
|
||||
}
|
||||
if propMap, ok := raw.(map[string][]string); ok {
|
||||
return propMap[name]
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindFirstKeyNode will locate the first key and value yaml.Node based on a key.
|
||||
func FindFirstKeyNode(key string, nodes []*yaml.Node, depth int) (keyNode *yaml.Node, valueNode *yaml.Node) {
|
||||
if depth > 40 {
|
||||
return nil, nil
|
||||
}
|
||||
for i, v := range nodes {
|
||||
if key != "" && key == v.Value {
|
||||
if i+1 >= len(nodes) {
|
||||
return v, nodes[i] // next node is what we need.
|
||||
}
|
||||
return v, nodes[i+1] // next node is what we need.
|
||||
}
|
||||
if len(v.Content) > 0 {
|
||||
depth++
|
||||
x, y := FindFirstKeyNode(key, v.Content, depth)
|
||||
if x != nil && y != nil {
|
||||
return x, y
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// KeyNodeResult is a result from a KeyNodeSearch performed by the FindAllKeyNodesWithPath
|
||||
type KeyNodeResult struct {
|
||||
KeyNode *yaml.Node
|
||||
ValueNode *yaml.Node
|
||||
Parent *yaml.Node
|
||||
Path []yaml.Node
|
||||
}
|
||||
|
||||
// KeyNodeSearch keeps a track of everything we have found on our adventure down the trees.
|
||||
type KeyNodeSearch struct {
|
||||
Key string
|
||||
Ignore []string
|
||||
Results []*KeyNodeResult
|
||||
AllowExtensions bool
|
||||
}
|
||||
|
||||
// FindKeyNodeTop is a non-recursive search of top level nodes for a key, will not look at content.
|
||||
// Returns the key and value
|
||||
func FindKeyNodeTop(key string, nodes []*yaml.Node) (keyNode *yaml.Node, valueNode *yaml.Node) {
|
||||
|
||||
for i, v := range nodes {
|
||||
if key == v.Value {
|
||||
return v, nodes[i+1] // next node is what we need.
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// FindKeyNode is a non-recursive search of a *yaml.Node Content for a child node with a key.
|
||||
// Returns the key and value
|
||||
func FindKeyNode(key string, nodes []*yaml.Node) (keyNode *yaml.Node, valueNode *yaml.Node) {
|
||||
|
||||
//numNodes := len(nodes)
|
||||
for i, v := range nodes {
|
||||
if i%2 == 0 && key == v.Value {
|
||||
return v, nodes[i+1] // next node is what we need.
|
||||
}
|
||||
for x, j := range v.Content {
|
||||
if key == j.Value {
|
||||
if IsNodeMap(v) {
|
||||
if x+1 == len(v.Content) {
|
||||
return v, v.Content[x]
|
||||
}
|
||||
return v, v.Content[x+1] // next node is what we need.
|
||||
|
||||
}
|
||||
if IsNodeArray(v) {
|
||||
return v, v.Content[x]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var ObjectLabel = "object"
|
||||
var IntegerLabel = "integer"
|
||||
var NumberLabel = "number"
|
||||
var StringLabel = "string"
|
||||
var BinaryLabel = "binary"
|
||||
var ArrayLabel = "array"
|
||||
var BooleanLabel = "boolean"
|
||||
var SchemaSource = "https://json-schema.org/draft/2020-12/schema"
|
||||
var SchemaId = "https://quobix.com/api/vacuum"
|
||||
|
||||
func MakeTagReadable(node *yaml.Node) string {
|
||||
switch node.Tag {
|
||||
case "!!map":
|
||||
return ObjectLabel
|
||||
case "!!seq":
|
||||
return ArrayLabel
|
||||
case "!!str":
|
||||
return StringLabel
|
||||
case "!!int":
|
||||
return IntegerLabel
|
||||
case "!!float":
|
||||
return NumberLabel
|
||||
case "!!bool":
|
||||
return BooleanLabel
|
||||
}
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
// IsNodeMap checks if the node is a map type
|
||||
func IsNodeMap(node *yaml.Node) bool {
|
||||
if node == nil {
|
||||
return false
|
||||
}
|
||||
return node.Tag == "!!map"
|
||||
}
|
||||
|
||||
// IsNodePolyMorphic will return true if the node contains polymorphic keys.
|
||||
func IsNodePolyMorphic(node *yaml.Node) bool {
|
||||
for i, v := range node.Content {
|
||||
if i%2 == 0 {
|
||||
if v.Value == "anyOf" || v.Value == "oneOf" || v.Value == "allOf" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// IsNodeArray checks if a node is an array type
|
||||
func IsNodeArray(node *yaml.Node) bool {
|
||||
if node == nil {
|
||||
return false
|
||||
}
|
||||
return node.Tag == "!!seq"
|
||||
}
|
||||
|
||||
// IsNodeStringValue checks if a node is a string value
|
||||
func IsNodeStringValue(node *yaml.Node) bool {
|
||||
if node == nil {
|
||||
return false
|
||||
}
|
||||
return node.Tag == "!!str"
|
||||
}
|
||||
|
||||
// IsNodeIntValue will check if a node is an int value
|
||||
func IsNodeIntValue(node *yaml.Node) bool {
|
||||
if node == nil {
|
||||
return false
|
||||
}
|
||||
return node.Tag == "!!int"
|
||||
}
|
||||
|
||||
// IsNodeFloatValue will check is a node is a float value.
|
||||
func IsNodeFloatValue(node *yaml.Node) bool {
|
||||
if node == nil {
|
||||
return false
|
||||
}
|
||||
return node.Tag == "!!float"
|
||||
}
|
||||
|
||||
// IsNodeBoolValue will check is a node is a bool
|
||||
func IsNodeBoolValue(node *yaml.Node) bool {
|
||||
if node == nil {
|
||||
return false
|
||||
}
|
||||
return node.Tag == "!!bool"
|
||||
}
|
||||
|
||||
// FixContext will clean up a JSONpath string to be correctly traversable.
|
||||
func FixContext(context string) string {
|
||||
|
||||
tokens := strings.Split(context, ".")
|
||||
var cleaned = []string{}
|
||||
for i, t := range tokens {
|
||||
|
||||
if v, err := strconv.Atoi(t); err == nil {
|
||||
|
||||
if v < 200 { // codes start here
|
||||
if cleaned[i-1] != "" {
|
||||
cleaned[i-1] += fmt.Sprintf("[%v]", t)
|
||||
}
|
||||
} else {
|
||||
cleaned = append(cleaned, t)
|
||||
}
|
||||
continue
|
||||
}
|
||||
cleaned = append(cleaned, strings.ReplaceAll(t, "(root)", "$"))
|
||||
|
||||
}
|
||||
return strings.Join(cleaned, ".")
|
||||
}
|
||||
|
||||
// IsJSON will tell you if a string is JSON or not.
|
||||
func IsJSON(testString string) bool {
|
||||
if testString == "" {
|
||||
return false
|
||||
}
|
||||
runes := []rune(strings.TrimSpace(testString))
|
||||
if runes[0] == '{' && runes[len(runes)-1] == '}' {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// IsYAML will tell you if a string is YAML or not.
|
||||
func IsYAML(testString string) bool {
|
||||
if testString == "" {
|
||||
return false
|
||||
}
|
||||
if IsJSON(testString) {
|
||||
return false
|
||||
}
|
||||
var n interface{}
|
||||
err := yaml.Unmarshal([]byte(testString), &n)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
_, err = yaml.Marshal(n)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// ConvertYAMLtoJSON will do exactly what you think it will. It will deserialize YAML into serialized JSON.
|
||||
func ConvertYAMLtoJSON(yamlData []byte) ([]byte, error) {
|
||||
var decodedYaml map[string]interface{}
|
||||
err := yaml.Unmarshal(yamlData, &decodedYaml)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// if the data can be decoded, it can be encoded (that's my view anyway). no need for an error check.
|
||||
jsonData, _ := json.Marshal(decodedYaml)
|
||||
return jsonData, nil
|
||||
}
|
||||
|
||||
// IsHttpVerb will check if an operation is valid or not.
|
||||
func IsHttpVerb(verb string) bool {
|
||||
verbs := []string{"get", "post", "put", "patch", "delete", "options", "trace", "head"}
|
||||
for _, v := range verbs {
|
||||
if verb == v {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
|
||||
segs := strings.Split(id, "/")
|
||||
name := segs[len(segs)-1]
|
||||
|
||||
replaced := strings.ReplaceAll(fmt.Sprintf("%s['%s']",
|
||||
strings.Join(segs[:len(segs)-1], "."), name), "#", "$")
|
||||
|
||||
if replaced[0] != '$' {
|
||||
replaced = fmt.Sprintf("$%s", replaced)
|
||||
}
|
||||
return name, replaced
|
||||
}
|
||||
|
||||
func ConvertComponentIdIntoPath(id string) (string, string) {
|
||||
segs := strings.Split(id, "/")
|
||||
name := segs[len(segs)-1]
|
||||
|
||||
return name, strings.ReplaceAll(fmt.Sprintf("%s.%s",
|
||||
strings.Join(segs[:len(segs)-1], "."), name), "#", "$")
|
||||
}
|
||||
|
||||
func RenderCodeSnippet(startNode *yaml.Node, specData []string, before, after int) string {
|
||||
|
||||
buf := new(strings.Builder)
|
||||
|
||||
startLine := startNode.Line - before
|
||||
endLine := startNode.Line + after
|
||||
|
||||
if startLine < 0 {
|
||||
startLine = 0
|
||||
}
|
||||
|
||||
if endLine >= len(specData) {
|
||||
endLine = len(specData) - 1
|
||||
}
|
||||
|
||||
delta := endLine - startLine
|
||||
|
||||
for i := 0; i < delta; i++ {
|
||||
l := startLine + i
|
||||
if l < len(specData) {
|
||||
line := specData[l]
|
||||
buf.WriteString(fmt.Sprintf("%s\n", line))
|
||||
}
|
||||
}
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
421
utils/utils_test.go
Normal file
421
utils/utils_test.go
Normal file
@@ -0,0 +1,421 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/yaml.v3"
|
||||
"io/ioutil"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type petstore []byte
|
||||
|
||||
var once sync.Once
|
||||
|
||||
var (
|
||||
psBytes petstore
|
||||
)
|
||||
|
||||
func getPetstore() petstore {
|
||||
once.Do(func() {
|
||||
psBytes, _ = ioutil.ReadFile("../test_specs/petstorev3.json")
|
||||
})
|
||||
return psBytes
|
||||
}
|
||||
|
||||
func TestRenderCodeSnippet(t *testing.T) {
|
||||
code := []string{"hey", "ho", "let's", "go!"}
|
||||
startNode := &yaml.Node{
|
||||
Line: 1,
|
||||
}
|
||||
rendered := RenderCodeSnippet(startNode, code, 1, 3)
|
||||
assert.Equal(t, "hey\nho\nlet's\n", rendered)
|
||||
|
||||
}
|
||||
|
||||
func TestFindNodes(t *testing.T) {
|
||||
nodes, err := FindNodes(getPetstore(), "$.info.contact")
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, nodes)
|
||||
assert.Len(t, nodes, 1)
|
||||
}
|
||||
|
||||
func TestFindNodes_BadPath(t *testing.T) {
|
||||
nodes, err := FindNodes(getPetstore(), "I am not valid")
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, nodes)
|
||||
}
|
||||
|
||||
func TestFindLastChildNode(t *testing.T) {
|
||||
nodes, _ := FindNodes(getPetstore(), "$.info")
|
||||
lastNode := FindLastChildNode(nodes[0])
|
||||
assert.Equal(t, "1.0.11", lastNode.Value) // should be the version.
|
||||
}
|
||||
|
||||
func TestFindLastChildNode_WithKids(t *testing.T) {
|
||||
nodes, _ := FindNodes(getPetstore(), "$.paths./pet")
|
||||
lastNode := FindLastChildNode(nodes[0])
|
||||
assert.Equal(t, "read:pets", lastNode.Value)
|
||||
}
|
||||
|
||||
func TestFindLastChildNode_NotFound(t *testing.T) {
|
||||
node := &yaml.Node{
|
||||
Value: "same",
|
||||
}
|
||||
lastNode := FindLastChildNode(node)
|
||||
assert.Equal(t, "same", lastNode.Value) // should be the same node
|
||||
}
|
||||
|
||||
func TestBuildPath(t *testing.T) {
|
||||
|
||||
assert.Equal(t, "$.fresh.fish.and.chicken.nuggets",
|
||||
BuildPath("$.fresh.fish", []string{"and", "chicken", "nuggets"}))
|
||||
}
|
||||
|
||||
func TestBuildPath_WithTrailingPeriod(t *testing.T) {
|
||||
|
||||
assert.Equal(t, "$.fresh.fish.and.chicken.nuggets",
|
||||
BuildPath("$.fresh.fish", []string{"and", "chicken", "nuggets", ""}))
|
||||
}
|
||||
|
||||
func TestFindNodesWithoutDeserializing(t *testing.T) {
|
||||
root, err := FindNodes(getPetstore(), "$")
|
||||
nodes, err := FindNodesWithoutDeserializing(root[0], "$.info.contact")
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, nodes)
|
||||
assert.Len(t, nodes, 1)
|
||||
}
|
||||
|
||||
func TestFindNodesWithoutDeserializing_InvalidPath(t *testing.T) {
|
||||
root, err := FindNodes(getPetstore(), "$")
|
||||
nodes, err := FindNodesWithoutDeserializing(root[0], "I love a good curry")
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, nodes)
|
||||
}
|
||||
|
||||
func TestConvertInterfaceIntoStringMap(t *testing.T) {
|
||||
var d interface{}
|
||||
n := make(map[string]string)
|
||||
n["melody"] = "baby girl"
|
||||
d = n
|
||||
parsed := ConvertInterfaceIntoStringMap(d)
|
||||
assert.Equal(t, "baby girl", parsed["melody"])
|
||||
}
|
||||
|
||||
func TestConvertInterfaceIntoStringMap_NoType(t *testing.T) {
|
||||
var d interface{}
|
||||
n := make(map[string]interface{})
|
||||
n["melody"] = "baby girl"
|
||||
d = n
|
||||
parsed := ConvertInterfaceIntoStringMap(d)
|
||||
assert.Equal(t, "baby girl", parsed["melody"])
|
||||
}
|
||||
|
||||
func TestConvertInterfaceToStringArray(t *testing.T) {
|
||||
var d interface{}
|
||||
n := make(map[string][]string)
|
||||
n["melody"] = []string{"melody", "is", "my", "baby"}
|
||||
d = n
|
||||
parsed := ConvertInterfaceToStringArray(d)
|
||||
assert.Equal(t, "baby", parsed[3])
|
||||
}
|
||||
|
||||
func TestConvertInterfaceToStringArray_NoType(t *testing.T) {
|
||||
var d interface{}
|
||||
m := make([]interface{}, 4)
|
||||
n := make(map[string]interface{})
|
||||
m[0] = "melody"
|
||||
m[1] = "is"
|
||||
m[2] = "my"
|
||||
m[3] = "baby"
|
||||
n["melody"] = m
|
||||
d = n
|
||||
parsed := ConvertInterfaceToStringArray(d)
|
||||
assert.Equal(t, "baby", parsed[3])
|
||||
}
|
||||
|
||||
func TestConvertInterfaceToStringArray_Invalid(t *testing.T) {
|
||||
var d interface{}
|
||||
d = "I am a carrot"
|
||||
parsed := ConvertInterfaceToStringArray(d)
|
||||
assert.Nil(t, parsed)
|
||||
}
|
||||
|
||||
func TestConvertInterfaceArrayToStringArray(t *testing.T) {
|
||||
var d interface{}
|
||||
m := []string{"maddox", "is", "my", "little", "champion"}
|
||||
d = m
|
||||
parsed := ConvertInterfaceArrayToStringArray(d)
|
||||
assert.Equal(t, "little", parsed[3])
|
||||
}
|
||||
|
||||
func TestConvertInterfaceArrayToStringArray_NoType(t *testing.T) {
|
||||
var d interface{}
|
||||
m := make([]interface{}, 4)
|
||||
m[0] = "melody"
|
||||
m[1] = "is"
|
||||
m[2] = "my"
|
||||
m[3] = "baby"
|
||||
d = m
|
||||
parsed := ConvertInterfaceArrayToStringArray(d)
|
||||
assert.Equal(t, "baby", parsed[3])
|
||||
}
|
||||
|
||||
func TestConvertInterfaceArrayToStringArray_Invalid(t *testing.T) {
|
||||
var d interface{}
|
||||
d = "weed is good"
|
||||
parsed := ConvertInterfaceArrayToStringArray(d)
|
||||
assert.Nil(t, parsed)
|
||||
}
|
||||
|
||||
func TestExtractValueFromInterfaceMap(t *testing.T) {
|
||||
var d interface{}
|
||||
m := make(map[string][]string)
|
||||
m["melody"] = []string{"is", "my", "baby"}
|
||||
d = m
|
||||
parsed := ExtractValueFromInterfaceMap("melody", d)
|
||||
assert.Equal(t, "baby", parsed.([]string)[2])
|
||||
}
|
||||
|
||||
func TestExtractValueFromInterfaceMap_NoType(t *testing.T) {
|
||||
var d interface{}
|
||||
m := make(map[string]interface{})
|
||||
n := make([]interface{}, 3)
|
||||
n[0] = "maddy"
|
||||
n[1] = "the"
|
||||
n[2] = "champion"
|
||||
m["maddy"] = n
|
||||
d = m
|
||||
parsed := ExtractValueFromInterfaceMap("maddy", d)
|
||||
assert.Equal(t, "champion", parsed.([]interface{})[2])
|
||||
}
|
||||
|
||||
func TestExtractValueFromInterfaceMap_Flat(t *testing.T) {
|
||||
var d interface{}
|
||||
m := make(map[string]interface{})
|
||||
m["maddy"] = "niblet"
|
||||
d = m
|
||||
parsed := ExtractValueFromInterfaceMap("maddy", d)
|
||||
assert.Equal(t, "niblet", parsed.(interface{}))
|
||||
}
|
||||
|
||||
func TestExtractValueFromInterfaceMap_NotFound(t *testing.T) {
|
||||
var d interface{}
|
||||
d = "not a map"
|
||||
parsed := ExtractValueFromInterfaceMap("melody", d)
|
||||
assert.Nil(t, parsed)
|
||||
}
|
||||
|
||||
func TestFindFirstKeyNode(t *testing.T) {
|
||||
nodes, _ := FindNodes(getPetstore(), "$")
|
||||
key, value := FindFirstKeyNode("operationId", nodes, 0)
|
||||
assert.NotNil(t, key)
|
||||
assert.NotNil(t, value)
|
||||
assert.Equal(t, 55, key.Line)
|
||||
}
|
||||
|
||||
func TestFindFirstKeyNode_NotFound(t *testing.T) {
|
||||
nodes, _ := FindNodes(getPetstore(), "$")
|
||||
key, value := FindFirstKeyNode("i-do-not-exist-in-the-doc", nodes, 0)
|
||||
assert.Nil(t, key)
|
||||
assert.Nil(t, value)
|
||||
}
|
||||
|
||||
func TestFindFirstKeyNode_Map(t *testing.T) {
|
||||
nodes, _ := FindNodes(getPetstore(), "$")
|
||||
key, value := FindFirstKeyNode("pet", nodes, 0)
|
||||
assert.NotNil(t, key)
|
||||
assert.NotNil(t, value)
|
||||
assert.Equal(t, 27, key.Line)
|
||||
}
|
||||
|
||||
func TestFindKeyNodeTop(t *testing.T) {
|
||||
nodes, _ := FindNodes(getPetstore(), "$")
|
||||
k, v := FindKeyNodeTop("info", nodes[0].Content)
|
||||
assert.NotNil(t, k)
|
||||
assert.NotNil(t, v)
|
||||
assert.Equal(t, 3, k.Line)
|
||||
}
|
||||
|
||||
func TestFindKeyNodeTop_NotFound(t *testing.T) {
|
||||
nodes, _ := FindNodes(getPetstore(), "$")
|
||||
k, v := FindKeyNodeTop("i am a giant potato", nodes[0].Content)
|
||||
assert.Nil(t, k)
|
||||
assert.Nil(t, v)
|
||||
}
|
||||
|
||||
func TestFindKeyNode(t *testing.T) {
|
||||
nodes, _ := FindNodes(getPetstore(), "$")
|
||||
k, v := FindKeyNode("/pet", nodes[0].Content)
|
||||
assert.NotNil(t, k)
|
||||
assert.NotNil(t, v)
|
||||
assert.Equal(t, 47, k.Line)
|
||||
}
|
||||
|
||||
func TestFindKeyNode_NotFound(t *testing.T) {
|
||||
nodes, _ := FindNodes(getPetstore(), "$")
|
||||
k, v := FindKeyNode("I am not anything at all", nodes[0].Content)
|
||||
assert.Nil(t, k)
|
||||
assert.Nil(t, v)
|
||||
}
|
||||
|
||||
func TestMakeTagReadable(t *testing.T) {
|
||||
n := &yaml.Node{
|
||||
Tag: "!!map",
|
||||
}
|
||||
assert.Equal(t, ObjectLabel, MakeTagReadable(n))
|
||||
n.Tag = "!!seq"
|
||||
assert.Equal(t, ArrayLabel, MakeTagReadable(n))
|
||||
n.Tag = "!!str"
|
||||
assert.Equal(t, StringLabel, MakeTagReadable(n))
|
||||
n.Tag = "!!int"
|
||||
assert.Equal(t, IntegerLabel, MakeTagReadable(n))
|
||||
n.Tag = "!!float"
|
||||
assert.Equal(t, NumberLabel, MakeTagReadable(n))
|
||||
n.Tag = "!!bool"
|
||||
assert.Equal(t, BooleanLabel, MakeTagReadable(n))
|
||||
n.Tag = "mr potato man is here"
|
||||
assert.Equal(t, "unknown", MakeTagReadable(n))
|
||||
}
|
||||
|
||||
func TestIsNodeMap(t *testing.T) {
|
||||
n := &yaml.Node{
|
||||
Tag: "!!map",
|
||||
}
|
||||
assert.True(t, IsNodeMap(n))
|
||||
n.Tag = "!!pizza"
|
||||
assert.False(t, IsNodeMap(n))
|
||||
}
|
||||
|
||||
func TestIsNodeMap_Nil(t *testing.T) {
|
||||
assert.False(t, IsNodeMap(nil))
|
||||
}
|
||||
|
||||
func TestIsNodePolyMorphic(t *testing.T) {
|
||||
n := &yaml.Node{
|
||||
Content: []*yaml.Node{
|
||||
{
|
||||
Value: "anyOf",
|
||||
},
|
||||
},
|
||||
}
|
||||
assert.True(t, IsNodePolyMorphic(n))
|
||||
n.Content[0].Value = "cakes"
|
||||
assert.False(t, IsNodePolyMorphic(n))
|
||||
}
|
||||
|
||||
func TestIsNodeArray(t *testing.T) {
|
||||
n := &yaml.Node{
|
||||
Tag: "!!seq",
|
||||
}
|
||||
assert.True(t, IsNodeArray(n))
|
||||
n.Tag = "!!pizza"
|
||||
assert.False(t, IsNodeArray(n))
|
||||
}
|
||||
|
||||
func TestIsNodeArray_Nil(t *testing.T) {
|
||||
assert.False(t, IsNodeArray(nil))
|
||||
}
|
||||
|
||||
func TestIsNodeStringValue(t *testing.T) {
|
||||
n := &yaml.Node{
|
||||
Tag: "!!str",
|
||||
}
|
||||
assert.True(t, IsNodeStringValue(n))
|
||||
n.Tag = "!!pizza"
|
||||
assert.False(t, IsNodeStringValue(n))
|
||||
}
|
||||
|
||||
func TestIsNodeStringValue_Nil(t *testing.T) {
|
||||
assert.False(t, IsNodeStringValue(nil))
|
||||
}
|
||||
|
||||
func TestIsNodeIntValue(t *testing.T) {
|
||||
n := &yaml.Node{
|
||||
Tag: "!!int",
|
||||
}
|
||||
assert.True(t, IsNodeIntValue(n))
|
||||
n.Tag = "!!pizza"
|
||||
assert.False(t, IsNodeIntValue(n))
|
||||
}
|
||||
|
||||
func TestIsNodeIntValue_Nil(t *testing.T) {
|
||||
assert.False(t, IsNodeIntValue(nil))
|
||||
}
|
||||
|
||||
func TestIsNodeFloatValue(t *testing.T) {
|
||||
n := &yaml.Node{
|
||||
Tag: "!!float",
|
||||
}
|
||||
assert.True(t, IsNodeFloatValue(n))
|
||||
n.Tag = "!!pizza"
|
||||
assert.False(t, IsNodeFloatValue(n))
|
||||
}
|
||||
|
||||
func TestIsNodeFloatValue_Nil(t *testing.T) {
|
||||
assert.False(t, IsNodeFloatValue(nil))
|
||||
}
|
||||
|
||||
func TestIsNodeBoolValue(t *testing.T) {
|
||||
n := &yaml.Node{
|
||||
Tag: "!!bool",
|
||||
}
|
||||
assert.True(t, IsNodeBoolValue(n))
|
||||
n.Tag = "!!pizza"
|
||||
assert.False(t, IsNodeBoolValue(n))
|
||||
}
|
||||
|
||||
func TestIsNodeBoolValue_Nil(t *testing.T) {
|
||||
assert.False(t, IsNodeBoolValue(nil))
|
||||
}
|
||||
|
||||
func TestFixContext(t *testing.T) {
|
||||
assert.Equal(t, "$.nuggets[12].name", FixContext("(root).nuggets.12.name"))
|
||||
}
|
||||
|
||||
func TestFixContext_HttpCode(t *testing.T) {
|
||||
assert.Equal(t, "$.nuggets.404.name", FixContext("(root).nuggets.404.name"))
|
||||
}
|
||||
|
||||
func TestIsJSON(t *testing.T) {
|
||||
assert.True(t, IsJSON("{'hello':'there'}"))
|
||||
assert.False(t, IsJSON("potato shoes"))
|
||||
assert.False(t, IsJSON(""))
|
||||
}
|
||||
|
||||
func TestIsYAML(t *testing.T) {
|
||||
assert.True(t, IsYAML("hello:\n there:\n my-name: is quobix"))
|
||||
assert.True(t, IsYAML("potato shoes"))
|
||||
assert.False(t, IsYAML("{'hello':'there'}"))
|
||||
assert.False(t, IsYAML(""))
|
||||
assert.False(t, IsYAML("8908: hello: yeah: \n12309812: :123"))
|
||||
}
|
||||
|
||||
func TestConvertYAMLtoJSON(t *testing.T) {
|
||||
str, err := ConvertYAMLtoJSON([]byte("hello: there"))
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, str)
|
||||
assert.Equal(t, "{\"hello\":\"there\"}", string(str))
|
||||
|
||||
str, err = ConvertYAMLtoJSON([]byte("gonna: break: you:\nyeah:yeah:yeah"))
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, str)
|
||||
}
|
||||
|
||||
func TestIsHttpVerb(t *testing.T) {
|
||||
assert.True(t, IsHttpVerb("get"))
|
||||
assert.True(t, IsHttpVerb("post"))
|
||||
assert.False(t, IsHttpVerb("nuggets"))
|
||||
}
|
||||
|
||||
func TestConvertComponentIdIntoFriendlyPathSearch(t *testing.T) {
|
||||
segment, path := ConvertComponentIdIntoFriendlyPathSearch("#/chicken/chips/pizza/cake")
|
||||
assert.Equal(t, "$.chicken.chips.pizza['cake']", path)
|
||||
assert.Equal(t, "cake", segment)
|
||||
}
|
||||
|
||||
func TestConvertComponentIdIntoPath(t *testing.T) {
|
||||
segment, path := ConvertComponentIdIntoPath("#/chicken/chips/pizza/cake")
|
||||
assert.Equal(t, "$.chicken.chips.pizza.cake", path)
|
||||
assert.Equal(t, "cake", segment)
|
||||
}
|
||||
Reference in New Issue
Block a user