diff options
| author | 2026-06-06 21:06:11 +0000 | |
|---|---|---|
| committer | 2026-06-06 21:10:23 +0000 | |
| commit | ec2288c0e23e726ea8bbe0c9860e013e1b846bac (patch) | |
| tree | 0ec988854a0dc72209e9542b52a35368c2b30098 | |
| parent | Various fixes and QoL things (diff) | |
| signature | No signature | |
config: QoL renames
| -rw-r--r-- | config/config.go | 25 | ||||
| -rw-r--r-- | config/config_test.go | 52 | ||||
| -rw-r--r-- | config/errors.go | 6 | ||||
| -rw-r--r-- | config/key_value.go | 16 | ||||
| -rw-r--r-- | config/kind.go | 12 | ||||
| -rw-r--r-- | config/lookup.go | 34 |
6 files changed, 73 insertions, 72 deletions
diff --git a/config/config.go b/config/config.go index 317f4a7b..3c594939 100644 --- a/config/config.go +++ b/config/config.go @@ -18,11 +18,11 @@ import ( // // Includes aren't supported yet; they will be supported in a later revision. type Config struct { - entries []ConfigEntry + entries []Entry } -// ParseConfig reads and parses Git configuration entries from r. -func ParseConfig(r io.Reader) (*Config, error) { +// Parse reads and parses Git configuration entries from r. +func Parse(r io.Reader) (*Config, error) { parser := &configParser{ reader: bufio.NewReader(r), lineNum: 1, @@ -35,21 +35,22 @@ func ParseConfig(r io.Reader) (*Config, error) { return parser.parse() } -// ConfigEntry represents a single parsed configuration directive. -type ConfigEntry struct { - // The section name in canonical lowercase form. +// Entry represents a single parsed configuration directive. +type Entry struct { + // Section is the section name in canonical lowercase form. Section string - // The subsection name, retaining the exact form parsed from the input. + // Subsection is the subsection name, + // retaining the exact form parsed from the input. Subsection string - // The key name in canonical lowercase form. + // Key is the key name in canonical lowercase form. Key string - // Whether this entry has no value or an explicit value. + // Kind reports whether this entry has no value or an explicit value. Kind Kind - // The interpreted value of the configuration entry, + // Value is the interpreted value of the configuration entry, // including unescaped characters where appropriate. Value string } @@ -58,8 +59,8 @@ type ConfigEntry struct { // in the order they appeared. // // Modifying the returned slice does not affect the Config. -func (c *Config) Entries() []ConfigEntry { - return slices.Clone(c.entries) +func (config *Config) Entries() []Entry { + return slices.Clone(config.entries) } type configParser struct { diff --git a/config/config_test.go b/config/config_test.go index 1258341d..29200c1b 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -45,9 +45,9 @@ func TestConfig(t *testing.T) { defer func() { _ = cfgFile.Close() }() - cfg, err := config.ParseConfig(cfgFile) + cfg, err := config.Parse(cfgFile) if err != nil { - t.Fatalf("ParseConfig failed: %v", err) + t.Fatalf("Parse failed: %v", err) } got, err := cfg.Lookup("test", "", "enabled").String() @@ -112,9 +112,9 @@ func TestConfigSubsection(t *testing.T) { defer func() { _ = cfgFile.Close() }() - cfg, err := config.ParseConfig(cfgFile) + cfg, err := config.Parse(cfgFile) if err != nil { - t.Fatalf("ParseConfig failed: %v", err) + t.Fatalf("Parse failed: %v", err) } got, err := cfg.Lookup("test", "origin", "url").String() @@ -166,9 +166,9 @@ func TestConfigMultiValue(t *testing.T) { defer func() { _ = cfgFile.Close() }() - cfg, err := config.ParseConfig(cfgFile) + cfg, err := config.Parse(cfgFile) if err != nil { - t.Fatalf("ParseConfig failed: %v", err) + t.Fatalf("Parse failed: %v", err) } fetches := cfg.LookupAll("test", "origin", "fetch") @@ -231,9 +231,9 @@ func TestConfigCaseInsensitive(t *testing.T) { defer func() { _ = cfgFile.Close() }() - cfg, err := config.ParseConfig(cfgFile) + cfg, err := config.Parse(cfgFile) if err != nil { - t.Fatalf("ParseConfig failed: %v", err) + t.Fatalf("Parse failed: %v", err) } got, err := cfg.Lookup("test", "", "flag").String() @@ -299,9 +299,9 @@ func TestConfigBoolean(t *testing.T) { defer func() { _ = cfgFile.Close() }() - cfg, err := config.ParseConfig(cfgFile) + cfg, err := config.Parse(cfgFile) if err != nil { - t.Fatalf("ParseConfig failed: %v", err) + t.Fatalf("Parse failed: %v", err) } tests := make([]struct { @@ -353,14 +353,14 @@ toosmall = -2147483649 badnum = " 2x" ` - cfg, err := config.ParseConfig(strings.NewReader(cfgText)) + cfg, err := config.Parse(strings.NewReader(cfgText)) if err != nil { - t.Fatalf("ParseConfig failed: %v", err) + t.Fatalf("Parse failed: %v", err) } novalue := cfg.Lookup("test", "", "novalue") - if novalue.Kind != config.ValueValueless { - t.Fatalf("novalue kind: got %v, want %v", novalue.Kind, config.ValueValueless) + if novalue.Kind != config.KindValueless { + t.Fatalf("novalue kind: got %v, want %v", novalue.Kind, config.KindValueless) } novalueBool, err := novalue.Bool() @@ -369,8 +369,8 @@ badnum = " 2x" } empty := cfg.Lookup("test", "", "empty") - if empty.Kind != config.ValueString || empty.Value != "" { - t.Fatalf("empty: got (%v, %q), want (%v, %q)", empty.Kind, empty.Value, config.ValueString, "") + if empty.Kind != config.KindString || empty.Value != "" { + t.Fatalf("empty: got (%v, %q), want (%v, %q)", empty.Kind, empty.Value, config.KindString, "") } emptyBool, err := empty.Bool() @@ -454,8 +454,8 @@ badnum = " 2x" } missing := cfg.Lookup("test", "", "missing") - if missing.Kind != config.ValueMissing { - t.Fatalf("missing kind: got %v, want %v", missing.Kind, config.ValueMissing) + if missing.Kind != config.KindMissing { + t.Fatalf("missing kind: got %v, want %v", missing.Kind, config.KindMissing) } _, err = missing.Bool() @@ -509,9 +509,9 @@ func TestConfigComplexValues(t *testing.T) { defer func() { _ = cfgFile.Close() }() - cfg, err := config.ParseConfig(cfgFile) + cfg, err := config.Parse(cfgFile) if err != nil { - t.Fatalf("ParseConfig failed: %v", err) + t.Fatalf("Parse failed: %v", err) } tests := []string{"spaced", "special", "path", "number"} @@ -564,9 +564,9 @@ func TestConfigEntries(t *testing.T) { defer func() { _ = cfgFile.Close() }() - cfg, err := config.ParseConfig(cfgFile) + cfg, err := config.Parse(cfgFile) if err != nil { - t.Fatalf("ParseConfig failed: %v", err) + t.Fatalf("Parse failed: %v", err) } entries := cfg.Entries() @@ -627,7 +627,7 @@ func TestConfigErrorCases(t *testing.T) { r := strings.NewReader(tt.config) - _, err := config.ParseConfig(r) + _, err := config.Parse(r) if err == nil { t.Errorf("expected error for %s", tt.name) } @@ -687,7 +687,7 @@ func TestConfigRawBytes(t *testing.T) { } gitValue, gitErr := testRepo.ConfigGet(t, tt.gitKey) - furConfig, furErr := config.ParseConfig(bytes.NewReader(tt.cfgData)) + furConfig, furErr := config.Parse(bytes.NewReader(tt.cfgData)) if (gitErr == nil) != (furErr == nil) { t.Fatalf("git: %v\nfur: %v", gitErr, furErr) @@ -729,9 +729,9 @@ func FuzzConfig(f *testing.F) { gitValue, gitErr := testRepo.ConfigGet(t, gitKey) - furConfig, furErr := config.ParseConfig(bytes.NewReader(cfgData)) + furConfig, furErr := config.Parse(bytes.NewReader(cfgData)) if furErr == nil && furConfig == nil { - t.Fatalf("ParseConfig returned nil config with nil error") + t.Fatalf("Parse returned nil config with nil error") } sameErr := (gitErr == nil) == (furErr == nil) diff --git a/config/errors.go b/config/errors.go index f45c31ac..b1049b34 100644 --- a/config/errors.go +++ b/config/errors.go @@ -24,11 +24,11 @@ type LookupError struct { func (err *LookupError) Error() string { switch err.Kind { - case ValueMissing: + case KindMissing: return fmt.Sprintf("config: %s: missing config value", err.Operation) - case ValueValueless: + case KindValueless: return fmt.Sprintf("config: %s: valueless config key", err.Operation) - case ValueString: + case KindString: return fmt.Sprintf("config: %s: invalid string config value", err.Operation) default: return fmt.Sprintf("config: %s: unknown value kind %d", err.Operation, err.Kind) diff --git a/config/key_value.go b/config/key_value.go index 114f5c68..86034f53 100644 --- a/config/key_value.go +++ b/config/key_value.go @@ -49,11 +49,11 @@ func (p *configParser) parseKeyValue(cfg *Config) error { for { ch, err := p.nextChar() if errors.Is(err, io.EOF) { - cfg.entries = append(cfg.entries, ConfigEntry{ + cfg.entries = append(cfg.entries, Entry{ Section: p.currentSection, Subsection: p.currentSubsec, Key: keyStr, - Kind: ValueValueless, + Kind: KindValueless, Value: "", }) @@ -65,11 +65,11 @@ func (p *configParser) parseKeyValue(cfg *Config) error { } if ch == '\n' { - cfg.entries = append(cfg.entries, ConfigEntry{ + cfg.entries = append(cfg.entries, Entry{ Section: p.currentSection, Subsection: p.currentSubsec, Key: keyStr, - Kind: ValueValueless, + Kind: KindValueless, Value: "", }) @@ -82,11 +82,11 @@ func (p *configParser) parseKeyValue(cfg *Config) error { return err } - cfg.entries = append(cfg.entries, ConfigEntry{ + cfg.entries = append(cfg.entries, Entry{ Section: p.currentSection, Subsection: p.currentSubsec, Key: keyStr, - Kind: ValueValueless, + Kind: KindValueless, Value: "", }) @@ -107,11 +107,11 @@ func (p *configParser) parseKeyValue(cfg *Config) error { return err } - cfg.entries = append(cfg.entries, ConfigEntry{ + cfg.entries = append(cfg.entries, Entry{ Section: p.currentSection, Subsection: p.currentSubsec, Key: keyStr, - Kind: ValueString, + Kind: KindString, Value: value, }) diff --git a/config/kind.go b/config/kind.go index bbe4cf9d..07870c9e 100644 --- a/config/kind.go +++ b/config/kind.go @@ -12,14 +12,14 @@ import ( type Kind uint8 const ( - // ValueMissing means the queried key does not exist. - ValueMissing Kind = iota + // KindMissing means the queried key does not exist. + KindMissing Kind = iota - // ValueValueless means the key exists but has no "= <value>" part. - ValueValueless + // KindValueless means the key exists but has no "= <value>" part. + KindValueless - // ValueString means the key exists and has an explicit value (possibly ""). - ValueString + // KindString means the key exists and has an explicit value (possibly ""). + KindString ) func isValidSection(s string) bool { diff --git a/config/lookup.go b/config/lookup.go index 023d8f8b..ede22700 100644 --- a/config/lookup.go +++ b/config/lookup.go @@ -13,11 +13,11 @@ type LookupResult struct { // String returns the explicit string value. func (r LookupResult) String() (string, error) { switch r.Kind { - case ValueMissing: + case KindMissing: return "", &LookupError{Kind: r.Kind, Operation: "string"} - case ValueValueless: + case KindValueless: return "", &LookupError{Kind: r.Kind, Operation: "string"} - case ValueString: + case KindString: return r.Value, nil default: return "", &LookupError{Kind: r.Kind, Operation: "string"} @@ -27,11 +27,11 @@ func (r LookupResult) String() (string, error) { // Bool interprets this lookup result using Git config boolean rules. func (r LookupResult) Bool() (bool, error) { switch r.Kind { - case ValueMissing: + case KindMissing: return false, &LookupError{Kind: r.Kind, Operation: "bool"} - case ValueValueless: + case KindValueless: return true, nil - case ValueString: + case KindString: return parseBool(r.Value) default: return false, &LookupError{Kind: r.Kind, Operation: "bool"} @@ -41,11 +41,11 @@ func (r LookupResult) Bool() (bool, error) { // Int interprets this lookup result as a Git integer value. func (r LookupResult) Int() (int, error) { switch r.Kind { - case ValueMissing: + case KindMissing: return 0, &LookupError{Kind: r.Kind, Operation: "int"} - case ValueValueless: + case KindValueless: return 0, &LookupError{Kind: r.Kind, Operation: "int"} - case ValueString: + case KindString: return parseInt(r.Value) default: return 0, &LookupError{Kind: r.Kind, Operation: "int"} @@ -55,11 +55,11 @@ func (r LookupResult) Int() (int, error) { // Int64 interprets this lookup result as a Git int64 value. func (r LookupResult) Int64() (int64, error) { switch r.Kind { - case ValueMissing: + case KindMissing: return 0, &LookupError{Kind: r.Kind, Operation: "int64"} - case ValueValueless: + case KindValueless: return 0, &LookupError{Kind: r.Kind, Operation: "int64"} - case ValueString: + case KindString: return parseInt64(r.Value) default: return 0, &LookupError{Kind: r.Kind, Operation: "int64"} @@ -68,11 +68,11 @@ func (r LookupResult) Int64() (int64, error) { // Lookup retrieves the first value for a given section, optional subsection, // and key. -func (c *Config) Lookup(section, subsection, key string) LookupResult { +func (config *Config) Lookup(section, subsection, key string) LookupResult { section = strings.ToLower(section) key = strings.ToLower(key) - for _, entry := range c.entries { + for _, entry := range config.entries { if strings.EqualFold(entry.Section, section) && entry.Subsection == subsection && strings.EqualFold(entry.Key, key) { @@ -84,20 +84,20 @@ func (c *Config) Lookup(section, subsection, key string) LookupResult { } return LookupResult{ - Kind: ValueMissing, + Kind: KindMissing, Value: "", } } // LookupAll retrieves all values for a given section, optional subsection, // and key. -func (c *Config) LookupAll(section, subsection, key string) []LookupResult { +func (config *Config) LookupAll(section, subsection, key string) []LookupResult { section = strings.ToLower(section) key = strings.ToLower(key) var values []LookupResult - for _, entry := range c.entries { + for _, entry := range config.entries { if strings.EqualFold(entry.Section, section) && entry.Subsection == subsection && strings.EqualFold(entry.Key, key) { |
