Skip to content

Commit

Permalink
Rename "format" to "transcribe"
Browse files Browse the repository at this point in the history
Also:

* Add more ARD copy functions
* Remove timestamp
  • Loading branch information
tliron committed Jun 9, 2022
1 parent 422d5ad commit f5ef522
Show file tree
Hide file tree
Showing 23 changed files with 266 additions and 137 deletions.
114 changes: 109 additions & 5 deletions ard/copy.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,132 @@
package ard

func Copy(value Value) Value {
import (
"bytes"

"github.com/fxamacker/cbor/v2"
"github.com/tliron/yamlkeys"
"gopkg.in/yaml.v3"
)

func AgnosticCopy(value Value) (Value, error) {
if IsPrimitiveType(value) {
return value, nil
} else {
var err error
switch value_ := value.(type) {
case Map:
map_ := make(Map)
for key, value_ := range value_ {
if map_[key], err = AgnosticCopy(value_); err != nil {
return nil, err
}
}
return map_, nil

case StringMap:
map_ := make(StringMap)
for key, value_ := range value_ {
if map_[key], err = AgnosticCopy(value_); err != nil {
return nil, err
}
}
return map_, nil

case List:
list := make(List, len(value_))
for index, entry := range value_ {
if list[index], err = AgnosticCopy(entry); err != nil {
return nil, err
}
}
return list, nil

default:
// TODO: not very efficient
return AgnosticCopyThroughCBOR(value)
}
}
}

func AgnosticCopyThroughCBOR(value Value) (Value, error) {
if code, err := cbor.Marshal(value); err == nil {
var value_ Value
if err := cbor.Unmarshal(code, &value_); err == nil {
return value_, nil
} else {
return nil, err
}
} else {
return nil, err
}
}

func AgnosticCopyThroughYAML(value Value) (Value, error) {
if code, err := yaml.Marshal(value); err == nil {
if value, err := yamlkeys.Decode(bytes.NewReader(code)); err == nil {
return value, nil
} else {
return nil, err
}
} else {
return nil, err
}
}

func NormalizeMapsAgnosticCopy(value Value) (Value, error) {
if value_, err := AgnosticCopy(value); err == nil {
value_, _ = NormalizeMaps(value_)
return value_, nil
} else {
return nil, err
}
}

func NormalizeStringMapsAgnosticCopy(value Value) (Value, error) {
if value_, err := AgnosticCopy(value); err == nil {
value_, _ = NormalizeStringMaps(value_)
return value_, nil
} else {
return nil, err
}
}

func SimpleCopy(value Value) Value {
switch value_ := value.(type) {
case Map:
map_ := make(Map)
for key, value_ := range value_ {
map_[key] = Copy(value_)
map_[key] = SimpleCopy(value_)
}
return map_

case StringMap:
map_ := make(StringMap)
for key, value_ := range value_ {
map_[key] = Copy(value_)
map_[key] = SimpleCopy(value_)
}
return map_

case List:
list := make(List, len(value_))
for index, entry := range value_ {
list[index] = Copy(entry)
list[index] = SimpleCopy(entry)
}
return list

default:
return value
}
}

func NormalizeMapsSimpleCopy(value Value) Value {
value_ := SimpleCopy(value)
value_, _ = NormalizeMaps(value_)
return value_
}

return value
func NormalizeStringMapsSimpleCopy(value Value) Value {
value_ := SimpleCopy(value)
value_, _ = NormalizeStringMaps(value_)
return value_
}
43 changes: 3 additions & 40 deletions ard/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,18 @@ import (
"github.com/tliron/yamlkeys"
)

func MapsToStringMaps(value Value) (Value, bool) {
switch value_ := value.(type) {
case Map:
return MapToStringMap(value_), true

case StringMap:
changedStringMap := make(StringMap)
changed := false
for key, element := range value_ {
var changed_ bool
if element, changed_ = MapsToStringMaps(element); changed_ {
changed = true
}
changedStringMap[key] = element
}
if changed {
return changedStringMap, true
}

case List:
changedList := make(List, len(value_))
changed := false
for index, element := range value_ {
var changed_ bool
if element, changed_ = MapsToStringMaps(element); changed_ {
changed = true
}
changedList[index] = element
}
if changed {
return changedList, true
}
}

return value, false
}

// Ensure data adheres to map[string]any
// (JSON encoding does not support map[any]any)
func EnsureStringMaps(stringMap StringMap) StringMap {
stringMap_, _ := MapsToStringMaps(stringMap)
stringMap_, _ := NormalizeStringMaps(stringMap)
return stringMap_.(StringMap)
}

// Recursive
func StringMapToMap(stringMap StringMap) Map {
map_ := make(Map)
for key, value := range stringMap {
map_[key], _ = Normalize(value)
map_[key], _ = NormalizeMaps(value)
}
return map_
}
Expand All @@ -61,7 +24,7 @@ func StringMapToMap(stringMap StringMap) Map {
func MapToStringMap(map_ Map) StringMap {
stringMap := make(StringMap)
for key, value := range map_ {
stringMap[yamlkeys.KeyString(key)], _ = MapsToStringMaps(value)
stringMap[yamlkeys.KeyString(key)], _ = NormalizeStringMaps(value)
}
return stringMap
}
4 changes: 2 additions & 2 deletions ard/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func MergeMaps(target Map, source Map, mergeLists bool) {
}
}

target[key] = Copy(sourceValue)
target[key] = SimpleCopy(sourceValue)
}
}

