-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltin.go
More file actions
38 lines (34 loc) · 747 Bytes
/
builtin.go
File metadata and controls
38 lines (34 loc) · 747 Bytes
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
36
37
38
package encoder
import (
"fmt"
)
// byter is the interface that wraps the basic Bytes method.
type byter interface {
Bytes() []byte
}
// Builtin represents encoder for basic cases:
// * string
// * byte slice
// * byter
// * Stringer
type Builtin struct{}
func (e Builtin) Encode(dst []byte, x any) ([]byte, error) {
var err error
switch x.(type) {
case []byte:
dst = append(dst, x.([]byte)...)
case *[]byte:
dst = append(dst, *x.(*[]byte)...)
case string:
dst = append(dst, x.(string)...)
case *string:
dst = append(dst, *x.(*string)...)
case byter:
dst = append(dst, x.(byter).Bytes()...)
case fmt.Stringer:
dst = append(dst, x.(fmt.Stringer).String()...)
default:
err = ErrIncompatibleEncoder
}
return dst, err
}