-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrconvx.go
More file actions
35 lines (30 loc) · 1.28 KB
/
strconvx.go
File metadata and controls
35 lines (30 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// strconvx is a tiny package that helps converting values from/to text.
package strconvx
// StringMarshaler is implemented by types that can represent themselves as a string.
// It is similar in spirit to encoding.TextMarshaler, but returns a string directly.
type StringMarshaler interface {
// ToString returns the string representation of the value.
ToString() (string, error)
}
// StringUnmarshaler is implemented by types that can parse themselves from a string.
// It is similar in spirit to encoding.TextUnmarshaler, but accepts a string directly.
type StringUnmarshaler interface {
// FromString parses the value from its string representation.
FromString(string) error
}
// StringCodec is implemented by types that support bidirectional conversion
// between their value and a string representation.
//
// It combines both StringMarshaler and StringUnmarshaler interfaces.
type StringCodec interface {
StringMarshaler
StringUnmarshaler
}
// New creates a StringCodec instance for the given value.
// This is a shortcut to the default namespace's New method.
// Note: since this uses the default namespace, it does not support
// overriding or customizing existing type bindings.
// For more advanced usage, see Namespace.New.
func New(v any) (StringCodec, error) {
return defaultNS.New(v)
}