Expand All @@ -49,7 +49,7 @@ func MergeStringMaps(target StringMap, source StringMap, mergeLists bool) {
}
}

target[key] = Copy(sourceValue)
target[key] = SimpleCopy(sourceValue)
}
}

Expand Down
45 changes: 41 additions & 4 deletions ard/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ard

// Ensure data adheres to the ARD map type
// (JSON decoding uses map[string]any instead of map[any]any)
func Normalize(value Value) (Value, bool) {
func NormalizeMaps(value Value) (Value, bool) {
switch value_ := value.(type) {
case StringMap:
return StringMapToMap(value_), true
Expand All @@ -12,10 +12,10 @@ func Normalize(value Value) (Value, bool) {
changed := false
for key, element := range value_ {
var changedKey bool
key, changedKey = Normalize(key)
key, changedKey = NormalizeMaps(key)

var changedElement bool
element, changedElement = Normalize(element)
element, changedElement = NormalizeMaps(element)

if changedKey || changedElement {
changed = true
Expand All @@ -32,7 +32,44 @@ func Normalize(value Value) (Value, bool) {
changed := false
for index, element := range value_ {
var changed_ bool
if element, changed_ = Normalize(element); changed_ {
if element, changed_ = NormalizeMaps(element); changed_ {
changed = true
}
changedList[index] = element
}
if changed {
return changedList, true
}
}

return value, false
}

func NormalizeStringMaps(value Value) (Value, bool) {
switch value_ := value.(type) {
case Map:
return MapToStringMap(value_), true

case StringMap:
changedStringMap := make(StringMap)
changed := false
for key, element := range value_ {
var changed_ bool
if element, changed_ = NormalizeStringMaps(element); changed_ {
changed = true
}
changedStringMap[key] = element
}
if changed {
return changedStringMap, true
}

case List:
changedList := make(List, len(value_))
changed := false
for index, element := range value_ {
var changed_ bool
if element, changed_ = NormalizeStringMaps(element); changed_ {
changed = true
}
changedList[index] = element
Expand Down
2 changes: 1 addition & 1 deletion ard/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func ReadJSON(reader io.Reader, locate bool) (Value, Locator, error) {
decoder := json.NewDecoder(reader)
if err := decoder.Decode(&value); err == nil {
// The JSON decoder uses StringMaps, not Maps
value, _ := Normalize(value)
value, _ := NormalizeMaps(value)
return value, nil, nil
} else {
return nil, nil, err
Expand Down
19 changes: 19 additions & 0 deletions ard/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,22 @@ func IsTimestamp(value Value) bool {
_, ok := value.(time.Time)
return ok
}

func IsPrimitiveType(value Value) bool {
switch value.(type) {
case string:
return true
case bool:
return true
case int64, int32, int16, int8, int, uint64, uint32, uint16, uint8, uint:
return true
case float64, float32:
return true
case nil:
return true
case time.Time:
return true
default:
return false
}
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ require (
github.com/go-git/go-git/v5 v5.4.2
github.com/goccy/go-yaml v1.9.5
github.com/google/go-containerregistry v0.9.0
github.com/hashicorp/go-hclog v1.2.0
github.com/hashicorp/go-hclog v1.2.1
github.com/hashicorp/memberlist v0.3.1
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f
github.com/klauspost/pgzip v1.2.5
github.com/kortschak/utter v1.5.0
github.com/mitchellh/hashstructure/v2 v2.0.2
github.com/rs/zerolog v1.26.1
github.com/rs/zerolog v1.27.0
github.com/sasha-s/go-deadlock v0.3.1
github.com/segmentio/ksuid v1.0.4
github.com/spf13/cobra v1.4.0
Expand Down Expand Up @@ -81,7 +81,7 @@ require (
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/klauspost/compress v1.15.4 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mattn/go-colorable v0.1.11 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/miekg/dns v1.1.35 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
Expand Down
Loading

0 comments on commit f5ef522

Please sign in to comment.