From 272534584376c81116db5199fd2e4216e3698192 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sat, 05 Apr 2025 22:22:46 +0800 Subject: [PATCH] scfg: Error out when required directives are missing --- config.go | 2 -- internal/scfg/unmarshal.go | 14 ++++++++++++++ diff --git a/config.go b/config.go index 21587210a505b3ea3267b7e6d2478151dfb42db2..abb71dd2b2beacdb5dd194b0957e331661756f72 100644 --- a/config.go +++ b/config.go @@ -69,8 +69,6 @@ // LoadConfig loads a configuration file from the specified path and unmarshals // it to the global [config] struct. This may race with concurrent reads from // [config]; additional synchronization is necessary if the configuration is to // be made reloadable. -// -// TODO: Error out when there are missing fields func (s *Server) LoadConfig(path string) (err error) { var configFile *os.File if configFile, err = os.Open(path); err != nil { diff --git a/internal/scfg/unmarshal.go b/internal/scfg/unmarshal.go index 19f3dae73a775b5be26e850db350d015d160ddc0..e9e1a528a0700ab641b304f12169c4b07d92ff96 100644 --- a/internal/scfg/unmarshal.go +++ b/internal/scfg/unmarshal.go @@ -107,11 +107,14 @@ return err } v.SetMapIndex(reflect.ValueOf(name), mv) } + case reflect.Struct: si, err := getStructInfo(t) if err != nil { return err } + + seen := make(map[int]bool) for name, dirs := range dirsByName { fieldIndex, ok := si.children[name] @@ -123,7 +126,18 @@ fv := v.Field(fieldIndex) if err := dec.unmarshalDirectiveList(dirs, fv); err != nil { return err } + seen[fieldIndex] = true } + + for name, fieldIndex := range si.children { + if fieldIndex == si.param { + continue + } + if _, ok := seen[fieldIndex]; !ok { + return fmt.Errorf("scfg: missing required directive %q", name) + } + } + default: return fmt.Errorf("scfg: unsupported type for unmarshaling blocks: %v", t) } -- 2.48.1