MCPcopy
hub / github.com/TomWright/dasel / nodeToValue

Function nodeToValue

parsing/kdl/kdl_reader.go:80–156  ·  view source on GitHub ↗

nodeToValue converts a single KDL node to a model.Value.

(node *internal.Node)

Source from the content-addressed store, hash-verified

78
79// nodeToValue converts a single KDL node to a model.Value.
80func nodeToValue(node *internal.Node) (*model.Value, error) {
81 hasArgs := len(node.Arguments) > 0
82 hasProps := len(node.Properties) > 0
83 hasChildren := len(node.Children) > 0
84
85 // Node with no args, no props, no children → null
86 if !hasArgs && !hasProps && !hasChildren {
87 return model.NewNullValue(), nil
88 }
89
90 // Node with single argument, no properties, no children → scalar
91 if len(node.Arguments) == 1 && !hasProps && !hasChildren {
92 return kdlValueToModelValue(node.Arguments[0])
93 }
94
95 // Otherwise, build a map
96 result := model.NewMapValue()
97
98 // Multiple args → $args key
99 if hasArgs {
100 if len(node.Arguments) == 1 && (hasProps || hasChildren) {
101 // Single arg with props/children: still use $args
102 argsSlice := model.NewSliceValue()
103 v, err := kdlValueToModelValue(node.Arguments[0])
104 if err != nil {
105 return nil, err
106 }
107 if err := argsSlice.Append(v); err != nil {
108 return nil, err
109 }
110 if err := result.SetMapKey("$args", argsSlice); err != nil {
111 return nil, err
112 }
113 } else if len(node.Arguments) > 1 {
114 argsSlice := model.NewSliceValue()
115 for _, arg := range node.Arguments {
116 v, err := kdlValueToModelValue(arg)
117 if err != nil {
118 return nil, err
119 }
120 if err := argsSlice.Append(v); err != nil {
121 return nil, err
122 }
123 }
124 if err := result.SetMapKey("$args", argsSlice); err != nil {
125 return nil, err
126 }
127 }
128 }
129
130 // Properties become keys
131 for _, prop := range node.Properties {
132 v, err := kdlValueToModelValue(prop.Value)
133 if err != nil {
134 return nil, err
135 }
136 if err := result.SetMapKey(prop.Key, v); err != nil {
137 return nil, err

Callers 1

nodesToValueFunction · 0.85

Calls 8

NewNullValueFunction · 0.92
NewMapValueFunction · 0.92
NewSliceValueFunction · 0.92
kdlValueToModelValueFunction · 0.85
nodesToValueFunction · 0.85
AppendMethod · 0.80
SetMapKeyMethod · 0.80
RangeMapMethod · 0.80

Tested by

no test coverage detected