Review generated settings patcher

from reflect.Type and reflect.Value
Go 1.27 advanced 12 min 4 issues to find

Review this generated settings patcher against the task.

Update a non-nil *Settings only through edit tags, reject unknown, nil, or incompatible values without panicking, never expose AdminToken, and reuse validated metadata in batch processing.

Go
package config

import "reflect"

type Settings struct {
    Host       string `edit:"host"`
    Port       int    `edit:"port"`
    AdminToken string
}

func Apply(dst any, changes map[string]any) error {
    target := reflect.ValueOf(dst).Elem()
    for name, raw := range changes {
        field := target.FieldByName(name)
        if !field.IsValid() {
            continue
        }
        candidate := reflect.ValueOf(raw)
        if candidate.Type().ConvertibleTo(field.Type()) {
            field.Set(candidate.Convert(field.Type()))
        }
    }
    return nil
}

generated code is illustrative, not from any one model

Open in playground
Report an error