/
TryFormatLiteral.go
56 lines (50 loc) · 1.26 KB
/
TryFormatLiteral.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
// =================================================================
//
// Copyright (C) 2019 Spatial Current, Inc. - All Rights Reserved
// Released as open source under the MIT License. See LICENSE file.
//
// =================================================================
package dfl
import (
"fmt"
"reflect"
"strings"
)
func TryFormatLiteral(value interface{}, quotes []string, pretty bool, tabs int) string {
t := reflect.TypeOf(value)
if t.Kind() == reflect.Array || t.Kind() == reflect.Slice {
v := reflect.ValueOf(value)
s := "["
l := v.Len()
if l > 0 {
for i := 0; i < l; i++ {
s += TryFormatLiteral(v.Index(i).Interface(), quotes, false, 0)
if i < l-1 {
s += ", "
}
}
}
s += "]"
return s
}
switch value := value.(type) {
case string:
if len(value) == 0 {
return quotes[0] + quotes[0]
}
str := EscapeString(value)
if strings.ContainsAny(str, "\\-+=,.`'\"?:[]()\n\t ") {
return quotes[0] + str + quotes[0]
}
return str
case map[string]interface{}:
return NewDictionary(value).Dfl(quotes, pretty, tabs)
case map[string]struct{}:
return StringSet(value).Dfl(quotes, pretty, tabs)
case StringSet:
return value.Dfl(quotes, pretty, tabs)
case Null:
return value.Dfl()
}
return fmt.Sprint(value)
}