Skip to content

Commit

Permalink
fix: type mismatch in replacing implicit dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
SparkYuan committed Aug 24, 2023
1 parent 5fb4e61 commit 20251b9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
20 changes: 10 additions & 10 deletions pkg/engine/operation/graph/resource_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,9 @@ func ReplaceImplicitRef(
func ReplaceRef(
v reflect.Value,
resourceIndex map[string]*models.Resource,
replaceImplicitDependencyFun func(map[string]*models.Resource, string) (reflect.Value, status.Status),
repImplDepFunc func(map[string]*models.Resource, string) (reflect.Value, status.Status),
ss *vals.SecretStores,
replaceSecretFun func(string, string, *vals.SecretStores) (string, error),
repSecretFunc func(string, string, *vals.SecretStores) (string, error),
) ([]string, reflect.Value, status.Status) {
var result []string
if !v.IsValid() {
Expand All @@ -335,29 +335,29 @@ func ReplaceRef(
if v.IsNil() {
return nil, v, nil
}
return ReplaceRef(v.Elem(), resourceIndex, replaceImplicitDependencyFun, ss, replaceSecretFun)
return ReplaceRef(v.Elem(), resourceIndex, repImplDepFunc, ss, repSecretFunc)
case reflect.String:
vStr := v.String()
if replaceImplicitDependencyFun != nil {
if repImplDepFunc != nil {
if strings.HasPrefix(vStr, ImplicitRefPrefix) {
ref := strings.TrimPrefix(vStr, ImplicitRefPrefix)
util.CheckArgument(len(ref) > 0,
fmt.Sprintf("illegal implicit ref:%s. Implicit ref format: %sresourceKey.attribute", ref, ImplicitRefPrefix))
split := strings.Split(ref, ".")
result = append(result, split[0])
log.Infof("add implicit ref:%s", split[0])
// replace v with output
tv, s := replaceImplicitDependencyFun(resourceIndex, ref)
// replace ref with actual value
tv, s := repImplDepFunc(resourceIndex, ref)
if status.IsErr(s) {
return nil, v, s
}
v = tv
}
}

if ss != nil && replaceSecretFun != nil {
if ss != nil && repSecretFunc != nil {
if prefix, ok := vals.IsSecured(vStr); ok {
tStr, err := replaceSecretFun(prefix, vStr, ss)
tStr, err := repSecretFunc(prefix, vStr, ss)
if err != nil {
return nil, v, status.NewErrorStatus(err)
}
Expand All @@ -374,7 +374,7 @@ func ReplaceRef(
vs := reflect.MakeSlice(v.Type(), 0, 0)

for i := 0; i < v.Len(); i++ {
ref, tv, s := ReplaceRef(v.Index(i), resourceIndex, replaceImplicitDependencyFun, ss, replaceSecretFun)
ref, tv, s := ReplaceRef(v.Index(i), resourceIndex, repImplDepFunc, ss, repSecretFunc)
if status.IsErr(s) {
return nil, tv, s
}
Expand All @@ -392,7 +392,7 @@ func ReplaceRef(

iter := v.MapRange()
for iter.Next() {
ref, tv, s := ReplaceRef(iter.Value(), resourceIndex, replaceImplicitDependencyFun, ss, replaceSecretFun)
ref, tv, s := ReplaceRef(iter.Value(), resourceIndex, repImplDepFunc, ss, repSecretFunc)
if status.IsErr(s) {
return nil, tv, s
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/engine/operation/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ func updateDependencies(resource *models.Resource) ([]string, status.Status) {

// handle implicit dependency
v := reflect.ValueOf(resource.Attributes)
implicitRefKeys, _, s := graph.ReplaceImplicitRef(v, nil, func(map[string]*models.Resource, string) (reflect.Value, status.Status) {
return v, nil
implicitRefKeys, _, s := graph.ReplaceImplicitRef(v, nil, func(
res map[string]*models.Resource,
ref string,
) (reflect.Value, status.Status) {
// don't replace anything when parsing dependencies
return reflect.ValueOf(ref), nil
})
if status.IsErr(s) {
return nil, s
Expand Down

0 comments on commit 20251b9

Please sign in to comment.