yq/pkg/yqlib/value_parser.go

48 lines
894 B
Go
Raw Normal View History

package yqlib
import (
"strconv"
2019-12-16 05:46:20 +00:00
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 {
}
2020-01-08 21:17:56 +00:00
func NewValueParser() ValueParser {
return &valueParser{}
}
2019-12-16 05:46:20 +00:00
func (v *valueParser) Parse(argument string, customTag string) *yaml.Node {
var err interface{}
var tag = customTag
2020-01-08 21:17:56 +00:00
if tag == "" {
2019-12-16 05:46:20 +00:00
_, 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}
}
}
2020-01-08 21:17:56 +00:00
log.Debugf("parsed value '%v', tag: '%v'", argument, tag)
2019-12-16 05:46:20 +00:00
return &yaml.Node{Value: argument, Tag: tag, Kind: yaml.ScalarNode}
}