Skip to content

Commit

Permalink
fix reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineJac committed Nov 28, 2024
1 parent 00e7c4a commit d814d2f
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"reflect"
"sync"
"log"

"github.com/blang/semver/v4"
"github.com/kong/deck/utils"
Expand Down Expand Up @@ -159,10 +160,10 @@ func (v *Validator) Validate(formatVersion semver.Version) []error {

// Create a copy of entityMap with only the specififed resources to check online.
filteredEntityMap := make(map[string]string)
if v.onlineEntitiesFilter != nil {
if len(v.onlineEntitiesFilter) > 0 {
for _, value := range v.onlineEntitiesFilter {
for key, entityName := range EntityMap {
if value == entityName {
if value == key {
filteredEntityMap[key] = entityName
}
}
Expand All @@ -171,20 +172,27 @@ func (v *Validator) Validate(formatVersion semver.Version) []error {
// If no filter is specified, use the original entityMap.
filteredEntityMap = EntityMap
}


// Validate each entity using the filtered entityMap
for fieldName, entityName := range filteredEntityMap {
// Use reflection to get the value of the field from v.state
fieldValue := reflect.ValueOf(v.state).FieldByName(fieldName)
if fieldValue.IsValid() && fieldValue.CanInterface() {
if err := v.entities(fieldValue.Interface(), entityName); err != nil {
allErr = append(allErr, err...)
}
} else {
allErr = append(allErr, fmt.Errorf("invalid field '%s' in state", fieldName))
}
}
for fieldName, entityName := range filteredEntityMap {
// Log the fieldName and entityName
log.Printf("Validating field: %s, entity: %s", fieldName, entityName)

// Use reflection to get the value of the field from v.state
valueOfState := reflect.ValueOf(v.state)
if valueOfState.Kind() == reflect.Ptr {
valueOfState = valueOfState.Elem() // Dereference if it's a pointer
}

fieldValue := valueOfState.FieldByName(fieldName)
if fieldValue.IsValid() && fieldValue.CanInterface() {
if err := v.entities(fieldValue.Interface(), entityName); err != nil {
allErr = append(allErr, err...)
}
} else {
allErr = append(allErr, fmt.Errorf("invalid field '%s' in state", fieldName))
}
}

// validate routes format with Kong 3.x
parsed30, err := semver.ParseTolerant(utils.FormatVersion30)
Expand Down

0 comments on commit d814d2f

Please sign in to comment.