/
encoder.go
182 lines (161 loc) · 3.63 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2022 Franklin "Snaipe" Mathieu.
//
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
package encutil
import (
"fmt"
"go/constant"
"io"
"math/big"
"reflect"
"strings"
"unicode"
"unicode/utf8"
"snai.pe/boa/encoding"
"snai.pe/boa/internal/reflectutil"
"snai.pe/boa/syntax"
)
type Marshaler interface {
reflectutil.Marshaler
syntax.Marshaler
}
type MarshalerBase struct {
Writer io.Writer
Self Marshaler
encoding.CommonOptions
encoding.EncoderOptions
}
func (m *MarshalerBase) Encode(v interface{}) error {
if node, ok := v.(*syntax.Node); ok {
return node.Marshal(m.Self)
}
return reflectutil.Marshal(reflect.ValueOf(v), m.Self, m.NamingConvention)
}
func (m *MarshalerBase) Option(handle func(interface{}) bool, opts ...interface{}) error {
for _, opt := range opts {
switch setopt := opt.(type) {
case encoding.CommonOption:
setopt(&m.CommonOptions)
case encoding.EncoderOption:
setopt(&m.EncoderOptions)
default:
if handle != nil {
if ok := handle(opt); ok {
continue
}
}
return fmt.Errorf("%T is not a common option, nor an encoder option.", opt)
}
}
return nil
}
func (m *MarshalerBase) WriteString(s string) error {
_, err := io.WriteString(m.Writer, s)
return err
}
func (m *MarshalerBase) WriteNewline() error {
nl := m.LineBreak
if nl == "" {
nl = "\n"
}
_, err := io.WriteString(m.Writer, nl)
return err
}
func (m *MarshalerBase) WriteIndent(level int) error {
for i := 0; i < level; i++ {
if _, err := io.WriteString(m.Writer, m.Indent); err != nil {
return err
}
}
return nil
}
func (m *MarshalerBase) WriteComment(prefix string, comments []string, depth int) error {
for _, comment := range comments {
if err := m.WriteIndent(depth); err != nil {
return err
}
if err := m.WriteString(prefix); err != nil {
return err
}
if err := m.WriteString(comment); err != nil {
return err
}
if err := m.WriteString("\n"); err != nil {
return err
}
}
return nil
}
func (m *MarshalerBase) WriteQuoted(in string, delim rune) error {
if err := m.WriteString(string(delim)); err != nil {
return err
}
for _, r := range in {
switch r {
case delim, '\n', '\r', '\b', '\t', '\f', '\\':
if err := m.WriteString("\\"); err != nil {
return err
}
switch r {
case '\n':
r = 'n'
case '\r':
r = 'r'
case '\b':
r = 'b'
case '\t':
r = 't'
case '\f':
r = 'f'
}
}
var err error
if unicode.IsPrint(r) {
var buf [utf8.UTFMax]byte
l := utf8.EncodeRune(buf[:], r)
_, err = m.Writer.Write(buf[:l])
} else if r > 0xffff {
_, err = fmt.Fprintf(m.Writer, "\\U%08x", r)
} else {
_, err = fmt.Fprintf(m.Writer, "\\u%04x", r)
}
if err != nil {
return err
}
}
return m.WriteString(string(delim))
}
func (m *MarshalerBase) MarshalBool(b bool) error {
if b {
return m.WriteString("true")
} else {
return m.WriteString("false")
}
}
func (m *MarshalerBase) MarshalString(s string) error {
return m.WriteQuoted(s, '"')
}
func (m *MarshalerBase) MarshalNumber(v constant.Value) (err error) {
switch v.Kind() {
case constant.Int:
_, err = fmt.Fprintf(m.Writer, "%d", constant.Val(v))
case constant.Float:
val := constant.Val(v)
var out strings.Builder
if rat, ok := val.(*big.Rat); ok {
val, _ = rat.Float64()
}
if _, err := fmt.Fprintf(&out, "%g", val); err != nil {
return err
}
// Preserve at least a trailing .0 to keep floats as floats.
if !strings.ContainsAny(out.String(), ".eE") {
out.WriteString(".0")
}
err = m.WriteString(out.String())
default:
err = fmt.Errorf("unsupported constant %v", v)
}
return err
}