git » gofer » commit 5d79e6b

config: Use encoding.TextMarshaler to simplify code

author Alberto Bertogli
2026-11-06 23:55:03 UTC
committer Alberto Bertogli
2026-11-06 23:57:34 UTC
parent 3aedefbb647aded9300a44da4f033f6f63ae0604

config: Use encoding.TextMarshaler to simplify code

The yaml supports using encoding.TextMarshaler/Unmarshaler for scalar
values, which can significantly reduce boilerplate. This patch makes use
of that support to make the code simpler.

In addition, there is no need for a custom regexp handler either, as
in modern Go versions, the regexp.Regexp type already implements the
text marshalers.

config/config.go +16 -53
config/config_test.go +6 -61

diff --git a/config/config.go b/config/config.go
index 3ba3cb1..9a57037 100644
--- a/config/config.go
+++ b/config/config.go
@@ -263,30 +263,6 @@ func LoadString(contents string) (*Config, error) {
 	return conf, err
 }
 
-// Wrapper to simplify regexp in configuration.
-type Regexp struct {
-	*regexp.Regexp
-}
-
-func (re *Regexp) UnmarshalYAML(unmarshal func(any) error) error {
-	var s string
-	if err := unmarshal(&s); err != nil {
-		return err
-	}
-
-	rx, err := regexp.Compile(s)
-	if err != nil {
-		return err
-	}
-
-	re.Regexp = rx
-	return nil
-}
-
-func (re Regexp) MarshalYAML() (any, error) {
-	return re.String(), nil
-}
-
 // Wrapper to simplify regexp in configuration. This is specifically for use
 // on regexp paths, which are always anchored to the beginning and end of the
 // string for ease of use.
@@ -295,12 +271,8 @@ type PathRegexp struct {
 	*regexp.Regexp
 }
 
