yq/pkg/yqlib/value_parser.go

52 lines
1.0 KiB
Go
Raw Normal View History

package yqlib
import (
"strconv"
2019-12-16 05:46:20 +00:00
logging "gopkg.in/op/go-logging.v1"
yaml "gopkg.in/yaml.v3"
)
type ValueParser interface {
2019-12-16 05:46:20 +00:00
Parse(argument string, customTag string) *yaml.Node
}
2019-12-16 05:46:20 +00:00
type valueParser struct {
log *logging.Logger
}
2019-12-16 05:46:20 +00:00
func NewValueParser(l *logging.Logger) ValueParser {
return &valueParser{log: l}
}
2019-12-16 05:46:20 +00:00
func (v *valueParser) Parse(argument string, customTag string) *yaml.Node {
var err interface{}
var tag = customTag
var inQuotes = len(argument) > 0 && argument[0] == '"'
2019-12-16 05:46:20 +00:00
if tag == "" && !inQuotes {
_, err = strconv.ParseBool(argument)
if err == nil {
2019-12-16 05:46:20 +00:00
tag = "!!bool"
}
2019-12-16 05:46:20 +00:00
_, err = strconv.ParseFloat(argument, 64)
if err == nil {
2019-12-16 05:46:20 +00:00
tag = "!!float"
}
_, err = strconv.ParseInt(argument, 10, 64)
if err == nil {
tag = "!!int"
}
if argument == "null" {
tag = "!!null"
}
if argument == "[]" {
2019-12-16 05:46:20 +00:00
return &yaml.Node{Tag: "!!seq", Kind: yaml.SequenceNode}
}
}
2019-12-16 05:46:20 +00:00
v.log.Debugf("parsed value '%v', tag: '%v'", argument, tag)
return &yaml.Node{Value: argument, Tag: tag, Kind: yaml.ScalarNode}
}