{"record":{"id":"8790d79f42fd9ec8","repo":"cilium/cilium","slug":"converting-struct-to-map-w","errorCode":null,"errorMessage":"converting struct to map: %w","messagePattern":"converting struct to map: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/bpf/constants.go","lineNumber":30,"sourceCode":"\t\"reflect\"\n\t\"strings\"\n\n\t\"github.com/cilium/ebpf\"\n\n\t\"github.com/cilium/cilium/pkg/datapath/config\"\n\t\"github.com/cilium/cilium/pkg/datapath/config/types\"\n)\n\n// applyConstants sets the values of BPF C runtime configurables defined using\n// the DECLARE_CONFIG macro.\nfunc applyConstants(spec *ebpf.CollectionSpec, obj any) error {\n\tif obj == nil {\n\t\treturn nil\n\t}\n\n\tconstants, err := config.Map(obj)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"converting struct to map: %w\", err)\n\t}\n\n\tfor name, value := range constants {\n\t\tconstName := types.ConstantPrefix + name\n\n\t\tv, ok := spec.Variables[constName]\n\t\tif !ok {\n\t\t\treturn fmt.Errorf(\"can't set non-existent Variable %s\", name)\n\t\t}\n\n\t\tif v.SectionName != types.ConstantSection {\n\t\t\treturn fmt.Errorf(\"can only set Cilium config variables in section %s (got %s:%s), \", types.ConstantSection, v.SectionName, name)\n\t\t}\n\n\t\tif err := v.Set(value); err != nil {\n\t\t\treturn fmt.Errorf(\"setting Variable %s: %w\", name, err)\n\t\t}\n\t}","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/cilium/cilium/blob/ac7b90affa4baf0642e6685319d56907b3a73a6d/pkg/bpf/constants.go#L12-L48","documentation":"applyConstants converts the Go config struct passed in CollectionOptions.Constants into a map of field name -> value via config.Map, which relies on struct tags and reflection. This error wraps any failure of that conversion - invalid field types, unsupported kinds, or malformed config tags in the struct. It fires before any BPF variable is touched, so the problem is in the Go config object, not the ELF.","triggerScenarios":"LoadCollection/LoadAndAssign called with opts.Constants containing a struct whose fields have types config.Map cannot marshal (e.g. unsupported nested/complex kinds) or whose config struct tags are invalid.","commonSituations":"Adding a new field to a datapath config struct with an unsupported Go type; typos or wrong tags on config fields; passing a type not registered with the config conversion machinery.","solutions":["Check the wrapped %w error to find the offending struct field and fix its Go type or config tag","Restrict Constants struct fields to types supported by pkg/datapath/config (ints, bools, strings, etc.)","Pass a nil/simple config if the object doesn't need constants, or update config.Map to support the new type"],"exampleFix":"// before\n type Config struct {\n     Routes map[string]string `config:\"routes\"` // unsupported kind\n }\n\n// after\n type Config struct {\n     RouteCount uint32 `config:\"route_count\"`\n }","handlingStrategy":"validation","validationCode":"if opts.Constants != nil {\n    if _, err := config.Map(opts.Constants); err != nil {\n        return fmt.Errorf(\"invalid config struct: %w\", err)\n    }\n}","typeGuard":"func constantsConvertible(obj any) bool {\n    if obj == nil {\n        return true\n    }\n    _, err := config.Map(obj)\n    return err == nil\n}","tryCatchPattern":"if err := LoadAndAssign(logger, &obj, spec, opts); err != nil {\n    if strings.Contains(err.Error(), \"converting struct to map\") {\n        return fmt.Errorf(\"config struct has unsupported fields; check types and config tags: %w\", err)\n    }\n    return err\n}","preventionTips":["Keep Constants struct fields limited to types supported by pkg/datapath/config","Unit-test config.Map on your config struct before wiring it into loading","Add lint rules for config struct tags"],"tags":["ebpf","cilium","reflection","config-struct"],"backgroundTag":"struct-to-map-conversion-failed","analyzedSha":"ac7b90affa4baf0642e6685319d56907b3a73a6d","analyzedAt":"2026-08-31T18:27:15.868Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}