-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathencoding.go
279 lines (239 loc) · 5.67 KB
/
encoding.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package libs
// #include <stdlib.h>
// #include "sass/context.h"
import "C"
import (
"image/color"
"reflect"
"unsafe"
)
type UnionSassValue *C.union_Sass_Value
// NewUnionSassValue creates a new empty UnionSassValue
func NewUnionSassValue() UnionSassValue {
return MakeNil()
}
func CloneValue(usv UnionSassValue) UnionSassValue {
return C.sass_clone_value(usv)
}
func DeleteValue(usv UnionSassValue) {
C.sass_delete_value(usv)
}
// MakeNil creates Sass nil
func MakeNil() UnionSassValue {
return C.sass_make_null()
}
// MakeBool creates Sass bool from a bool
func MakeBool(b bool) UnionSassValue {
return C.sass_make_boolean(C.bool(b))
}
// MakeError creates Sass error from a string
func MakeError(s string) UnionSassValue {
cs := C.CString(s)
defer C.free(unsafe.Pointer(cs))
return C.sass_make_error(cs)
}
// MakeWarning creates Sass warning from a string
func MakeWarning(s string) UnionSassValue {
cs := C.CString(s)
defer C.free(unsafe.Pointer(cs))
return C.sass_make_warning(cs)
}
func MakeString(s string) UnionSassValue {
str := C.CString(s)
defer C.free(unsafe.Pointer(str))
return C.sass_make_string(str)
}
// TODO: validate unit
func MakeNumber(f float64, unit string) UnionSassValue {
cunit := C.CString(unit)
cf := C.double(f)
defer C.free(unsafe.Pointer(cunit))
return C.sass_make_number(cf, cunit)
}
// MakeColor creates a Sass color from color.RGBA
func MakeColor(c color.RGBA) UnionSassValue {
r := C.double(c.R)
g := C.double(c.G)
b := C.double(c.B)
a := C.double(c.A)
return C.sass_make_color(r, g, b, a)
}
// MakeList creates a Sass List
func MakeList(len int) UnionSassValue {
return C.sass_make_list(C.size_t(len), C.SASS_COMMA, false)
}
// MakeMap cretes a new Sass Map
func MakeMap(len int) UnionSassValue {
return C.sass_make_map(C.size_t(len))
}
// Slice creates a Sass List from a Go slice. Reflection is used.
func Slice(usv UnionSassValue, inf interface{}) {
if !IsList(usv) {
panic("sass value is not a list")
}
l := Len(usv)
r := reflect.ValueOf(inf)
if r.Kind() == reflect.Ptr {
r = r.Elem()
}
if !r.CanSet() {
panic("value is not addressable")
}
// if a non-slice is passed, make a slice
t := r.Type()
if r.Kind() != reflect.Slice {
t = reflect.SliceOf(t)
}
d := reflect.MakeSlice(t, l, l)
for i := 0; i < l; i++ {
sv := Index(usv, i)
inf := Interface(sv)
rf := reflect.ValueOf(inf)
// Special case for nil
if inf == nil {
d.Index(i).Set(reflect.ValueOf("<nil>"))
continue
}
d.Index(i).Set(rf)
}
r.Set(d)
}
func IsNil(usv UnionSassValue) bool {
return bool(C.sass_value_is_null(usv))
}
func IsBool(usv UnionSassValue) bool {
return bool(C.sass_value_is_boolean(usv))
}
func IsString(usv UnionSassValue) bool {
return bool(C.sass_value_is_string(usv))
}
func IsColor(usv UnionSassValue) bool {
return bool(C.sass_value_is_color(usv))
}
func IsNumber(usv UnionSassValue) bool {
return bool(C.sass_value_is_number(usv))
}
func IsList(usv UnionSassValue) bool {
return bool(C.sass_value_is_list(usv))
}
func IsMap(usv UnionSassValue) bool {
return bool(C.sass_value_is_map(usv))
}
func IsError(usv UnionSassValue) bool {
return bool(C.sass_value_is_error(usv))
}
// Interface creates Go types from union sass_value
func Interface(usv UnionSassValue) interface{} {
switch {
case IsNil(usv):
return nil
case IsBool(usv):
return Bool(usv)
case IsString(usv):
return String(usv)
case IsColor(usv):
return Color(usv)
case IsNumber(usv):
return Number(usv)
case IsList(usv):
fallthrough
//return List(usv)
case IsMap(usv):
fallthrough
//return Map(usv)
default:
return nil
}
panic("call of interface not supported on type")
}
func Len(usv UnionSassValue) int {
switch {
case IsList(usv):
return int(C.sass_list_get_length(usv))
case IsMap(usv):
return int(C.sass_map_get_length(usv))
}
panic("call of len on unknown type")
}
func Error(usv UnionSassValue) string {
c := C.sass_error_get_message(usv)
gc := C.GoString(c)
return gc
}
func String(usv UnionSassValue) string {
c := C.sass_string_get_value(usv)
gc := C.GoString(c)
return gc
}
type SassNumber struct {
Value float64
Unit string
}
func (n SassNumber) Float() float64 {
return n.Value
}
func (n SassNumber) UnitOf() string {
return n.Unit
}
func Number(usv UnionSassValue) SassNumber {
return SassNumber{
Value: Float(usv),
Unit: Unit(usv),
}
}
func Float(usv UnionSassValue) float64 {
f := C.sass_number_get_value(usv)
return float64(f)
}
func Unit(usv UnionSassValue) string {
c := C.sass_number_get_unit(usv)
return C.GoString(c)
}
func Bool(usv UnionSassValue) bool {
b := C.sass_boolean_get_value(usv)
return bool(b)
}
func Color(usv UnionSassValue) color.Color {
return color.RGBA{
R: uint8(C.sass_color_get_r(usv)),
G: uint8(C.sass_color_get_g(usv)),
B: uint8(C.sass_color_get_b(usv)),
A: uint8(C.sass_color_get_a(usv)),
}
}
func Index(usv UnionSassValue, i int) UnionSassValue {
switch {
case IsList(usv):
return C.sass_list_get_value(usv, C.size_t(i))
default:
panic("call of index on unknown type")
}
return NewUnionSassValue()
}
func SetIndex(usv UnionSassValue, i int, item UnionSassValue) {
switch {
case IsList(usv):
C.sass_list_set_value(usv, C.size_t(i), item)
return
default:
}
panic("call of setindex on unknown type")
}
func MapKeys(m UnionSassValue) []UnionSassValue {
res := make([]UnionSassValue, Len(m))
for i := range res {
res[i] = C.sass_map_get_key(m, C.size_t(i))
}
return res
}
func mapVals(m UnionSassValue) []UnionSassValue {
res := make([]UnionSassValue, Len(m))
for i := range res {
res[i] = C.sass_map_get_value(m, C.size_t(i))
}
return res
}
// TODO: support maps
// func MapIndex(m UnionSassValue, key UnionSassValue) UnionSassValue {
// keys := MapKeys(m)
// }