-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also: * Add more ARD copy functions * Remove timestamp
- Loading branch information
Showing
23 changed files
with
266 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.