From d814d2f9791c296e1ec2b3dc05ccdc6a139bf9a3 Mon Sep 17 00:00:00 2001 From: Antoine Jacquemin Date: Thu, 28 Nov 2024 20:31:05 +0100 Subject: [PATCH] fix reflection --- validate/validate.go | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/validate/validate.go b/validate/validate.go index a03135605..7ba8c82b6 100644 --- a/validate/validate.go +++ b/validate/validate.go @@ -7,6 +7,7 @@ import ( "net/http" "reflect" "sync" + "log" "github.com/blang/semver/v4" "github.com/kong/deck/utils" @@ -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 } } @@ -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)