mirror of
https://github.com/johnkerl/miller.git
synced 2026-07-17 16:38:54 +00:00
* Comment style * IRecordTransformer -> RecordTransfomer * make fmt * else-return style mod * snake-case -> camel-case * Remove redundant err = nil and similar zero-value initializations. * redundant break; * bugfix * neaten * typofix * simplify/standardize init of zero-length slices * Standardize fmt.Fprintf w/ errors * fix double print of "mlr:" * neatening * Uniformize error messages * make docs * avoid shadowing package names * shorten some receiver names
88 lines
2.2 KiB
Go
88 lines
2.2 KiB
Go
package mlrval
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/johnkerl/miller/v6/pkg/lib"
|
|
)
|
|
|
|
func (mv *Mlrval) GetArrayLength() (int, bool) {
|
|
if mv.IsArray() {
|
|
return len(mv.intf.([]*Mlrval)), true
|
|
}
|
|
return -999, false
|
|
}
|
|
|
|
func CopyMlrvalArray(input []*Mlrval) []*Mlrval {
|
|
output := make([]*Mlrval, len(input))
|
|
for i, element := range input {
|
|
if element == nil {
|
|
output[i] = nil
|
|
} else {
|
|
output[i] = element.Copy()
|
|
}
|
|
}
|
|
return output
|
|
}
|
|
|
|
// For the flatten verb and DSL function.
|
|
|
|
func (mv *Mlrval) FlattenToMap(prefix string, delimiter string) Mlrval {
|
|
retval := NewMlrmap()
|
|
|
|
if mv.IsMap() {
|
|
// Without this, the for-loop below is zero-pass and fields with "{}"
|
|
// values would disappear entirely in a JSON-to-CSV conversion.
|
|
if mv.intf.(*Mlrmap).IsEmpty() {
|
|
if prefix != "" {
|
|
retval.PutCopy(prefix, FromString("{}"))
|
|
}
|
|
}
|
|
|
|
for pe := mv.intf.(*Mlrmap).Head; pe != nil; pe = pe.Next {
|
|
nextPrefix := pe.Key
|
|
if prefix != "" {
|
|
nextPrefix = prefix + delimiter + nextPrefix
|
|
}
|
|
if pe.Value.IsMap() || pe.Value.IsArray() {
|
|
nextResult := pe.Value.FlattenToMap(nextPrefix, delimiter)
|
|
lib.InternalCodingErrorIf(nextResult.mvtype != MT_MAP)
|
|
for pf := nextResult.intf.(*Mlrmap).Head; pf != nil; pf = pf.Next {
|
|
retval.PutCopy(pf.Key, pf.Value.Copy())
|
|
}
|
|
} else {
|
|
retval.PutCopy(nextPrefix, pe.Value.Copy())
|
|
}
|
|
}
|
|
|
|
} else if mv.IsArray() {
|
|
// Without this, the for-loop below is zero-pass and fields with "[]"
|
|
// values would disappear entirely in a JSON-to-CSV conversion.
|
|
if len(mv.intf.([]*Mlrval)) == 0 {
|
|
if prefix != "" {
|
|
retval.PutCopy(prefix, FromString("[]"))
|
|
}
|
|
}
|
|
|
|
for zindex, value := range mv.intf.([]*Mlrval) {
|
|
nextPrefix := strconv.Itoa(zindex + 1) // Miller user-space indices are 1-up
|
|
if prefix != "" {
|
|
nextPrefix = prefix + delimiter + nextPrefix
|
|
}
|
|
if value.IsMap() || value.IsArray() {
|
|
nextResult := value.FlattenToMap(nextPrefix, delimiter)
|
|
lib.InternalCodingErrorIf(nextResult.mvtype != MT_MAP)
|
|
for pf := nextResult.intf.(*Mlrmap).Head; pf != nil; pf = pf.Next {
|
|
retval.PutCopy(pf.Key, pf.Value.Copy())
|
|
}
|
|
} else {
|
|
retval.PutCopy(nextPrefix, value.Copy())
|
|
}
|
|
}
|
|
|
|
} else {
|
|
retval.PutCopy(prefix, mv.Copy())
|
|
}
|
|
|
|
return *FromMap(retval)
|
|
}
|