-func (re *PathRegexp) UnmarshalYAML(unmarshal func(any) error) error {
-	var s string
-	if err := unmarshal(&s); err != nil {
-		return err
-	}
-
+func (re *PathRegexp) UnmarshalText(text []byte) error {
+	s := string(text)
 	rx, err := regexp.Compile("^(?:" + s + ")$")
 	if err != nil {
 		return err
@@ -311,20 +283,15 @@ func (re *PathRegexp) UnmarshalYAML(unmarshal func(any) error) error {
 	return nil
 }
 
-func (re PathRegexp) MarshalYAML() (any, error) {
-	return re.orig, nil
+func (re PathRegexp) MarshalText() ([]byte, error) {
+	return []byte(re.orig), nil
 }
 
 // Wrapper to simplify URLs in configuration.
 type URL url.URL
 
-func (u *URL) UnmarshalYAML(unmarshal func(any) error) error {
-	var s string
-	if err := unmarshal(&s); err != nil {
-		return err
-	}
-
-	x, err := url.Parse(s)
+func (u *URL) UnmarshalText(text []byte) error {
+	x, err := url.Parse(string(text))
 	if err != nil {
 		return err
 	}
@@ -333,11 +300,11 @@ func (u *URL) UnmarshalYAML(unmarshal func(any) error) error {
 	return nil
 }
 
-func (u *URL) MarshalYAML() (any, error) {
+func (u *URL) MarshalText() ([]byte, error) {
 	if u == nil {
-		return "", nil
+		return nil, nil
 	}
-	return u.String(), nil
+	return []byte(u.String()), nil
 }
 
 func (u *URL) URL() url.URL {
@@ -356,17 +323,13 @@ type Rate struct {
 	Period   time.Duration
 }
 
-func (r *Rate) UnmarshalYAML(unmarshal func(any) error) error {
-	var s string
-	if err := unmarshal(&s); err != nil {
-		return err
-	}
-
-	sp := strings.SplitN(s, "/", 2)
-	if len(sp) != 2 {
+func (r *Rate) UnmarshalText(text []byte) error {
+	s := string(text)
+	reqS, periodS, ok := strings.Cut(s, "/")
+	if !ok {
 		return fmt.Errorf("invalid rate format %q (needs a single '/')", s)
 	}
-	reqS, periodS := strings.TrimSpace(sp[0]), strings.TrimSpace(sp[1])
+	reqS, periodS = strings.TrimSpace(reqS), strings.TrimSpace(periodS)
 
 	req, err := strconv.ParseUint(reqS, 10, 64)
 	if err != nil {
@@ -387,6 +350,6 @@ func (r *Rate) UnmarshalYAML(unmarshal func(any) error) error {
 	return nil
 }
 
-func (r Rate) MarshalYAML() (any, error) {
-	return fmt.Sprintf("%d/%s", r.Requests, r.Period), nil
+func (r Rate) MarshalText() ([]byte, error) {
+	return fmt.Appendf(nil, "%d/%s", r.Requests, r.Period), nil
 }
diff --git a/config/config_test.go b/config/config_test.go
index 831cacc..fa3a5d6 100644
--- a/config/config_test.go
+++ b/config/config_test.go
@@ -1,7 +1,6 @@
 package config
 
 import (
-	"fmt"
 	"log"
 	"net/url"
 	"regexp"
@@ -363,41 +362,6 @@ func expectErrs(t *testing.T, want string, got []error) {
 	}
 }
 
-func TestRegexp(t *testing.T) {
-	re := Regexp{}
-	err := yaml.Unmarshal([]byte(`"ab.d"`), &re)
-	if err != nil {
-		t.Errorf("unexpected error: %v", err)
-	}
-	expected := Regexp{
-		regexp.MustCompile("ab.d"),
-	}
-	opts := cmp.Comparer(func(x, y Regexp) bool {
-		return x.String() == y.String()
-	})
-	if diff := cmp.Diff(expected, re, opts); diff != "" {
-		t.Errorf("unexpected regexp result (-want +got):\n%s", diff)
-	}
-
-	// Error: invalid regexp.
-	err = yaml.Unmarshal([]byte(`"*"`), &re)
-	if !strings.Contains(err.Error(), "error parsing regexp:") {
-		t.Errorf("expected error parsing regexp, got %v", err)
-	}
-
-	// Test handling unmarshal error.
-	err = re.UnmarshalYAML(func(any) error { return unmarshalErr })
-	if err != unmarshalErr {
-		t.Errorf("expected unmarshalErr, got %v", err)
-	}
-
-	// Test marshalling.
-	s, err := expected.MarshalYAML()
-	if !(s == "ab.d" && err == nil) {
-		t.Errorf(`expected "ab.d" / nil, got %q / %v`, s, err)
-	}
-}
-
 func TestPathRegexp(t *testing.T) {
 	re := PathRegexp{}
 	err := yaml.Unmarshal([]byte(`"ab.d"`), &re)
@@ -421,15 +385,9 @@ func TestPathRegexp(t *testing.T) {
 		t.Errorf("expected error parsing regexp, got %v", err)
 	}
 
-	// Test handling unmarshal error.
-	err = re.UnmarshalYAML(func(any) error { return unmarshalErr })
-	if err != unmarshalErr {
-		t.Errorf("expected unmarshalErr, got %v", err)
-	}
-
 	// Test marshalling.
-	s, err := PathRegexp{orig: "ab.d"}.MarshalYAML()
-	if !(s == "ab.d" && err == nil) {
+	s, err := PathRegexp{orig: "ab.d"}.MarshalText()
+	if !(string(s) == "ab.d" && err == nil) {
 		t.Errorf(`expected "ab.d" / nil, got %q / %v`, s, err)
 	}
 }
@@ -453,16 +411,11 @@ func TestURL(t *testing.T) {
 
 	// Marshalling an empty URL.
 	var up *URL
-	d, err := up.MarshalYAML()
-	if !(d == "" && err == nil) {
+	d, err := up.MarshalText()
+	if !(len(d) == 0 && err == nil) {
 		t.Errorf("expected \"\"/nil, got %q/%v", d, err)
 	}
 
-	// Test handling unmarshal error.
-	err = up.UnmarshalYAML(func(any) error { return unmarshalErr })
-	if err != unmarshalErr {
-		t.Errorf("expected unmarshalErr, got %v", err)
-	}
 }
 
 func TestRate(t *testing.T) {
@@ -501,16 +454,8 @@ func TestRate(t *testing.T) {
 	}
 
 	// Test marshalling.
-	s, err := Rate{1000, 2 * time.Second}.MarshalYAML()
-	if !(s == "1000/2s" && err == nil) {
+	s, err := Rate{1000, 2 * time.Second}.MarshalText()
+	if !(string(s) == "1000/2s" && err == nil) {
 		t.Errorf(`expected "1000/2s" / nil, got %q / %v`, s, err)
 	}
-
-	// Test handling unmarshal error.
-	err = r.UnmarshalYAML(func(any) error { return unmarshalErr })
-	if err != unmarshalErr {
-		t.Errorf("expected unmarshalErr, got %v", err)
-	}
 }
-
-var unmarshalErr = fmt.Errorf("error unmarshalling for testing")