| 1 |
package tomlscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/ |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytesscip-go gomod github.com/golang/go/src go1.25 bytes/" |
| 5 |
"fmtscip-go gomod github.com/golang/go/src go1.25 fmt/" |
| 6 |
"reflectscip-go gomod github.com/golang/go/src go1.25 reflect/" |
| 7 |
"strconvscip-go gomod github.com/golang/go/src go1.25 strconv/" |
| 8 |
"stringsscip-go gomod github.com/golang/go/src go1.25 strings/" |
| 9 |
|
| 10 |
pkgscip-go gomod github.com/pelletier/go-toml/v2 v2.4.3 `github.com/pelletier/go-toml/v2`/ "github.com/pelletier/go-toml/v2scip-go gomod github.com/pelletier/go-toml/v2 v2.4.3 `github.com/pelletier/go-toml/v2`/" |
| 11 |
"github.com/tomwright/dasel/v3/modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/" |
| 12 |
"github.com/tomwright/dasel/v3/parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/" |
| 13 |
) |
| 14 |
|
| 15 |
var _ parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.Writerscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer# = (*tomlWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/tomlWriter#)(nil) |
| 16 |
|
| 17 |
func newTOMLWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/newTOMLWriter().(options parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.WriterOptionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/WriterOptions#) (parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.Writerscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer#, error) { |
| 18 |
w := &tomlWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/tomlWriter#{optionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/tomlWriter#options.: options} |
| 19 |
return w, nil |
| 20 |
} |
| 21 |
|
| 22 |
type tomlWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/tomlWriter# struct { |
| 23 |
optionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/tomlWriter#options. parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.WriterOptionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/WriterOptions# |
| 24 |
} |
| 25 |
|
| 26 |
// Write converts the dasel model.Value into Go values backed by dynamically |
| 27 |
// generated struct types (reflect.StructOf) that preserve key ordering, then |
| 28 |
// delegates to go-toml Marshal to produce canonical TOML output. |
| 29 |
// This implementation doesn't preserve all formatting metadata (multiline strings, etc) |
| 30 |
// and needs some work to do so. |
| 31 |
func (j *tomlWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/tomlWriter#) Writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/tomlWriter#Write().(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#) ([]byte, error) { |
| 32 |
if value == nil { |
| 33 |
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().("nil value") |
| 34 |
} |
| 35 |
|
| 36 |
// We currently encode the top level value directly. We have little control over nested values. |
| 37 |
// Perhaps it would be better to implement a custom Encoder that respects metadata on nested values? |
| 38 |
var buf bytesscip-go gomod github.com/golang/go/src go1.25 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25 bytes/Buffer# |
| 39 |
encoder := pkgscip-go gomod github.com/pelletier/go-toml/v2 v2.4.3 `github.com/pelletier/go-toml/v2`/.NewEncoderscip-go gomod github.com/pelletier/go-toml/v2 v2.4.3 `github.com/pelletier/go-toml/v2`/NewEncoder().(&buf) |
| 40 |
if j.optionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/tomlWriter#options..Compactscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/WriterOptions#Compact. { |
| 41 |
encoder.SetIndentTablesscip-go gomod github.com/pelletier/go-toml/v2 v2.4.3 `github.com/pelletier/go-toml/v2`/Encoder#SetIndentTables().(false) |
| 42 |
encoder.SetTablesInlinescip-go gomod github.com/pelletier/go-toml/v2 v2.4.3 `github.com/pelletier/go-toml/v2`/Encoder#SetTablesInline().(true) |
| 43 |
encoder.SetArraysMultilinescip-go gomod github.com/pelletier/go-toml/v2 v2.4.3 `github.com/pelletier/go-toml/v2`/Encoder#SetArraysMultiline().(false) |
| 44 |
} |
| 45 |
|
| •46 |
var outBytes []byte |
| 47 |
|
| 48 |
if value.IsMapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#IsMap().() { |
| 49 |
goValue, err := buildGoValueForMapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/buildGoValueForMap().(value) |
| 50 |
if err != nil { |
| 51 |
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().("failed to construct go value: %w", err) |
| 52 |
} |
| 53 |
if err := encoder.Encodescip-go gomod github.com/pelletier/go-toml/v2 v2.4.3 `github.com/pelletier/go-toml/v2`/Encoder#Encode().(goValue); err != nil { |
| 54 |
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().("toml encode failed: %w", err) |
| 55 |
} |
| 56 |
outBytes = buf.Bytesscip-go gomod github.com/golang/go/src go1.25 bytes/Buffer#Bytes().() |
| 57 |
} else { |
| 58 |
// handle scalars, slices, etc. |
| 59 |
var err error |
| 60 |
outBytes, err = encodeRootValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/encodeRootValue().(encoder, &buf, value) |
| 61 |
if err != nil { |
| 62 |
return nil, err |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
// Ensure trailing newline for consistency with other format writers/tests. |
| 67 |
if len(outBytes) == 0 || outBytes[len(outBytes)-1] != '\n' { |
| 68 |
outBytes = append(outBytes, '\n') |
| 69 |
} |
| 70 |
|
| 71 |
return outBytes, nil |
| 72 |
} |
| 73 |
|
| •74 |
// rootValueKey is the placeholder key used when encoding a non-table document root. |
| 75 |
const rootValueKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/rootValueKey. = "v" |
| 76 |
|
| 77 |
// encodeRootValue encodes a top level value that isn't a map, e.g. the result of |
| 78 |
// selecting a single scalar or list out of a document. |
| 79 |
// A TOML document root must be a table, so go-toml will only encode these values |
| 80 |
// as part of a key/value pair. We encode against a placeholder key and strip the |
| 81 |
// `<key> = ` prefix back off, leaving go-toml in charge of formatting the value. |
| 82 |
func encodeRootValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/encodeRootValue().(encoder *pkgscip-go gomod github.com/pelletier/go-toml/v2 v2.4.3 `github.com/pelletier/go-toml/v2`/.Encoderscip-go gomod github.com/pelletier/go-toml/v2 v2.4.3 `github.com/pelletier/go-toml/v2`/Encoder#, buf *bytesscip-go gomod github.com/golang/go/src go1.25 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25 bytes/Buffer#, 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#) ([]byte, error) { |
| 83 |
typ, rv, err := goTypeAndValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/goTypeAndValue().(value) |
| 84 |
if err != nil { |
| 85 |
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().("failed to convert non-map top-level value: %w", err) |
| 86 |
} |
| 87 |
|
| 88 |
// A null root has no TOML representation at all. Pass the nil interface |
| 89 |
// through so go-toml reports it rather than silently writing nothing. |
| 90 |
if typ.Kindscip-go gomod github.com/golang/go/src go1.25 reflect/Type#Kind().() == reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Interfacescip-go gomod github.com/golang/go/src go1.25 reflect/Interface. && rv.IsZeroscip-go gomod github.com/golang/go/src go1.25 reflect/Value#IsZero().() { |
| 91 |
if err := encoder.Encodescip-go gomod github.com/pelletier/go-toml/v2 v2.4.3 `github.com/pelletier/go-toml/v2`/Encoder#Encode().(nil); err != nil { |
| 92 |
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().("toml encode failed: %w", err) |
| 93 |
} |
| 94 |
return buf.Bytesscip-go gomod github.com/golang/go/src go1.25 bytes/Buffer#Bytes().(), nil |
| 95 |
} |
| 96 |
|
| 97 |
// The inline option keeps maps and lists of maps as inline tables, rather than |
| 98 |
// go-toml emitting table headers built from the placeholder key. |
| 99 |
wrapperType := reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.StructOfscip-go gomod github.com/golang/go/src go1.25 reflect/StructOf().([]reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.StructFieldscip-go gomod github.com/golang/go/src go1.25 reflect/StructField#{{ |
| 100 |
Namescip-go gomod github.com/golang/go/src go1.25 reflect/StructField#Name.: "F0", |
| 101 |
Typescip-go gomod github.com/golang/go/src go1.25 reflect/StructField#Type.: typ, |
| 102 |
Tagscip-go gomod github.com/golang/go/src go1.25 reflect/StructField#Tag.: reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.StructTagscip-go gomod github.com/golang/go/src go1.25 reflect/StructTag#(fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25 fmt/Sprintf().(`toml:"%s,inline"`, rootValueKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/rootValueKey.)), |
| 103 |
}}) |
| 104 |
wrapper := reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Newscip-go gomod github.com/golang/go/src go1.25 reflect/New().(wrapperType).Elemscip-go gomod github.com/golang/go/src go1.25 reflect/Value#Elem().() |
| 105 |
wrapper.Fieldscip-go gomod github.com/golang/go/src go1.25 reflect/Value#Field().(0).Setscip-go gomod github.com/golang/go/src go1.25 reflect/Value#Set().(rv) |
| 106 |
|
| 107 |
if err := encoder.Encodescip-go gomod github.com/pelletier/go-toml/v2 v2.4.3 `github.com/pelletier/go-toml/v2`/Encoder#Encode().(wrapper.Interfacescip-go gomod github.com/golang/go/src go1.25 reflect/Value#Interface().()); err != nil { |
| 108 |
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().("toml encode failed: %w", err) |
| 109 |
} |
| 110 |
|
| 111 |
prefix := []byte(rootValueKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/rootValueKey. + " = ") |
| 112 |
out := buf.Bytesscip-go gomod github.com/golang/go/src go1.25 bytes/Buffer#Bytes().() |
| 113 |
if !bytesscip-go gomod github.com/golang/go/src go1.25 bytes/.HasPrefixscip-go gomod github.com/golang/go/src go1.25 bytes/HasPrefix().(out, prefix) { |
| 114 |
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().("toml encode failed: unexpected encoding of top-level value: %s", string(out)) |
| 115 |
} |
| 116 |
return out[len(prefix):], nil |
| 117 |
} |
| 118 |
|
| 119 |
// buildGoValueForMap constructs a reflect.Value that is a struct with fields in |
| 120 |
// the same order as keys in the provided map Value. The struct fields are |
| 121 |
// tagged with `toml:"<original key>"` so go-toml will use the original key |
| 122 |
// names when encoding. |
| 123 |
func buildGoValueForMapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/buildGoValueForMap().(v *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#) (interface{}, error) { |
| 124 |
kvs, err := v.MapKeyValuesscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#MapKeyValues().() |
| 125 |
if err != nil { |
| 126 |
return nil, err |
| 127 |
} |
| 128 |
|
| 129 |
// Build struct fields in order |
| 130 |
fields := make([]reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.StructFieldscip-go gomod github.com/golang/go/src go1.25 reflect/StructField#, 0, len(kvs)) |
| 131 |
fieldValues := make([]reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25 reflect/Value#, 0, len(kvs)) |
| 132 |
|
| 133 |
for i, kv := range kvs { |
| 134 |
ft, fv, err := goTypeAndValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/goTypeAndValue().(kv.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/KeyValue#Value.) |
| 135 |
if err != nil { |
| 136 |
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().("error converting key %q: %w", kv.Keyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/KeyValue#Key., err) |
| 137 |
} |
| 138 |
|
| 139 |
tomlTagContents := []string{kv.Keyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/KeyValue#Key.} |
| 140 |
|
| 141 |
// These currently do not take effect. They are placeholders for future functionality. |
| 142 |
if v, ok := kv.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/KeyValue#Value..MetadataValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#MetadataValue().(tomlStringStyleKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/tomlStringStyleKey.); ok { |
| 143 |
if style, ok := v.(string); ok && style != "" { |
| 144 |
switch style { |
| 145 |
case tomlStringStyleMultilineBasicscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/tomlStringStyleMultilineBasic., tomlStringStyleMultilineLiteralscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/tomlStringStyleMultilineLiteral.: |
| 146 |
tomlTagContents = append(tomlTagContents, "multiline") |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
if v, ok := kv.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/KeyValue#Value..MetadataValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#MetadataValue().(tomlTableStyleKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/tomlTableStyleKey.); ok { |
| 151 |
if style, ok := v.(string); ok && style != "" { |
| 152 |
switch style { |
| 153 |
case tomlTableStyleInlinescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/tomlTableStyleInline.: |
| 154 |
tomlTagContents = append(tomlTagContents, "inline") |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
// create exported field name (F0, F1...) and set tag to preserve toml key |
| 160 |
field := reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.StructFieldscip-go gomod github.com/golang/go/src go1.25 reflect/StructField#{ |
| 161 |
Namescip-go gomod github.com/golang/go/src go1.25 reflect/StructField#Name.: "F" + strconvscip-go gomod github.com/golang/go/src go1.25 strconv/.Itoascip-go gomod github.com/golang/go/src go1.25 strconv/Itoa().(i), |
| 162 |
Typescip-go gomod github.com/golang/go/src go1.25 reflect/StructField#Type.: ft, |
| 163 |
Tagscip-go gomod github.com/golang/go/src go1.25 reflect/StructField#Tag.: reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.StructTagscip-go gomod github.com/golang/go/src go1.25 reflect/StructTag#(fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25 fmt/Sprintf().(`toml:"%s"`, stringsscip-go gomod github.com/golang/go/src go1.25 strings/.Joinscip-go gomod github.com/golang/go/src go1.25 strings/Join().(tomlTagContents, ","))), |
| 164 |
} |
| 165 |
fields = append(fields, field) |
| 166 |
fieldValues = append(fieldValues, fv) |
| 167 |
} |
| 168 |
|
| 169 |
structType := reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.StructOfscip-go gomod github.com/golang/go/src go1.25 reflect/StructOf().(fields) |
| 170 |
val := reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Newscip-go gomod github.com/golang/go/src go1.25 reflect/New().(structType).Elemscip-go gomod github.com/golang/go/src go1.25 reflect/Value#Elem().() |
| 171 |
|
| 172 |
for i := range fieldValues { |
| 173 |
val.Fieldscip-go gomod github.com/golang/go/src go1.25 reflect/Value#Field().(i).Setscip-go gomod github.com/golang/go/src go1.25 reflect/Value#Set().(fieldValues[i]) |
| 174 |
} |
| 175 |
|
| 176 |
return val.Interfacescip-go gomod github.com/golang/go/src go1.25 reflect/Value#Interface().(), nil |
| 177 |
} |
| 178 |
|
| 179 |
// goTypeAndValue returns a reflect.Type and a reflect.Value suitable for use as a |
| 180 |
// struct field/type for the given model.Value. It converts nested maps into |
| 181 |
// reflect.StructOf types recursively (preserving key order), converts slices to |
| 182 |
// slices of appropriate element types when possible, or []interface{} otherwise. |
| 183 |
func goTypeAndValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/goTypeAndValue().(v *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#) (reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Typescip-go gomod github.com/golang/go/src go1.25 reflect/Type#, reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25 reflect/Value#, error) { |
| 184 |
switch v.Typescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Type().() { |
| 185 |
case modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.TypeStringscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeString.: |
| 186 |
s, err := v.StringValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#StringValue().() |
| 187 |
if err != nil { |
| 188 |
return nil, reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25 reflect/Value#{}, err |
| 189 |
} |
| 190 |
return reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.25 reflect/TypeOf().(""), reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25 reflect/ValueOf().(s), nil |
| 191 |
case modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.TypeIntscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeInt.: |
| 192 |
i, err := v.IntValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#IntValue().() |
| 193 |
if err != nil { |
| 194 |
return nil, reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25 reflect/Value#{}, err |
| 195 |
} |
| 196 |
// use int64 to be safe |
| 197 |
return reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.25 reflect/TypeOf().(int64(0)), reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25 reflect/ValueOf().(i), nil |
| 198 |
case modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.TypeFloatscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeFloat.: |
| 199 |
f, err := v.FloatValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#FloatValue().() |
| 200 |
if err != nil { |
| 201 |
return nil, reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25 reflect/Value#{}, err |
| 202 |
} |
| 203 |
return reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.25 reflect/TypeOf().(float64(0)), reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25 reflect/ValueOf().(f), nil |
| 204 |
case modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.TypeBoolscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeBool.: |
| 205 |
b, err := v.BoolValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#BoolValue().() |
| 206 |
if err != nil { |
| 207 |
return nil, reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25 reflect/Value#{}, err |
| 208 |
} |
| 209 |
return reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.25 reflect/TypeOf().(true), reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25 reflect/ValueOf().(b), nil |
| 210 |
case modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.TypeNullscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeNull.: |
| 211 |
// represent null as interface{}(nil) |
| 212 |
return reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.25 reflect/TypeOf().((*interface{})(nil)).Elemscip-go gomod github.com/golang/go/src go1.25 reflect/Type#Elem().(), reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Zeroscip-go gomod github.com/golang/go/src go1.25 reflect/Zero().(reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.25 reflect/TypeOf().((*interface{})(nil)).Elemscip-go gomod github.com/golang/go/src go1.25 reflect/Type#Elem().()), nil |
| 213 |
case modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.TypeMapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeMap.: |
| 214 |
// For nested maps, prefer map[string]interface{} values rather than |
| 215 |
// generating nested struct types. Using struct types for nested maps |
| 216 |
// causes go-toml to emit explicit table headers for those nested |
| 217 |
// structures which can change the document shape on round-trip. The |
| 218 |
// top-level map is still handled by buildGoValueForMap which generates |
| 219 |
// a struct to preserve ordering. |
| 220 |
kvs, err := v.MapKeyValuesscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#MapKeyValues().() |
| 221 |
if err != nil { |
| 222 |
return nil, reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25 reflect/Value#{}, err |
| 223 |
} |
| 224 |
m := make(map[string]interface{}) |
| 225 |
for _, kv := range kvs { |
| 226 |
_, rv, err := goTypeAndValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/goTypeAndValue().(kv.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/KeyValue#Value.) |
| 227 |
if err != nil { |
| 228 |
return nil, reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25 reflect/Value#{}, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("error in nested key %q: %w", kv.Keyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/KeyValue#Key., err) |
| 229 |
} |
| 230 |
m[kv.Keyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/KeyValue#Key.] = rv.Interfacescip-go gomod github.com/golang/go/src go1.25 reflect/Value#Interface().() |
| 231 |
} |
| 232 |
return reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.25 reflect/TypeOf().(map[string]interface{}{}), reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25 reflect/ValueOf().(m), nil |
| 233 |
case modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.TypeSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeSlice.: |
| 234 |
// Decide element types. If all elements are maps and have compatible keys, |
| 235 |
// build a struct type for elements and return a slice of that struct type. |
| 236 |
length, _ := v.SliceLenscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SliceLen().() |
| 237 |
if length == 0 { |
| 238 |
// empty slice -> use []interface{} |
| 239 |
elemType := reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.25 reflect/TypeOf().((*interface{})(nil)).Elemscip-go gomod github.com/golang/go/src go1.25 reflect/Type#Elem().() |
| 240 |
return reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.SliceOfscip-go gomod github.com/golang/go/src go1.25 reflect/SliceOf().(elemType), reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.MakeSlicescip-go gomod github.com/golang/go/src go1.25 reflect/MakeSlice().(reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.SliceOfscip-go gomod github.com/golang/go/src go1.25 reflect/SliceOf().(elemType), 0, 0), nil |
| 241 |
} |
| 242 |
|
| 243 |
// inspect first element |
| 244 |
first, _ := v.GetSliceIndexscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#GetSliceIndex().(0) |
| 245 |
if first.IsMapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#IsMap().() { |
| 246 |
// Collect union of keys across elements in order of appearance |
| 247 |
seen := map[string]bool{} |
| 248 |
keys := make([]string, 0) |
| 249 |
_ = v.RangeSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#RangeSlice().(func(_ int, item *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 { |
| 250 |
if !item.IsMapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#IsMap().() { |
| 251 |
// mixed types -> fallback to []interface{} |
| 252 |
keys = nil |
| 253 |
return nil |
| 254 |
} |
| 255 |
kvs, _ := item.MapKeyValuesscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#MapKeyValues().() |
| 256 |
for _, kv := range kvs { |
| 257 |
if !seen[kv.Keyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/KeyValue#Key.] { |
| 258 |
seen[kv.Keyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/KeyValue#Key.] = true |
| 259 |
keys = append(keys, kv.Keyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/KeyValue#Key.) |
| 260 |
} |
| 261 |
} |
| 262 |
return nil |
| 263 |
}) |
| 264 |
|
| 265 |
if keys != nil { |
| 266 |
// Build []map[string]interface{} to preserve keys and values without |
| 267 |
// forcing a generated struct type which can cause conversion issues. |
| 268 |
sliceMaps := make([]map[string]interface{}, 0, length) |
| 269 |
_ = v.RangeSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#RangeSlice().(func(_ int, item *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 { |
| 270 |
m := map[string]interface{}{} |
| 271 |
if item.IsMapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#IsMap().() { |
| 272 |
kvs, _ := item.MapKeyValuesscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#MapKeyValues().() |
| 273 |
for _, kv := range kvs { |
| 274 |
_, rv, err := goTypeAndValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/goTypeAndValue().(kv.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/KeyValue#Value.) |
| 275 |
if err != nil { |
| 276 |
return err |
| 277 |
} |
| 278 |
m[kv.Keyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/KeyValue#Key.] = rv.Interfacescip-go gomod github.com/golang/go/src go1.25 reflect/Value#Interface().() |
| 279 |
} |
| 280 |
} |
| 281 |
sliceMaps = append(sliceMaps, m) |
| 282 |
return nil |
| 283 |
}) |
| 284 |
return reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.25 reflect/TypeOf().([]map[string]interface{}{}), reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25 reflect/ValueOf().(sliceMaps), nil |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
// fallback: build []interface{} |
| 289 |
elems := make([]interface{}, 0, length) |
| 290 |
_ = v.RangeSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#RangeSlice().(func(_ int, item *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 { |
| 291 |
gt, rv, err := goTypeAndValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/toml`/goTypeAndValue().(item) |
| 292 |
if err != nil { |
| 293 |
return err |
| 294 |
} |
| 295 |
// convert reflect.Value to interface{} |
| 296 |
elems = append(elems, rv.Interfacescip-go gomod github.com/golang/go/src go1.25 reflect/Value#Interface().()) |
| 297 |
_ = gt |
| 298 |
return nil |
| 299 |
}) |
| 300 |
return reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.25 reflect/TypeOf().([]interface{}{}), reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25 reflect/ValueOf().(elems), nil |
| 301 |
default: |
| 302 |
// fallback to stringified interface |
| 303 |
s := fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25 fmt/Sprintf().("%v", v.Interfacescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Interface().()) |
| 304 |
return reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.25 reflect/TypeOf().(""), reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25 reflect/ValueOf().(s), nil |
| 305 |
} |
| 306 |
} |