encode.textMarshalerEncoder   A
last analyzed

Complexity

Conditions 5

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 10
dl 0
loc 15
ccs 0
cts 9
cp 0
crap 30
rs 9.3333
c 0
b 0
f 0
nop 1
1
package encode
2
3
import (
4
	"encoding"
5
	"reflect"
6
7
	"github.com/et-nik/binngo/binn"
8
)
9
10
func String(s string) []byte {
11 1
	var t []byte
12
13 1
	t = append(t, Size(len(s), false)...)
14 1
	t = append(t, []byte(s)...)
15
16 1
	return t
17
}
18
19
func textMarshalerEncoder(v reflect.Value) ([]byte, error) {
20
	if v.Kind() == reflect.Ptr && v.IsNil() {
21
		return []byte{binn.Null}, nil
22
	}
23
24
	m, ok := v.Interface().(encoding.TextMarshaler)
25
	if !ok {
26
		return []byte{binn.Null}, nil
27
	}
28
	b, err := m.MarshalText()
29
	if err != nil {
30
		return nil, err
31
	}
32
33
	return String(string(b)), nil
34
}
35