0x4a52466c696e74 vor 4 Monaten
Ursprung
Commit
baae4541d1
2 geänderte Dateien mit 21 neuen und 3 gelöschten Zeilen
  1. 17 1
      serialize.go
  2. 4 2
      z_test.go

+ 17 - 1
serialize.go

@@ -127,7 +127,23 @@ loop:
 				rVal = from.FieldByName(fType.Name)
 			}
 		case reflect.Map:
-			if rVal = from.MapIndex(reflect.ValueOf(strings.ToLower(fType.Name))); !rVal.IsValid() {
+			/*
+				log.Println(fType.Name)
+				if rVal = from.MapIndex(reflect.ValueOf(strings.ToLower(fType.Name))); !rVal.IsValid() {
+					rVal = from.MapIndex(reflect.ValueOf(fType.Name))
+				}
+			*/
+			mapKeys := from.MapKeys()
+			lowerFTypeName := strings.ToLower(fType.Name)
+			for _, key := range mapKeys {
+				lowerKey := strings.ToLower(key.String())
+				if lowerKey == lowerFTypeName {
+					rVal = from.MapIndex(key)
+					break
+				}
+			}
+			if !rVal.IsValid() {
+				// Если ни один из ключей не совпал, попытаться найти по оригинальному ключу
 				rVal = from.MapIndex(reflect.ValueOf(fType.Name))
 			}
 		default:

+ 4 - 2
z_test.go

@@ -313,7 +313,8 @@ func TestFieldNames(t *testing.T) {
 }
 
 type AuthEmail struct {
-	Email string `rest:"required" example:"mail@mail.ml"`
+	Email     string `rest:"required" example:"mail@mail.ml"`
+	ThemeName string `rest:"required"`
 }
 
 func (s *AuthEmail) Validate(req IRequestIn) IRequestOut {
@@ -339,7 +340,8 @@ func (s *AuthEmail) Execute(req IRequestIn) IRequestOut {
 
 func TestSerializeEmail(t *testing.T) {
 	m := json.Map{
-		"email": "flint@77i.su",
+		"email":     "flint@77i.su",
+		"themeName": "th-name",
 	}
 
 	r := &AuthEmail{}