Skip to content

Commit

Permalink
Improvements to letter case, ARD, URL
Browse files Browse the repository at this point in the history
* Use []byte for internal URLs
* Use iancoleman/strcase for letter case
  • Loading branch information
tliron committed Jun 27, 2022
1 parent 185513f commit 7a633dc
Show file tree
Hide file tree
Showing 17 changed files with 161 additions and 159 deletions.
52 changes: 10 additions & 42 deletions ard/copy.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
package ard

import (
"bytes"

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

func AgnosticCopy(value Value) (Value, error) {
func CopyToARD(value Value) (Value, error) {
if IsPrimitiveType(value) {
return value, nil
} else {
Expand All @@ -17,7 +9,7 @@ func AgnosticCopy(value Value) (Value, error) {
case Map:
map_ := make(Map)
for key, value_ := range value_ {
if map_[key], err = AgnosticCopy(value_); err != nil {
if map_[key], err = CopyToARD(value_); err != nil {
return nil, err
}
}
Expand All @@ -26,7 +18,7 @@ func AgnosticCopy(value Value) (Value, error) {
case StringMap:
map_ := make(StringMap)
for key, value_ := range value_ {
if map_[key], err = AgnosticCopy(value_); err != nil {
if map_[key], err = CopyToARD(value_); err != nil {
return nil, err
}
}
Expand All @@ -35,62 +27,38 @@ func AgnosticCopy(value Value) (Value, error) {
case List:
list := make(List, len(value_))
for index, entry := range value_ {
if list[index], err = AgnosticCopy(entry); err != nil {
if list[index], err = CopyToARD(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
return RoundtripCBOR(value)
}
} else {
return nil, err
}
}

func NormalizeMapsAgnosticCopy(value Value) (Value, error) {
if value_, err := AgnosticCopy(value); err == nil {
func NormalizeMapsCopyToARD(value Value) (Value, error) {
if value_, err := CopyToARD(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 {
func NormalizeStringMapsCopyToARD(value Value) (Value, error) {
if value_, err := CopyToARD(value); err == nil {
value_, _ = NormalizeStringMaps(value_)
return value_, nil
} else {
return nil, err
}
}

// Will leave non-ARD types as is
func SimpleCopy(value Value) Value {
switch value_ := value.(type) {
case Map:
Expand Down
34 changes: 25 additions & 9 deletions ard/reflection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@ package ard
import (
"fmt"
"reflect"
"sync"
"time"

"github.com/tliron/kutil/reflection"
)

type StructFieldNameMapperFunc func(fieldName string) string

//
// Reflector
//

type Reflector struct {
IgnoreMissingStructFields bool
NilMeansZero bool
StructFieldNameMapper StructFieldNameMapperFunc

fieldNamesCache sync.Map
}

func NewReflector() *Reflector {
return &Reflector{}
return new(Reflector)
}

func (self *Reflector) ToComposite(value Value, compositeValuePtr any) error {
Expand Down Expand Up @@ -134,7 +140,7 @@ func (self *Reflector) ToCompositeReflect(value Value, compositeValue reflect.Va
}

case reflect.Struct:
fieldNames := NewFieldNames(compositeType)
fieldNames := self.NewFieldNames(compositeType)
for k, v := range value_ {
if k_, ok := k.(string); ok {
if err := self.setStructField(compositeValue, k_, v, fieldNames); err != nil {
Expand Down Expand Up @@ -173,7 +179,7 @@ func (self *Reflector) ToCompositeReflect(value Value, compositeValue reflect.Va
}

case reflect.Struct:
fieldNames := NewFieldNames(compositeType)
fieldNames := self.NewFieldNames(compositeType)
for k, v := range value_ {
if err := self.setStructField(compositeValue, k, v, fieldNames); err != nil {
return err
Expand Down Expand Up @@ -249,7 +255,7 @@ func (self *Reflector) FromCompositeReflect(compositeValue reflect.Value) (Value

case reflect.Struct:
map_ := make(Map)
fieldNames := NewFieldNames(compositeType)
fieldNames := self.NewFieldNames(compositeType)
for name, fieldName := range fieldNames {
elem := compositeValue.FieldByName(fieldName)
if elem_, err := self.FromCompositeReflect(elem); err == nil {
Expand Down Expand Up @@ -292,24 +298,34 @@ func (self *Reflector) setStructField(structValue reflect.Value, fieldName strin

type FieldNames map[string]string // ARD name to struct field name

func NewFieldNames(type_ reflect.Type) FieldNames {
self := make(FieldNames)
func (self *Reflector) NewFieldNames(type_ reflect.Type) FieldNames {
if v, ok := self.fieldNamesCache.Load(type_); ok {
return v.(FieldNames)
}

fieldNames := make(FieldNames)
tags := reflection.GetFieldTagsForType(type_, "ard")

// Tagged fields
for fieldName, tag := range tags {
self[tag] = fieldName
fieldNames[tag] = fieldName
}

// Untagged fields
for _, field := range reflection.GetStructFields(type_) {
fieldName := field.Name
if _, ok := tags[fieldName]; !ok {
self[fieldName] = fieldName
if self.StructFieldNameMapper != nil {
fieldNames[self.StructFieldNameMapper(fieldName)] = fieldName
} else {
fieldNames[fieldName] = fieldName
}
}
}

return self
self.fieldNamesCache.Store(type_, fieldNames)

return fieldNames
}

func (self FieldNames) GetField(structValue reflect.Value, name string) reflect.Value {
Expand Down
25 changes: 13 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,30 @@ require (
github.com/fxamacker/cbor/v2 v2.4.0
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/google/go-containerregistry v0.10.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/iancoleman/strcase v0.2.0
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.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
github.com/spf13/cobra v1.5.0
github.com/spf13/pflag v1.0.5
github.com/tliron/yamlkeys v1.3.5
github.com/zchee/color/v2 v2.0.6
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467
google.golang.org/protobuf v1.28.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.24.1
k8s.io/apiextensions-apiserver v0.24.1
k8s.io/apimachinery v0.24.1
k8s.io/client-go v0.24.1
k8s.io/klog/v2 v2.60.1
k8s.io/api v0.24.2
k8s.io/apiextensions-apiserver v0.24.2
k8s.io/apimachinery v0.24.2
k8s.io/client-go v0.24.2
k8s.io/klog/v2 v2.70.0
)

require (
Expand Down Expand Up @@ -99,13 +100,13 @@ require (
github.com/vbatts/tar-split v0.11.2 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
golang.org/x/net v0.0.0-20220516155154-20f960328961 // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
golang.org/x/net v0.0.0-20220524220425-1d687d428aca // indirect
golang.org/x/oauth2 v0.0.0-20220524215830-622c5d57e401 // indirect
golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 // indirect
golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df // indirect
google.golang.org/appengine v1.6.7 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
Expand Down
Loading

0 comments on commit 7a633dc

Please sign in to comment.