encode/string.go   A
last analyzed

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 30.76%

Importance

Changes 0
Metric Value
cc 6
eloc 20
dl 0
loc 33
c 0
b 0
f 0
ccs 4
cts 13
cp 0.3076
crap 17.9449
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A encode.String 0 7 1
A encode.textMarshalerEncoder 0 15 5
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