yq/pkg/yqlib/lib.go

501 lines
19 KiB
Go
Raw Normal View History

2021-01-12 22:35:57 +00:00
// Use the top level Evaluator or StreamEvaluator to evaluate expressions and return matches.
2020-11-03 23:48:43 +00:00
package yqlib
2020-10-08 23:59:03 +00:00
import (
"bytes"
2020-10-21 01:54:58 +00:00
"container/list"
2020-10-08 23:59:03 +00:00
"fmt"
2022-05-06 03:46:14 +00:00
"math"
2021-09-02 05:26:44 +00:00
"strconv"
"strings"
2020-10-08 23:59:03 +00:00
2020-11-14 02:38:44 +00:00
logging "gopkg.in/op/go-logging.v1"
yaml "gopkg.in/yaml.v3"
2020-10-08 23:59:03 +00:00
)
2022-02-01 03:47:51 +00:00
var ExpressionParser ExpressionParserInterface
func InitExpressionParser() {
if ExpressionParser == nil {
ExpressionParser = newExpressionParser()
}
}
2020-11-30 05:05:07 +00:00
var log = logging.MustGetLogger("yq-lib")
var PrettyPrintExp = `(... | (select(tag != "!!str"), select(tag == "!!str") | select(test("(?i)^(y|yes|n|no|on|off)$") | not)) ) style=""`
// GetLogger returns the yq logger instance.
func GetLogger() *logging.Logger {
return log
}
type operationType struct {
2020-10-11 23:44:33 +00:00
Type string
NumArgs uint // number of arguments to the op
Precedence uint
Handler operatorHandler
2020-10-11 23:44:33 +00:00
}
var orOpType = &operationType{Type: "OR", NumArgs: 2, Precedence: 20, Handler: orOperator}
var andOpType = &operationType{Type: "AND", NumArgs: 2, Precedence: 20, Handler: andOperator}
2021-02-15 06:31:12 +00:00
var reduceOpType = &operationType{Type: "REDUCE", NumArgs: 2, Precedence: 35, Handler: reduceOperator}
2021-02-15 03:27:00 +00:00
var blockOpType = &operationType{Type: "BLOCK", Precedence: 10, NumArgs: 2, Handler: emptyOperator}
2020-10-17 11:10:47 +00:00
var unionOpType = &operationType{Type: "UNION", NumArgs: 2, Precedence: 10, Handler: unionOperator}
2020-10-16 01:29:26 +00:00
var pipeOpType = &operationType{Type: "PIPE", NumArgs: 2, Precedence: 30, Handler: pipeOperator}
var assignOpType = &operationType{Type: "ASSIGN", NumArgs: 2, Precedence: 40, Handler: assignUpdateOperator}
var addAssignOpType = &operationType{Type: "ADD_ASSIGN", NumArgs: 2, Precedence: 40, Handler: addAssignOperator}
2021-03-24 21:12:01 +00:00
var subtractAssignOpType = &operationType{Type: "SUBTRACT_ASSIGN", NumArgs: 2, Precedence: 40, Handler: subtractAssignOperator}
2020-11-19 05:45:05 +00:00
var assignAttributesOpType = &operationType{Type: "ASSIGN_ATTRIBUTES", NumArgs: 2, Precedence: 40, Handler: assignAttributesOperator}
var assignStyleOpType = &operationType{Type: "ASSIGN_STYLE", NumArgs: 2, Precedence: 40, Handler: assignStyleOperator}
2021-02-03 04:51:26 +00:00
var assignVariableOpType = &operationType{Type: "ASSIGN_VARIABLE", NumArgs: 2, Precedence: 40, Handler: assignVariableOperator}
var assignTagOpType = &operationType{Type: "ASSIGN_TAG", NumArgs: 2, Precedence: 40, Handler: assignTagOperator}
var assignCommentOpType = &operationType{Type: "ASSIGN_COMMENT", NumArgs: 2, Precedence: 40, Handler: assignCommentsOperator}
var assignAnchorOpType = &operationType{Type: "ASSIGN_ANCHOR", NumArgs: 2, Precedence: 40, Handler: assignAnchorOperator}
var assignAliasOpType = &operationType{Type: "ASSIGN_ALIAS", NumArgs: 2, Precedence: 40, Handler: assignAliasOperator}
2020-11-02 00:20:38 +00:00
var multiplyOpType = &operationType{Type: "MULTIPLY", NumArgs: 2, Precedence: 42, Handler: multiplyOperator}
2022-01-22 05:40:17 +00:00
var multiplyAssignOpType = &operationType{Type: "MULTIPLY_ASSIGN", NumArgs: 2, Precedence: 42, Handler: multiplyAssignOperator}
var addOpType = &operationType{Type: "ADD", NumArgs: 2, Precedence: 42, Handler: addOperator}
2021-03-24 21:12:01 +00:00
var subtractOpType = &operationType{Type: "SUBTRACT", NumArgs: 2, Precedence: 42, Handler: subtractOperator}
var alternativeOpType = &operationType{Type: "ALTERNATIVE", NumArgs: 2, Precedence: 42, Handler: alternativeOperator}
2020-10-18 21:36:33 +00:00
var equalsOpType = &operationType{Type: "EQUALS", NumArgs: 2, Precedence: 40, Handler: equalsOperator}
2022-03-17 03:08:08 +00:00
var notEqualsOpType = &operationType{Type: "NOT_EQUALS", NumArgs: 2, Precedence: 40, Handler: notEqualsOperator}
var compareOpType = &operationType{Type: "COMPARE", NumArgs: 2, Precedence: 40, Handler: compareOperator}
// createmap needs to be above union, as we use union to build the components of the objects
var createMapOpType = &operationType{Type: "CREATE_MAP", NumArgs: 2, Precedence: 15, Handler: createMapOperator}
var shortPipeOpType = &operationType{Type: "SHORT_PIPE", NumArgs: 2, Precedence: 45, Handler: pipeOperator}
2020-10-16 01:29:26 +00:00
var lengthOpType = &operationType{Type: "LENGTH", NumArgs: 0, Precedence: 50, Handler: lengthOperator}
2022-02-27 00:56:46 +00:00
var lineOpType = &operationType{Type: "LINE", NumArgs: 0, Precedence: 50, Handler: lineOperator}
var columnOpType = &operationType{Type: "LINE", NumArgs: 0, Precedence: 50, Handler: columnOperator}
2022-11-08 02:40:00 +00:00
var expressionOpType = &operationType{Type: "EXP", NumArgs: 0, Precedence: 50, Handler: expressionOperator}
var collectOpType = &operationType{Type: "COLLECT", NumArgs: 1, Precedence: 50, Handler: collectOperator}
2021-11-30 23:32:36 +00:00
var mapOpType = &operationType{Type: "MAP", NumArgs: 1, Precedence: 50, Handler: mapOperator}
var errorOpType = &operationType{Type: "ERROR", NumArgs: 1, Precedence: 50, Handler: errorOperator}
2022-03-09 03:38:02 +00:00
var pickOpType = &operationType{Type: "PICK", NumArgs: 1, Precedence: 50, Handler: pickOperator}
2022-02-01 03:47:51 +00:00
var evalOpType = &operationType{Type: "EVAL", NumArgs: 1, Precedence: 50, Handler: evalOperator}
2021-11-30 23:32:36 +00:00
var mapValuesOpType = &operationType{Type: "MAP_VALUES", NumArgs: 1, Precedence: 50, Handler: mapValuesOperator}
var formatDateTimeOpType = &operationType{Type: "FORMAT_DATE_TIME", NumArgs: 1, Precedence: 50, Handler: formatDateTime}
var withDtFormatOpType = &operationType{Type: "WITH_DATE_TIME_FORMAT", NumArgs: 1, Precedence: 50, Handler: withDateTimeFormat}
var nowOpType = &operationType{Type: "NOW", NumArgs: 0, Precedence: 50, Handler: nowOp}
var tzOpType = &operationType{Type: "TIMEZONE", NumArgs: 1, Precedence: 50, Handler: tzOp}
2021-10-22 01:37:47 +00:00
var encodeOpType = &operationType{Type: "ENCODE", NumArgs: 0, Precedence: 50, Handler: encodeOperator}
var decodeOpType = &operationType{Type: "DECODE", NumArgs: 0, Precedence: 50, Handler: decodeOperator}
2021-05-09 05:12:50 +00:00
2021-05-14 04:29:55 +00:00
var anyOpType = &operationType{Type: "ANY", NumArgs: 0, Precedence: 50, Handler: anyOperator}
var allOpType = &operationType{Type: "ALL", NumArgs: 0, Precedence: 50, Handler: allOperator}
2021-09-15 05:18:10 +00:00
var containsOpType = &operationType{Type: "CONTAINS", NumArgs: 1, Precedence: 50, Handler: containsOperator}
2021-05-14 05:01:44 +00:00
var anyConditionOpType = &operationType{Type: "ANY_CONDITION", NumArgs: 1, Precedence: 50, Handler: anyOperator}
var allConditionOpType = &operationType{Type: "ALL_CONDITION", NumArgs: 1, Precedence: 50, Handler: allOperator}
2021-05-14 04:29:55 +00:00
2021-05-09 03:59:23 +00:00
var toEntriesOpType = &operationType{Type: "TO_ENTRIES", NumArgs: 0, Precedence: 50, Handler: toEntriesOperator}
2021-05-09 05:12:50 +00:00
var fromEntriesOpType = &operationType{Type: "FROM_ENTRIES", NumArgs: 0, Precedence: 50, Handler: fromEntriesOperator}
var withEntriesOpType = &operationType{Type: "WITH_ENTRIES", NumArgs: 1, Precedence: 50, Handler: withEntriesOperator}
2021-09-12 11:52:02 +00:00
var withOpType = &operationType{Type: "WITH", NumArgs: 1, Precedence: 50, Handler: withOperator}
2021-01-14 03:25:31 +00:00
var splitDocumentOpType = &operationType{Type: "SPLIT_DOC", NumArgs: 0, Precedence: 50, Handler: splitDocumentOperator}
2021-02-04 01:39:04 +00:00
var getVariableOpType = &operationType{Type: "GET_VARIABLE", NumArgs: 0, Precedence: 55, Handler: getVariableOperator}
var getStyleOpType = &operationType{Type: "GET_STYLE", NumArgs: 0, Precedence: 50, Handler: getStyleOperator}
var getTagOpType = &operationType{Type: "GET_TAG", NumArgs: 0, Precedence: 50, Handler: getTagOperator}
2021-11-23 22:57:35 +00:00
var getKeyOpType = &operationType{Type: "GET_KEY", NumArgs: 0, Precedence: 50, Handler: getKeyOperator}
2022-09-30 00:27:35 +00:00
var isKeyOpType = &operationType{Type: "IS_KEY", NumArgs: 0, Precedence: 50, Handler: isKeyOperator}
2021-11-23 23:16:48 +00:00
var getParentOpType = &operationType{Type: "GET_PARENT", NumArgs: 0, Precedence: 50, Handler: getParentOperator}
2021-11-23 22:57:35 +00:00
var getCommentOpType = &operationType{Type: "GET_COMMENT", NumArgs: 0, Precedence: 50, Handler: getCommentsOperator}
var getAnchorOpType = &operationType{Type: "GET_ANCHOR", NumArgs: 0, Precedence: 50, Handler: getAnchorOperator}
2022-07-13 04:45:28 +00:00
var getAliasOpType = &operationType{Type: "GET_ALIAS", NumArgs: 0, Precedence: 50, Handler: getAliasOperator}
var getDocumentIndexOpType = &operationType{Type: "GET_DOCUMENT_INDEX", NumArgs: 0, Precedence: 50, Handler: getDocumentIndexOperator}
var getFilenameOpType = &operationType{Type: "GET_FILENAME", NumArgs: 0, Precedence: 50, Handler: getFilenameOperator}
var getFileIndexOpType = &operationType{Type: "GET_FILE_INDEX", NumArgs: 0, Precedence: 50, Handler: getFileIndexOperator}
2022-10-05 03:12:08 +00:00
var getPathOpType = &operationType{Type: "GET_PATH", NumArgs: 0, Precedence: 50, Handler: getPathOperator}
2022-10-05 03:12:08 +00:00
var setPathOpType = &operationType{Type: "SET_PATH", NumArgs: 1, Precedence: 50, Handler: setPathOperator}
2022-10-05 09:09:53 +00:00
var delPathsOpType = &operationType{Type: "DEL_PATHS", NumArgs: 1, Precedence: 50, Handler: delPathsOperator}
2020-11-02 00:20:38 +00:00
var explodeOpType = &operationType{Type: "EXPLODE", NumArgs: 1, Precedence: 50, Handler: explodeOperator}
2021-11-28 02:25:22 +00:00
var sortByOpType = &operationType{Type: "SORT_BY", NumArgs: 1, Precedence: 50, Handler: sortByOperator}
2022-02-22 03:15:31 +00:00
var reverseOpType = &operationType{Type: "REVERSE", NumArgs: 0, Precedence: 50, Handler: reverseOperator}
2021-12-04 02:54:12 +00:00
var sortOpType = &operationType{Type: "SORT", NumArgs: 0, Precedence: 50, Handler: sortOperator}
2022-02-22 05:17:23 +00:00
var sortKeysOpType = &operationType{Type: "SORT_KEYS", NumArgs: 1, Precedence: 50, Handler: sortKeysOperator}
2022-02-22 05:17:23 +00:00
2021-01-14 03:46:50 +00:00
var joinStringOpType = &operationType{Type: "JOIN", NumArgs: 1, Precedence: 50, Handler: joinStringOperator}
2021-04-15 00:09:41 +00:00
var subStringOpType = &operationType{Type: "SUBSTR", NumArgs: 1, Precedence: 50, Handler: substituteStringOperator}
2021-07-07 12:40:46 +00:00
var matchOpType = &operationType{Type: "MATCH", NumArgs: 1, Precedence: 50, Handler: matchOperator}
2021-07-11 01:08:18 +00:00
var captureOpType = &operationType{Type: "CAPTURE", NumArgs: 1, Precedence: 50, Handler: captureOperator}
var testOpType = &operationType{Type: "TEST", NumArgs: 1, Precedence: 50, Handler: testOperator}
2021-01-14 04:05:50 +00:00
var splitStringOpType = &operationType{Type: "SPLIT", NumArgs: 1, Precedence: 50, Handler: splitStringOperator}
2022-02-22 05:17:23 +00:00
var changeCaseOpType = &operationType{Type: "CHANGE_CASE", NumArgs: 0, Precedence: 50, Handler: changeCaseOperator}
2022-08-08 03:35:57 +00:00
var trimOpType = &operationType{Type: "TRIM", NumArgs: 0, Precedence: 50, Handler: trimSpaceOperator}
2020-11-02 00:20:38 +00:00
2021-11-16 04:29:16 +00:00
var loadOpType = &operationType{Type: "LOAD", NumArgs: 1, Precedence: 50, Handler: loadYamlOperator}
2021-01-14 04:45:07 +00:00
var keysOpType = &operationType{Type: "KEYS", NumArgs: 0, Precedence: 50, Handler: keysOperator}
var collectObjectOpType = &operationType{Type: "COLLECT_OBJECT", NumArgs: 0, Precedence: 50, Handler: collectObjectOperator}
2021-02-04 01:39:04 +00:00
var traversePathOpType = &operationType{Type: "TRAVERSE_PATH", NumArgs: 0, Precedence: 55, Handler: traversePathOperator}
2021-02-03 04:51:26 +00:00
var traverseArrayOpType = &operationType{Type: "TRAVERSE_ARRAY", NumArgs: 2, Precedence: 50, Handler: traverseArrayOperator}
2020-10-20 02:53:26 +00:00
2021-02-04 01:39:04 +00:00
var selfReferenceOpType = &operationType{Type: "SELF", NumArgs: 0, Precedence: 55, Handler: selfOperator}
var valueOpType = &operationType{Type: "VALUE", NumArgs: 0, Precedence: 50, Handler: valueOperator}
var envOpType = &operationType{Type: "ENV", NumArgs: 0, Precedence: 50, Handler: envOperator}
var notOpType = &operationType{Type: "NOT", NumArgs: 0, Precedence: 50, Handler: notOperator}
2021-02-15 03:27:00 +00:00
var emptyOpType = &operationType{Type: "EMPTY", Precedence: 50, Handler: emptyOperator}
2020-10-20 02:53:26 +00:00
var envsubstOpType = &operationType{Type: "ENVSUBST", NumArgs: 0, Precedence: 50, Handler: envsubstOperator}
var recursiveDescentOpType = &operationType{Type: "RECURSIVE_DESCENT", NumArgs: 0, Precedence: 50, Handler: recursiveDescentOperator}
2020-10-11 23:44:33 +00:00
var selectOpType = &operationType{Type: "SELECT", NumArgs: 1, Precedence: 50, Handler: selectOperator}
var hasOpType = &operationType{Type: "HAS", NumArgs: 1, Precedence: 50, Handler: hasOperator}
2021-05-13 23:43:52 +00:00
var uniqueOpType = &operationType{Type: "UNIQUE", NumArgs: 0, Precedence: 50, Handler: unique}
var uniqueByOpType = &operationType{Type: "UNIQUE_BY", NumArgs: 1, Precedence: 50, Handler: uniqueBy}
2021-10-26 04:07:50 +00:00
var groupByOpType = &operationType{Type: "GROUP_BY", NumArgs: 1, Precedence: 50, Handler: groupBy}
2021-10-26 04:42:25 +00:00
var flattenOpType = &operationType{Type: "FLATTEN_BY", NumArgs: 0, Precedence: 50, Handler: flattenOp}
var deleteChildOpType = &operationType{Type: "DELETE", NumArgs: 1, Precedence: 40, Handler: deleteChildOperator}
2020-10-12 01:24:59 +00:00
2020-10-20 04:33:20 +00:00
type Operation struct {
OperationType *operationType
2020-10-20 02:53:26 +00:00
Value interface{}
StringValue string
CandidateNode *CandidateNode // used for Value Path elements
2020-10-29 23:56:45 +00:00
Preferences interface{}
2021-01-11 22:55:55 +00:00
UpdateAssign bool // used for assign ops, when true it means we evaluate the rhs given the lhs
2020-10-11 23:44:33 +00:00
}
2021-09-07 06:58:34 +00:00
func recurseNodeArrayEqual(lhs *yaml.Node, rhs *yaml.Node) bool {
if len(lhs.Content) != len(rhs.Content) {
return false
}
for index := 0; index < len(lhs.Content); index = index + 1 {
if !recursiveNodeEqual(lhs.Content[index], rhs.Content[index]) {
return false
}
}
return true
}
func findInArray(array *yaml.Node, item *yaml.Node) int {
for index := 0; index < len(array.Content); index = index + 1 {
if recursiveNodeEqual(array.Content[index], item) {
return index
}
}
return -1
}
func findKeyInMap(dataMap *yaml.Node, item *yaml.Node) int {
2022-03-09 03:38:02 +00:00
for index := 0; index < len(dataMap.Content); index = index + 2 {
if recursiveNodeEqual(dataMap.Content[index], item) {
2022-03-09 03:38:02 +00:00
return index
}
}
return -1
}
2021-09-07 06:58:34 +00:00
func recurseNodeObjectEqual(lhs *yaml.Node, rhs *yaml.Node) bool {
if len(lhs.Content) != len(rhs.Content) {
return false
}
for index := 0; index < len(lhs.Content); index = index + 2 {
key := lhs.Content[index]
value := lhs.Content[index+1]
indexInRHS := findInArray(rhs, key)
2021-09-07 06:58:34 +00:00
if indexInRHS == -1 || !recursiveNodeEqual(value, rhs.Content[indexInRHS+1]) {
2021-09-07 06:58:34 +00:00
return false
}
}
return true
}
2022-02-22 03:50:45 +00:00
func guessTagFromCustomType(node *yaml.Node) string {
if strings.HasPrefix(node.Tag, "!!") {
return node.Tag
} else if node.Value == "" {
2022-11-27 21:34:58 +00:00
log.Debug("guessTagFromCustomType: node has no value to guess the type with")
2022-02-22 03:50:45 +00:00
return node.Tag
}
2022-07-27 02:26:22 +00:00
dataBucket, errorReading := parseSnippet(node.Value)
2022-02-22 03:50:45 +00:00
if errorReading != nil {
2022-11-27 21:34:58 +00:00
log.Debug("guessTagFromCustomType: could not guess underlying tag type %v", errorReading)
2022-02-22 03:50:45 +00:00
return node.Tag
}
2022-07-27 02:26:22 +00:00
guessedTag := unwrapDoc(dataBucket).Tag
2022-02-22 03:50:45 +00:00
log.Info("im guessing the tag %v is a %v", node.Tag, guessedTag)
return guessedTag
}
2022-07-27 02:26:22 +00:00
func parseSnippet(value string) (*yaml.Node, error) {
2022-10-30 05:29:42 +00:00
if value == "" {
return &yaml.Node{
Kind: yaml.ScalarNode,
Tag: "!!null",
}, nil
}
decoder := NewYamlDecoder(ConfiguredYamlPreferences)
err := decoder.Init(strings.NewReader(value))
if err != nil {
return nil, err
}
parsedNode, err := decoder.Decode()
2022-11-09 10:36:53 +00:00
if err != nil {
return nil, err
}
if len(parsedNode.Node.Content) == 0 {
2022-07-27 02:26:22 +00:00
return nil, fmt.Errorf("bad data")
}
2022-11-15 00:35:31 +00:00
result := unwrapDoc(parsedNode.Node)
result.Line = 0
result.Column = 0
return result, err
2022-07-27 02:26:22 +00:00
}
2021-09-07 06:58:34 +00:00
func recursiveNodeEqual(lhs *yaml.Node, rhs *yaml.Node) bool {
2022-01-22 02:47:22 +00:00
if lhs.Kind != rhs.Kind {
2021-09-07 06:58:34 +00:00
return false
2022-01-22 02:47:22 +00:00
}
if lhs.Kind == yaml.ScalarNode {
//process custom tags of scalar nodes.
//dont worry about matching tags of maps or arrays.
2022-02-22 03:50:45 +00:00
lhsTag := guessTagFromCustomType(lhs)
rhsTag := guessTagFromCustomType(rhs)
2022-01-22 02:47:22 +00:00
if lhsTag != rhsTag {
return false
}
}
if lhs.Tag == "!!null" {
2021-09-07 06:58:34 +00:00
return true
} else if lhs.Kind == yaml.ScalarNode {
return lhs.Value == rhs.Value
} else if lhs.Kind == yaml.SequenceNode {
return recurseNodeArrayEqual(lhs, rhs)
} else if lhs.Kind == yaml.MappingNode {
return recurseNodeObjectEqual(lhs, rhs)
}
return false
2022-02-20 03:29:52 +00:00
}
func deepCloneContent(content []*yaml.Node) []*yaml.Node {
clonedContent := make([]*yaml.Node, len(content))
for i, child := range content {
clonedContent[i] = deepClone(child)
}
return clonedContent
}
2021-09-07 06:58:34 +00:00
2022-03-09 03:38:02 +00:00
func deepCloneNoContent(node *yaml.Node) *yaml.Node {
return deepCloneWithOptions(node, false)
}
2022-02-20 03:29:52 +00:00
func deepClone(node *yaml.Node) *yaml.Node {
2022-03-09 03:38:02 +00:00
return deepCloneWithOptions(node, true)
}
func deepCloneWithOptions(node *yaml.Node, cloneContent bool) *yaml.Node {
2022-02-20 03:29:52 +00:00
if node == nil {
return nil
}
2022-03-09 03:38:02 +00:00
var clonedContent []*yaml.Node
if cloneContent {
clonedContent = deepCloneContent(node.Content)
}
2022-02-20 03:29:52 +00:00
return &yaml.Node{
Content: clonedContent,
Kind: node.Kind,
Style: node.Style,
Tag: node.Tag,
Value: node.Value,
Anchor: node.Anchor,
Alias: deepClone(node.Alias),
HeadComment: node.HeadComment,
LineComment: node.LineComment,
FootComment: node.FootComment,
Line: node.Line,
Column: node.Column,
}
2021-09-07 06:58:34 +00:00
}
2021-09-02 05:26:44 +00:00
// yaml numbers can be hex encoded...
2022-05-06 03:46:14 +00:00
func parseInt64(numberString string) (string, int64, error) {
2021-09-02 05:26:44 +00:00
if strings.HasPrefix(numberString, "0x") ||
strings.HasPrefix(numberString, "0X") {
num, err := strconv.ParseInt(numberString[2:], 16, 64)
2021-09-02 05:26:44 +00:00
return "0x%X", num, err
}
num, err := strconv.ParseInt(numberString, 10, 64)
2021-09-02 05:26:44 +00:00
return "%v", num, err
}
2022-05-22 11:19:59 +00:00
func parseInt(numberString string) (int, error) {
2022-05-06 03:46:14 +00:00
var err error
var parsed int64
if strings.HasPrefix(numberString, "0x") ||
strings.HasPrefix(numberString, "0X") {
parsed, err = strconv.ParseInt(numberString[2:], 16, 64)
} else {
parsed, err = strconv.ParseInt(numberString, 10, 64)
}
if err != nil {
2022-05-22 11:19:59 +00:00
return 0, err
2022-10-29 07:15:21 +00:00
} else if parsed > math.MaxInt || parsed < math.MinInt {
return 0, fmt.Errorf("%v is not within [%v, %v]", parsed, math.MinInt, math.MaxInt)
2022-05-06 03:46:14 +00:00
}
2022-05-22 11:19:59 +00:00
return int(parsed), err
2022-05-06 03:46:14 +00:00
}
func createStringScalarNode(stringValue string) *yaml.Node {
var node = &yaml.Node{Kind: yaml.ScalarNode}
node.Value = stringValue
node.Tag = "!!str"
return node
}
2021-07-07 12:40:46 +00:00
func createScalarNode(value interface{}, stringValue string) *yaml.Node {
var node = &yaml.Node{Kind: yaml.ScalarNode}
2020-10-20 04:33:20 +00:00
node.Value = stringValue
switch value.(type) {
case float32, float64:
node.Tag = "!!float"
case int, int64, int32:
node.Tag = "!!int"
case bool:
node.Tag = "!!bool"
case string:
node.Tag = "!!str"
2020-10-20 04:40:11 +00:00
case nil:
node.Tag = "!!null"
2020-10-20 04:33:20 +00:00
}
2021-07-07 12:40:46 +00:00
return node
}
func headAndLineComment(node *yaml.Node) string {
return headComment(node) + lineComment(node)
}
func headComment(node *yaml.Node) string {
return strings.Replace(node.HeadComment, "#", "", 1)
}
func lineComment(node *yaml.Node) string {
return strings.Replace(node.LineComment, "#", "", 1)
}
func footComment(node *yaml.Node) string {
return strings.Replace(node.FootComment, "#", "", 1)
}
2021-07-07 12:40:46 +00:00
func createValueOperation(value interface{}, stringValue string) *Operation {
var node = createScalarNode(value, stringValue)
2020-10-20 04:33:20 +00:00
return &Operation{
OperationType: valueOpType,
2020-10-20 04:33:20 +00:00
Value: value,
StringValue: stringValue,
2021-07-07 12:40:46 +00:00
CandidateNode: &CandidateNode{Node: node},
2020-10-20 04:33:20 +00:00
}
}
2020-10-11 23:44:33 +00:00
// debugging purposes only
2020-10-20 04:33:20 +00:00
func (p *Operation) toString() string {
if p == nil {
return "OP IS NIL"
}
if p.OperationType == traversePathOpType {
2020-10-20 02:53:26 +00:00
return fmt.Sprintf("%v", p.Value)
} else if p.OperationType == selfReferenceOpType {
2020-10-20 02:53:26 +00:00
return "SELF"
} else if p.OperationType == valueOpType {
2020-10-20 02:53:26 +00:00
return fmt.Sprintf("%v (%T)", p.Value, p.Value)
} else {
return fmt.Sprintf("%v", p.OperationType.Type)
2020-10-11 23:44:33 +00:00
}
}
// use for debugging only
2020-10-21 01:54:58 +00:00
func NodesToString(collection *list.List) string {
2020-10-10 11:42:09 +00:00
if !log.IsEnabledFor(logging.DEBUG) {
return ""
}
2021-09-05 01:07:40 +00:00
result := fmt.Sprintf("%v results\n", collection.Len())
2020-10-10 11:42:09 +00:00
for el := collection.Front(); el != nil; el = el.Next() {
result = result + "\n" + NodeToString(el.Value.(*CandidateNode))
}
return result
}
2020-10-08 23:59:03 +00:00
func NodeToString(node *CandidateNode) string {
if !log.IsEnabledFor(logging.DEBUG) {
return ""
}
value := node.Node
if value == nil {
2020-10-17 11:10:47 +00:00
return "-- nil --"
2020-10-08 23:59:03 +00:00
}
buf := new(bytes.Buffer)
encoder := yaml.NewEncoder(buf)
errorEncoding := encoder.Encode(value)
if errorEncoding != nil {
log.Error("Error debugging node, %v", errorEncoding.Error())
}
errorClosingEncoder := encoder.Close()
if errorClosingEncoder != nil {
log.Error("Error closing encoder: ", errorClosingEncoder.Error())
}
2020-10-27 05:45:16 +00:00
tag := value.Tag
if value.Kind == yaml.DocumentNode {
tag = "doc"
2020-10-29 23:56:45 +00:00
} else if value.Kind == yaml.AliasNode {
tag = "alias"
2020-10-27 05:45:16 +00:00
}
return fmt.Sprintf(`D%v, P%v, (%v)::%v`, node.Document, node.Path, tag, buf.String())
2020-10-08 23:59:03 +00:00
}
func KindString(kind yaml.Kind) string {
switch kind {
case yaml.ScalarNode:
return "ScalarNode"
case yaml.SequenceNode:
return "SequenceNode"
case yaml.MappingNode:
return "MappingNode"
case yaml.DocumentNode:
return "DocumentNode"
case yaml.AliasNode:
return "AliasNode"
default:
return "unknown!"
}
}