| 1 | package kdlscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl`/ |
| 2 | |
| 3 | import ( |
| 4 | "fmtscip-go gomod github.com/golang/go/src go1.25 fmt/" |
| 5 | |
| 6 | "github.com/tomwright/dasel/v3/modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/" |
| 7 | "github.com/tomwright/dasel/v3/parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/" |
| 8 | "github.com/tomwright/dasel/v3/parsing/kdl/internalscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/" |
| 9 | ) |
| 10 | |
| 11 | var _ parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.Readerscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Reader# = (*kdlReaderscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl`/kdlReader#)(nil) |
| 12 | |
| 13 | func newKDLReaderscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl`/newKDLReader().(_ parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.ReaderOptionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/ReaderOptions#) (parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.Readerscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Reader#, error) { |
| 14 | return &kdlReaderscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl`/kdlReader#{}, nil |
| 15 | } |
| 16 | |
| 17 | type kdlReaderscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl`/kdlReader# struct{} |
| 18 | |
| 19 | func (r *kdlReaderscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl`/kdlReader#) Readscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl`/kdlReader#Read().(data []byte) (*modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#, error) { |
| 20 | doc, err := internalscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/.Parsescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/Parse().(string(data)) |
| 21 | if err != nil { |
| 22 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("kdl: parse error: %w", err) |
| 23 | } |
| 24 | return nodesToValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl`/nodesToValue().(doc.Nodesscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/Document#Nodes.) |
| 25 | } |
| 26 | |
| 27 | // nodesToValue converts a list of KDL nodes into a model.Value map. |
| 28 | // Duplicate node names at the same level are promoted to slices. |
| 29 | func nodesToValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl`/nodesToValue().(nodes []*internalscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/.Nodescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/Node#) (*modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#, error) { |
| 30 | result := modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewMapValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewMapValue().() |
| 31 | seen := make(map[string]int) // tracks how many times a name appears |
| 32 | |
| 33 | for _, node := range nodes { |
| 34 | val, err := nodeToValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl`/nodeToValue().(node) |
| 35 | if err != nil { |
| 36 | return nil, err |
| 37 | } |
| 38 | |
| 39 | count := seen[node.Namescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/Node#Name.] |
| 40 | seen[node.Namescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/Node#Name.] = count + 1 |
| 41 | |
| 42 | switch count { |
| 43 | case 0: |
| 44 | // First time seeing this name |
| 45 | if err := result.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().(node.Namescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/Node#Name., val); err != nil { |
| 46 | return nil, err |
| 47 | } |
| 48 | case 1: |
| 49 | // Second time — promote existing value to slice |
| 50 | existing, err := result.GetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#GetMapKey().(node.Namescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/Node#Name.) |
| 51 | if err != nil { |
| 52 | return nil, err |
| 53 | } |
| 54 | slice := modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewSliceValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewSliceValue().() |
| 55 | if err := slice.Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(existing); err != nil { |
| 56 | return nil, err |
| 57 | } |
| 58 | if err := slice.Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(val); err != nil { |
| 59 | return nil, err |
| 60 | } |
| 61 | if err := result.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().(node.Namescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/Node#Name., slice); err != nil { |
| 62 | return nil, err |
| 63 | } |
| 64 | default: |
| 65 | // Third+ time — append to existing slice |
| 66 | existing, err := result.GetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#GetMapKey().(node.Namescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/Node#Name.) |
| 67 | if err != nil { |
| 68 | return nil, err |
| 69 | } |
| 70 | if err := existing.Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(val); err != nil { |
| 71 | return nil, err |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return result, nil |
| 77 | } |
| 78 | |
| 79 | // nodeToValue converts a single KDL node to a model.Value. |
| 80 | func nodeToValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl`/nodeToValue().(node *internalscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/.Nodescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/Node#) (*modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#, error) { |
| 81 | hasArgs := len(node.Argumentsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/Node#Arguments.) > 0 |
| 82 | hasProps := len(node.Propertiesscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/Node#Properties.) > 0 |
| 83 | hasChildren := len(node.Childrenscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/Node#Children.) > 0 |
| 84 | |
| 85 | // Node with no args, no props, no children → null |
| 86 | if !hasArgs && !hasProps && !hasChildren { |
| 87 | return modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewNullValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewNullValue().(), nil |
| 88 | } |
| 89 | |
| 90 | // Node with single argument, no properties, no children → scalar |
| 91 | if len(node.Argumentsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/Node#Arguments.) == 1 && !hasProps && !hasChildren { |
| 92 | return kdlValueToModelValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl`/kdlValueToModelValue().(node.Argumentsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/Node#Arguments.[0]) |
| 93 | } |
| 94 | |
| 95 | // Otherwise, build a map |
| 96 | result := modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewMapValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewMapValue().() |
| 97 | |
| 98 | // Multiple args → $args key |
| 99 | if hasArgs { |
| 100 | if len(node.Argumentsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/Node#Arguments.) == 1 && (hasProps || hasChildren) { |
| 101 | // Single arg with props/children: still use $args |
| 102 | argsSlice := modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewSliceValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewSliceValue().() |
| 103 | v, err := kdlValueToModelValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl`/kdlValueToModelValue().(node.Argumentsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/Node#Arguments.[0]) |
| 104 | if err != nil { |
| 105 | return nil, err |
| 106 | } |
| 107 | if err := argsSlice.Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(v); err != nil { |
| 108 | return nil, err |
| 109 | } |
| 110 | if err := result.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().("$args", argsSlice); err != nil { |
| 111 | return nil, err |
| 112 | } |
| 113 | } else if len(node.Argumentsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/Node#Arguments.) > 1 { |
| 114 | argsSlice := modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewSliceValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewSliceValue().() |
| 115 | for _, arg := range node.Argumentsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/Node#Arguments. { |
| 116 | v, err := kdlValueToModelValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl`/kdlValueToModelValue().(arg) |
| 117 | if err != nil { |
| 118 | return nil, err |
| 119 | } |
| 120 | if err := argsSlice.Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(v); err != nil { |
| 121 | return nil, err |
| 122 | } |
| 123 | } |
| 124 | if err := result.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().("$args", argsSlice); err != nil { |
| 125 | return nil, err |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // Properties become keys |
| 131 | for _, prop := range node.Propertiesscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/Node#Properties. { |
| 132 | v, err := kdlValueToModelValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl`/kdlValueToModelValue().(prop.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/Property#Value.) |
| 133 | if err != nil { |
| 134 | return nil, err |
| 135 | } |
| 136 | if err := result.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().(prop.Keyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/Property#Key., v); err != nil { |
| 137 | return nil, err |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // Children are merged into the map |
| 142 | if hasChildren { |
| 143 | childMap, err := nodesToValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl`/nodesToValue().(node.Childrenscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/Node#Children.) |
| 144 | if err != nil { |
| 145 | return nil, err |
| 146 | } |
| 147 | // Merge child keys into result |
| 148 | if err := childMap.RangeMapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#RangeMap().(func(key string, val *modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) error { |
| 149 | return result.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().(key, val) |
| 150 | }); err != nil { |
| 151 | return nil, err |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return result, nil |
| 156 | } |
| 157 | |
| 158 | func kdlValueToModelValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl`/kdlValueToModelValue().(v *internalscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/Value#) (*modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#, error) { |
| 159 | switch val := v.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/kdl/internal`/Value#Value..(type) { |
| 160 | case string: |
| 161 | return modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewStringValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewStringValue().(val), nil |
| 162 | case int64: |
| 163 | return modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewIntValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewIntValue().(val), nil |
| 164 | case float64: |
| 165 | return modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewFloatValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewFloatValue().(val), nil |
| 166 | case bool: |
| 167 | return modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewBoolValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewBoolValue().(val), nil |
| 168 | case nil: |
| 169 | return modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewNullValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewNullValue().(), nil |
| 170 | default: |
| 171 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("kdl: unsupported value type %T", val) |
| 172 | } |
| 173 | } |