go-yaml/parser/context.go

50 lines
788 B
Go
Raw Normal View History

2019-11-05 17:02:55 +09:00
package parser
2021-09-07 17:31:17 +09:00
import (
"fmt"
"strings"
2021-09-07 17:31:17 +09:00
)
2019-11-05 17:02:55 +09:00
// context context at parsing
type context struct {
2024-10-31 18:26:08 +09:00
path string
2021-09-07 17:31:17 +09:00
}
var pathSpecialChars = []string{
"$", "*", ".", "[", "]",
}
func containsPathSpecialChar(path string) bool {
for _, char := range pathSpecialChars {
if strings.Contains(path, char) {
return true
}
}
return false
}
func normalizePath(path string) string {
if containsPathSpecialChar(path) {
return fmt.Sprintf("'%s'", path)
}
return path
}
2021-09-07 17:31:17 +09:00
func (c *context) withChild(path string) *context {
2024-10-31 18:26:08 +09:00
ctx := *c
ctx.path = c.path + "." + normalizePath(path)
return &ctx
2021-09-07 17:31:17 +09:00
}
func (c *context) withIndex(idx uint) *context {
2024-10-31 18:26:08 +09:00
ctx := *c
ctx.path = c.path + "[" + fmt.Sprint(idx) + "]"
return &ctx
2019-11-05 17:02:55 +09:00
}
2024-10-31 18:26:08 +09:00
func newContext() *context {
2019-11-05 17:02:55 +09:00
return &context{
2024-10-31 18:26:08 +09:00
path: "$",
2019-11-05 17:02:55 +09:00
}
}