This repository has been archived by the owner on Mar 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
encoder.go
157 lines (123 loc) · 3.16 KB
/
encoder.go
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package md
import (
"fmt"
"reflect"
"strings"
)
const (
tag = "slack_md"
tagOmitField = "-"
tagObfuscateField = "obfuscate"
)
var (
marshalerType = reflect.TypeOf(new(Marshaler)).Elem()
)
// Marshal returns the Slack Markdown encoding of v.
//
// Marshal traverses the value v recursively, similar to
// how encoding/json does.
//
// Struct field names will be printed in bold. Values will
// follow the default Golang enconding, except for pointers,
// which will be automatically dereferenced, and for objects
// implementing the Marshaler interface.
//
// Two tags are provided to facilitate proper serilializing.
//
// // Field is ignored by this package.
// Field int `json:"-"`
//
// // Field appears, but at most the last 4 characters are shown
// Field int `json:"obfuscate"`
//
// Those tags will preventing sensitive that from showing in your
// Slack channels.
//
func Marshal(v interface{}) ([]byte, error) {
return marshal(
v,
marshalOpts{indentLevel: 0},
)
}
type marshalOpts struct {
indentLevel int
obfuscate bool
}
func (o marshalOpts) withIncrementedIndentLevel() marshalOpts {
o.indentLevel = o.indentLevel + 1
return o
}
func marshal(v interface{}, opts marshalOpts) ([]byte, error) {
t := reflect.TypeOf(v)
if t.Implements(marshalerType) {
return v.(Marshaler).MarshalSlackMD()
}
switch t.Kind() {
case reflect.String:
return marshalStr(v, opts)
case reflect.Struct:
return marshalStruct(v, opts)
case reflect.Ptr:
return marshalPrt(v, opts)
default:
return []byte(fmt.Sprintf("%v", v)), nil
}
}
func marshalStr(v interface{}, opts marshalOpts) ([]byte, error) {
valueStr := v.(string)
if opts.obfuscate {
valueStr = obfuscate(valueStr)
}
return []byte(valueStr), nil
}
func obfuscate(str string) string {
length := len(str)
if length <= 4 {
return str
}
return strings.Repeat("*", length-4) + str[length-4:]
}
func marshalPrt(v interface{}, opts marshalOpts) ([]byte, error) {
value := reflect.ValueOf(v)
if value.IsNil() {
return []byte("null"), nil
}
return marshal(value.Elem().Interface(), opts)
}
func marshalStruct(v interface{}, opts marshalOpts) ([]byte, error) {
lines := []string{}
value := reflect.ValueOf(v)
t := reflect.TypeOf(v)
for i := 0; i < value.NumField(); i++ {
fieldType := t.Field(i)
fieldValue := value.Field(i)
if !fieldValue.CanInterface() {
continue
}
innerOpts := opts.withIncrementedIndentLevel()
if fieldType.Tag.Get(tag) == tagObfuscateField {
innerOpts.obfuscate = true
}
marshaledStructValue, err := marshal(fieldValue.Interface(), innerOpts)
if err != nil {
return nil, err
}
if fieldType.Tag.Get(tag) == tagOmitField {
continue
}
line := fmt.Sprintf("*%s*: %s", fieldType.Name, marshaledStructValue)
lines = append(lines, line)
}
lines = applyIndentation(lines, opts.indentLevel)
formattedLines := strings.Join(lines, "\n")
return []byte(formattedLines), nil
}
func applyIndentation(lines []string, level int) []string {
for i, line := range lines {
lines[i] = strings.Repeat("\t", level) + line
}
if len(lines) > 0 && level > 0 {
lines[0] = "\n" + lines[0] // inner structs should start on a new line
}
return lines
}