Pin github.com/googleapis/gnostic to v0.4.0
See https://github.com/google/gnostic/pull/155
This commit is contained in:
		
							
								
								
									
										347
									
								
								vendor/github.com/googleapis/gnostic/compiler/helpers.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										347
									
								
								vendor/github.com/googleapis/gnostic/compiler/helpers.go
									
									
									
										generated
									
									
										vendored
									
									
								
							| @ -1,4 +1,4 @@ | ||||
| // Copyright 2017 Google LLC. All Rights Reserved. | ||||
| // Copyright 2017 Google Inc. All Rights Reserved. | ||||
| // | ||||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||||
| // you may not use this file except in compliance with the License. | ||||
| @ -16,63 +16,56 @@ package compiler | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"gopkg.in/yaml.v2" | ||||
| 	"regexp" | ||||
| 	"sort" | ||||
| 	"strconv" | ||||
|  | ||||
| 	"github.com/googleapis/gnostic/jsonschema" | ||||
| 	"gopkg.in/yaml.v3" | ||||
| ) | ||||
|  | ||||
| // compiler helper functions, usually called from generated code | ||||
|  | ||||
| // UnpackMap gets a *yaml.Node if possible. | ||||
| func UnpackMap(in *yaml.Node) (*yaml.Node, bool) { | ||||
| 	if in == nil { | ||||
| 		return nil, false | ||||
| // UnpackMap gets a yaml.MapSlice if possible. | ||||
| func UnpackMap(in interface{}) (yaml.MapSlice, bool) { | ||||
| 	m, ok := in.(yaml.MapSlice) | ||||
| 	if ok { | ||||
| 		return m, true | ||||
| 	} | ||||
| 	return in, true | ||||
| 	// do we have an empty array? | ||||
| 	a, ok := in.([]interface{}) | ||||
| 	if ok && len(a) == 0 { | ||||
| 		// if so, return an empty map | ||||
| 		return yaml.MapSlice{}, true | ||||
| 	} | ||||
| 	return nil, false | ||||
| } | ||||
|  | ||||
| // SortedKeysForMap returns the sorted keys of a yamlv2.MapSlice. | ||||
| func SortedKeysForMap(m *yaml.Node) []string { | ||||
| // SortedKeysForMap returns the sorted keys of a yaml.MapSlice. | ||||
| func SortedKeysForMap(m yaml.MapSlice) []string { | ||||
| 	keys := make([]string, 0) | ||||
| 	if m.Kind == yaml.MappingNode { | ||||
| 		for i := 0; i < len(m.Content); i += 2 { | ||||
| 			keys = append(keys, m.Content[i].Value) | ||||
| 		} | ||||
| 	for _, item := range m { | ||||
| 		keys = append(keys, item.Key.(string)) | ||||
| 	} | ||||
| 	sort.Strings(keys) | ||||
| 	return keys | ||||
| } | ||||
|  | ||||
| // MapHasKey returns true if a yamlv2.MapSlice contains a specified key. | ||||
| func MapHasKey(m *yaml.Node, key string) bool { | ||||
| 	if m == nil { | ||||
| 		return false | ||||
| 	} | ||||
| 	if m.Kind == yaml.MappingNode { | ||||
| 		for i := 0; i < len(m.Content); i += 2 { | ||||
| 			itemKey := m.Content[i].Value | ||||
| 			if key == itemKey { | ||||
| 				return true | ||||
| 			} | ||||
| // MapHasKey returns true if a yaml.MapSlice contains a specified key. | ||||
| func MapHasKey(m yaml.MapSlice, key string) bool { | ||||
| 	for _, item := range m { | ||||
| 		itemKey, ok := item.Key.(string) | ||||
| 		if ok && key == itemKey { | ||||
| 			return true | ||||
| 		} | ||||
| 	} | ||||
| 	return false | ||||
| } | ||||
|  | ||||
| // MapValueForKey gets the value of a map value for a specified key. | ||||
| func MapValueForKey(m *yaml.Node, key string) *yaml.Node { | ||||
| 	if m == nil { | ||||
| 		return nil | ||||
| 	} | ||||
| 	if m.Kind == yaml.MappingNode { | ||||
| 		for i := 0; i < len(m.Content); i += 2 { | ||||
| 			itemKey := m.Content[i].Value | ||||
| 			if key == itemKey { | ||||
| 				return m.Content[i+1] | ||||
| 			} | ||||
| func MapValueForKey(m yaml.MapSlice, key string) interface{} { | ||||
| 	for _, item := range m { | ||||
| 		itemKey, ok := item.Key.(string) | ||||
| 		if ok && key == itemKey { | ||||
| 			return item.Value | ||||
| 		} | ||||
| 	} | ||||
| 	return nil | ||||
| @ -90,118 +83,8 @@ func ConvertInterfaceArrayToStringArray(interfaceArray []interface{}) []string { | ||||
| 	return stringArray | ||||
| } | ||||
|  | ||||
| // SequenceNodeForNode returns a node if it is a SequenceNode. | ||||
| func SequenceNodeForNode(node *yaml.Node) (*yaml.Node, bool) { | ||||
| 	if node.Kind != yaml.SequenceNode { | ||||
| 		return nil, false | ||||
| 	} | ||||
| 	return node, true | ||||
| } | ||||
|  | ||||
| // BoolForScalarNode returns the bool value of a node. | ||||
| func BoolForScalarNode(node *yaml.Node) (bool, bool) { | ||||
| 	if node == nil { | ||||
| 		return false, false | ||||
| 	} | ||||
| 	if node.Kind == yaml.DocumentNode { | ||||
| 		return BoolForScalarNode(node.Content[0]) | ||||
| 	} | ||||
| 	if node.Kind != yaml.ScalarNode { | ||||
| 		return false, false | ||||
| 	} | ||||
| 	if node.Tag != "!!bool" { | ||||
| 		return false, false | ||||
| 	} | ||||
| 	v, err := strconv.ParseBool(node.Value) | ||||
| 	if err != nil { | ||||
| 		return false, false | ||||
| 	} | ||||
| 	return v, true | ||||
| } | ||||
|  | ||||
| // IntForScalarNode returns the integer value of a node. | ||||
| func IntForScalarNode(node *yaml.Node) (int64, bool) { | ||||
| 	if node == nil { | ||||
| 		return 0, false | ||||
| 	} | ||||
| 	if node.Kind == yaml.DocumentNode { | ||||
| 		return IntForScalarNode(node.Content[0]) | ||||
| 	} | ||||
| 	if node.Kind != yaml.ScalarNode { | ||||
| 		return 0, false | ||||
| 	} | ||||
| 	if node.Tag != "!!int" { | ||||
| 		return 0, false | ||||
| 	} | ||||
| 	v, err := strconv.ParseInt(node.Value, 10, 64) | ||||
| 	if err != nil { | ||||
| 		return 0, false | ||||
| 	} | ||||
| 	return v, true | ||||
| } | ||||
|  | ||||
| // FloatForScalarNode returns the float value of a node. | ||||
| func FloatForScalarNode(node *yaml.Node) (float64, bool) { | ||||
| 	if node == nil { | ||||
| 		return 0.0, false | ||||
| 	} | ||||
| 	if node.Kind == yaml.DocumentNode { | ||||
| 		return FloatForScalarNode(node.Content[0]) | ||||
| 	} | ||||
| 	if node.Kind != yaml.ScalarNode { | ||||
| 		return 0.0, false | ||||
| 	} | ||||
| 	if (node.Tag != "!!int") && (node.Tag != "!!float") { | ||||
| 		return 0.0, false | ||||
| 	} | ||||
| 	v, err := strconv.ParseFloat(node.Value, 64) | ||||
| 	if err != nil { | ||||
| 		return 0.0, false | ||||
| 	} | ||||
| 	return v, true | ||||
| } | ||||
|  | ||||
| // StringForScalarNode returns the string value of a node. | ||||
| func StringForScalarNode(node *yaml.Node) (string, bool) { | ||||
| 	if node == nil { | ||||
| 		return "", false | ||||
| 	} | ||||
| 	if node.Kind == yaml.DocumentNode { | ||||
| 		return StringForScalarNode(node.Content[0]) | ||||
| 	} | ||||
| 	switch node.Kind { | ||||
| 	case yaml.ScalarNode: | ||||
| 		switch node.Tag { | ||||
| 		case "!!int": | ||||
| 			return node.Value, true | ||||
| 		case "!!str": | ||||
| 			return node.Value, true | ||||
| 		case "!!timestamp": | ||||
| 			return node.Value, true | ||||
| 		case "!!null": | ||||
| 			return "", true | ||||
| 		default: | ||||
| 			return "", false | ||||
| 		} | ||||
| 	default: | ||||
| 		return "", false | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // StringArrayForSequenceNode converts a sequence node to an array of strings, if possible. | ||||
| func StringArrayForSequenceNode(node *yaml.Node) []string { | ||||
| 	stringArray := make([]string, 0) | ||||
| 	for _, item := range node.Content { | ||||
| 		v, ok := StringForScalarNode(item) | ||||
| 		if ok { | ||||
| 			stringArray = append(stringArray, v) | ||||
| 		} | ||||
| 	} | ||||
| 	return stringArray | ||||
| } | ||||
|  | ||||
| // MissingKeysInMap identifies which keys from a list of required keys are not in a map. | ||||
| func MissingKeysInMap(m *yaml.Node, requiredKeys []string) []string { | ||||
| func MissingKeysInMap(m yaml.MapSlice, requiredKeys []string) []string { | ||||
| 	missingKeys := make([]string, 0) | ||||
| 	for _, k := range requiredKeys { | ||||
| 		if !MapHasKey(m, k) { | ||||
| @ -212,109 +95,64 @@ func MissingKeysInMap(m *yaml.Node, requiredKeys []string) []string { | ||||
| } | ||||
|  | ||||
| // InvalidKeysInMap returns keys in a map that don't match a list of allowed keys and patterns. | ||||
| func InvalidKeysInMap(m *yaml.Node, allowedKeys []string, allowedPatterns []*regexp.Regexp) []string { | ||||
| func InvalidKeysInMap(m yaml.MapSlice, allowedKeys []string, allowedPatterns []*regexp.Regexp) []string { | ||||
| 	invalidKeys := make([]string, 0) | ||||
| 	if m == nil || m.Kind != yaml.MappingNode { | ||||
| 		return invalidKeys | ||||
| 	} | ||||
| 	for i := 0; i < len(m.Content); i += 2 { | ||||
| 		key := m.Content[i].Value | ||||
| 		found := false | ||||
| 		// does the key match an allowed key? | ||||
| 		for _, allowedKey := range allowedKeys { | ||||
| 			if key == allowedKey { | ||||
| 				found = true | ||||
| 				break | ||||
| 			} | ||||
| 		} | ||||
| 		if !found { | ||||
| 			// does the key match an allowed pattern? | ||||
| 			for _, allowedPattern := range allowedPatterns { | ||||
| 				if allowedPattern.MatchString(key) { | ||||
| 	for _, item := range m { | ||||
| 		itemKey, ok := item.Key.(string) | ||||
| 		if ok { | ||||
| 			key := itemKey | ||||
| 			found := false | ||||
| 			// does the key match an allowed key? | ||||
| 			for _, allowedKey := range allowedKeys { | ||||
| 				if key == allowedKey { | ||||
| 					found = true | ||||
| 					break | ||||
| 				} | ||||
| 			} | ||||
| 			if !found { | ||||
| 				invalidKeys = append(invalidKeys, key) | ||||
| 				// does the key match an allowed pattern? | ||||
| 				for _, allowedPattern := range allowedPatterns { | ||||
| 					if allowedPattern.MatchString(key) { | ||||
| 						found = true | ||||
| 						break | ||||
| 					} | ||||
| 				} | ||||
| 				if !found { | ||||
| 					invalidKeys = append(invalidKeys, key) | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| 	return invalidKeys | ||||
| } | ||||
|  | ||||
| // NewNullNode creates a new Null node. | ||||
| func NewNullNode() *yaml.Node { | ||||
| 	node := &yaml.Node{ | ||||
| 		Kind: yaml.ScalarNode, | ||||
| 		Tag:  "!!null", | ||||
| // DescribeMap describes a map (for debugging purposes). | ||||
| func DescribeMap(in interface{}, indent string) string { | ||||
| 	description := "" | ||||
| 	m, ok := in.(map[string]interface{}) | ||||
| 	if ok { | ||||
| 		keys := make([]string, 0) | ||||
| 		for k := range m { | ||||
| 			keys = append(keys, k) | ||||
| 		} | ||||
| 		sort.Strings(keys) | ||||
| 		for _, k := range keys { | ||||
| 			v := m[k] | ||||
| 			description += fmt.Sprintf("%s%s:\n", indent, k) | ||||
| 			description += DescribeMap(v, indent+"  ") | ||||
| 		} | ||||
| 		return description | ||||
| 	} | ||||
| 	return node | ||||
| } | ||||
|  | ||||
| // NewMappingNode creates a new Mapping node. | ||||
| func NewMappingNode() *yaml.Node { | ||||
| 	return &yaml.Node{ | ||||
| 		Kind:    yaml.MappingNode, | ||||
| 		Content: make([]*yaml.Node, 0), | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // NewSequenceNode creates a new Sequence node. | ||||
| func NewSequenceNode() *yaml.Node { | ||||
| 	node := &yaml.Node{ | ||||
| 		Kind:    yaml.SequenceNode, | ||||
| 		Content: make([]*yaml.Node, 0), | ||||
| 	} | ||||
| 	return node | ||||
| } | ||||
|  | ||||
| // NewScalarNodeForString creates a new node to hold a string. | ||||
| func NewScalarNodeForString(s string) *yaml.Node { | ||||
| 	return &yaml.Node{ | ||||
| 		Kind:  yaml.ScalarNode, | ||||
| 		Tag:   "!!str", | ||||
| 		Value: s, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // NewSequenceNodeForStringArray creates a new node to hold an array of strings. | ||||
| func NewSequenceNodeForStringArray(strings []string) *yaml.Node { | ||||
| 	node := &yaml.Node{ | ||||
| 		Kind:    yaml.SequenceNode, | ||||
| 		Content: make([]*yaml.Node, 0), | ||||
| 	} | ||||
| 	for _, s := range strings { | ||||
| 		node.Content = append(node.Content, NewScalarNodeForString(s)) | ||||
| 	} | ||||
| 	return node | ||||
| } | ||||
|  | ||||
| // NewScalarNodeForBool creates a new node to hold a bool. | ||||
| func NewScalarNodeForBool(b bool) *yaml.Node { | ||||
| 	return &yaml.Node{ | ||||
| 		Kind:  yaml.ScalarNode, | ||||
| 		Tag:   "!!bool", | ||||
| 		Value: fmt.Sprintf("%t", b), | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // NewScalarNodeForFloat creates a new node to hold a float. | ||||
| func NewScalarNodeForFloat(f float64) *yaml.Node { | ||||
| 	return &yaml.Node{ | ||||
| 		Kind:  yaml.ScalarNode, | ||||
| 		Tag:   "!!float", | ||||
| 		Value: fmt.Sprintf("%g", f), | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // NewScalarNodeForInt creates a new node to hold an integer. | ||||
| func NewScalarNodeForInt(i int64) *yaml.Node { | ||||
| 	return &yaml.Node{ | ||||
| 		Kind:  yaml.ScalarNode, | ||||
| 		Tag:   "!!int", | ||||
| 		Value: fmt.Sprintf("%d", i), | ||||
| 	a, ok := in.([]interface{}) | ||||
| 	if ok { | ||||
| 		for i, v := range a { | ||||
| 			description += fmt.Sprintf("%s%d:\n", indent, i) | ||||
| 			description += DescribeMap(v, indent+"  ") | ||||
| 		} | ||||
| 		return description | ||||
| 	} | ||||
| 	description += fmt.Sprintf("%s%+v\n", indent, in) | ||||
| 	return description | ||||
| } | ||||
|  | ||||
| // PluralProperties returns the string "properties" pluralized. | ||||
| @ -357,40 +195,3 @@ func StringValue(item interface{}) (value string, ok bool) { | ||||
| 	} | ||||
| 	return "", false | ||||
| } | ||||
|  | ||||
| // Description returns a human-readable represention of an item. | ||||
| func Description(item interface{}) string { | ||||
| 	value, ok := item.(*yaml.Node) | ||||
| 	if ok { | ||||
| 		return jsonschema.Render(value) | ||||
| 	} | ||||
| 	return fmt.Sprintf("%+v", item) | ||||
| } | ||||
|  | ||||
| // Display returns a description of a node for use in error messages. | ||||
| func Display(node *yaml.Node) string { | ||||
| 	switch node.Kind { | ||||
| 	case yaml.ScalarNode: | ||||
| 		switch node.Tag { | ||||
| 		case "!!str": | ||||
| 			return fmt.Sprintf("%s (string)", node.Value) | ||||
| 		} | ||||
| 	} | ||||
| 	return fmt.Sprintf("%+v (%T)", node, node) | ||||
| } | ||||
|  | ||||
| // Marshal creates a yaml version of a structure in our preferred style | ||||
| func Marshal(in *yaml.Node) []byte { | ||||
| 	clearStyle(in) | ||||
| 	//bytes, _ := yaml.Marshal(&yaml.Node{Kind: yaml.DocumentNode, Content: []*yaml.Node{in}}) | ||||
| 	bytes, _ := yaml.Marshal(in) | ||||
|  | ||||
| 	return bytes | ||||
| } | ||||
|  | ||||
| func clearStyle(node *yaml.Node) { | ||||
| 	node.Style = 0 | ||||
| 	for _, c := range node.Content { | ||||
| 		clearStyle(c) | ||||
| 	} | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user