Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Minor refactors and fix linter errors
Browse files Browse the repository at this point in the history
Signed-off-by: Hasan Turken <[email protected]>
  • Loading branch information
turkenh committed Sep 30, 2021
1 parent 17cfaca commit 8163825
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
26 changes: 14 additions & 12 deletions pkg/json/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"strconv"
"strings"

"github.com/pkg/errors"

jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
)

const (
Expand All @@ -20,6 +19,11 @@ const (
errFmtCannotExpandForObject = "cannot expand wildcard for object with paths %v"
)

// ValuesMatchingPaths returns values matching provided field paths in the input
// data. Field paths are dot separated strings where numbers representing
// indexes in arrays, strings representing key for maps and "*" will act as a
// wildcard mapping to each element of array or each key of map.
// See the unit tests for examples.
func ValuesMatchingPaths(data []byte, fieldPaths []string) (map[string][]byte, error) {
vals := make(map[string][]byte)
for _, fp := range fieldPaths {
Expand All @@ -35,16 +39,14 @@ func ValuesMatchingPaths(data []byte, fieldPaths []string) (map[string][]byte, e
}

func valuesMatchingPath(data []byte, fieldPath string) (map[string][]byte, error) {
api := jsoniter.ConfigDefault

keys, err := keysForFieldPath(api, data, fieldPath)
keys, err := keysForFieldPath(data, fieldPath)
if err != nil {
return nil, errors.Wrap(err, errCannotExpandWildcards)
}

res := make(map[string][]byte, len(keys))
for _, k := range keys {
v, err := value(api.Get(data, k...))
v, err := value(jsoniter.Get(data, k...))
if err != nil {
return nil, errors.Wrapf(err, errFmtCannotGetValueForPath, k)
}
Expand All @@ -68,8 +70,8 @@ func valuesMatchingPath(data []byte, fieldPath string) (map[string][]byte, error
return res, nil
}

func keysForFieldPath(api jsoniter.API, data []byte, fieldPath string) ([][]interface{}, error) {
d := api.Get(data)
func keysForFieldPath(data []byte, fieldPath string) ([][]interface{}, error) {
d := jsoniter.Get(data)
fps := strings.Split(fieldPath, ".")

fpi := make([]interface{}, len(fps))
Expand All @@ -81,10 +83,10 @@ func keysForFieldPath(api jsoniter.API, data []byte, fieldPath string) ([][]inte
}
fpi[i] = f
}
return expandWildcard(d, fpi)
return expandWildcards(d, fpi)
}

func expandWildcard(a jsoniter.Any, paths []interface{}) ([][]interface{}, error) {
func expandWildcards(a jsoniter.Any, paths []interface{}) ([][]interface{}, error) { // nolint:gocyclo
var res [][]interface{}

for i, v := range paths {
Expand All @@ -96,7 +98,7 @@ func expandWildcard(a jsoniter.Any, paths []interface{}) ([][]interface{}, error
np := make([]interface{}, len(paths))
copy(np, paths)
np = append(append(np[:i], j), np[i+1:]...)
r, err := expandWildcard(a, np)
r, err := expandWildcards(a, np)
if err != nil {
return nil, errors.Wrapf(err, errFmtCannotExpandForArray, np)
}
Expand All @@ -107,7 +109,7 @@ func expandWildcard(a jsoniter.Any, paths []interface{}) ([][]interface{}, error
np := make([]interface{}, len(paths))
copy(np, paths)
np = append(append(np[:i], k), np[i+1:]...)
r, err := expandWildcard(a, np)
r, err := expandWildcards(a, np)
if err != nil {
return nil, errors.Wrapf(err, errFmtCannotExpandForObject, np)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/types/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (g *Builder) buildResource(res *schema.Resource, cfg *config.Resource, tfPa
// Data will be loaded from the referenced secret key.
fieldNameCamel += "SecretRef"
// todo(hasan): do we need the pointer type if optional?
fieldType = typeXPSecretKeySelector
fieldType = typeSecretKeySelector

jsonTag += "SecretRef"
tfTag = "-"
Expand Down
4 changes: 2 additions & 2 deletions pkg/types/reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (

var typeReferenceField types.Type
var typeSelectorField types.Type
var typeXPSecretKeySelector types.Type
var typeSecretKeySelector types.Type
var commentOptional *comments.Comment

func (g *Builder) generateReferenceFields(t *types.TypeName, f *types.Var, r config.Reference) (fields []*types.Var, tags []string) {
Expand Down Expand Up @@ -72,7 +72,7 @@ func init() {
typeReferenceField = pkgs[0].Types.Scope().Lookup("Reference").Type()
typeSelectorField = pkgs[0].Types.Scope().Lookup("Selector").Type()
// todo(hasan): do we need a `LocalSecretKeySelector` instead?
typeXPSecretKeySelector = pkgs[0].Types.Scope().Lookup("SecretKeySelector").Type()
typeSecretKeySelector = pkgs[0].Types.Scope().Lookup("SecretKeySelector").Type()

commentOptional, err = comments.New("")
if err != nil {
Expand Down

0 comments on commit 8163825

Please sign in to comment.