The "config".Unmarshaler interface can be used to implement unmarshaling behavior that differs from the default. That can be used to support some recurring needs:
- A
type Port uint16 that accepts numeric and string values. If a string, "net".LookupPort is used to convert it to an integer. If the numeric value is outside of the 1–65535 range, an error is returned.
- A
type ServiceName string that is the inverse: a numeric value (in the 1–65535 range) is converted to a string.
- Control mapping of multiple values onto a scalar type. For example, when mapping
["foo", "bar", "qux"] to a single string, should this result in an error (current behavior), the first value ("foo"), the last value ("qux"), or a random value? Special types could handle that. The "last value wins" / value is overwritten behavior is not uncommon in C plugins.
- Provide minimum / maximum values for numeric values.
- Check string values with a regular expression.
The
"config".Unmarshalerinterface can be used to implement unmarshaling behavior that differs from the default. That can be used to support some recurring needs:type Port uint16that accepts numeric and string values. If a string,"net".LookupPortis used to convert it to an integer. If the numeric value is outside of the 1–65535 range, an error is returned.type ServiceName stringthat is the inverse: a numeric value (in the 1–65535 range) is converted to a string.["foo", "bar", "qux"]to a singlestring, should this result in an error (current behavior), the first value ("foo"), the last value ("qux"), or a random value? Special types could handle that. The "last value wins" / value is overwritten behavior is not uncommon in C plugins.