-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnjson_annotation.go
125 lines (115 loc) · 2.53 KB
/
njson_annotation.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
package nbtreader
import (
"fmt"
"reflect"
)
type TypeAnnotation uint8
const (
NoAnnotation TypeAnnotation = iota
InferenceAnnotation
CompoundAnnotation
ByteArrayAnnotation
IntArrayAnnotation
LongArrayAnnotation
InferenceArrayAnnotation
ByteAnnotation
ShortAnnotation
IntAnnotation
LongAnnotation
FloatAnnotation
DoubleAnnotation
StringAnnotation
)
func (ta TypeAnnotation) Characters() string {
switch ta {
default:
return ""
case CompoundAnnotation:
return "C"
case ByteArrayAnnotation:
return "B;"
case IntArrayAnnotation:
return "I;"
case LongArrayAnnotation:
return "L;"
case InferenceArrayAnnotation:
return ";"
case ByteAnnotation:
return "b"
case ShortAnnotation:
return "s"
case IntAnnotation:
return "i"
case LongAnnotation:
return "L"
case FloatAnnotation:
return "f"
case DoubleAnnotation:
return "d"
case StringAnnotation:
return "S"
}
}
func (ta TypeAnnotation) StringSubtype(sub string) string {
if ta == NoAnnotation {
return ""
}
if sub == "" {
return fmt.Sprintf("<%s>", ta.Characters())
}
return fmt.Sprintf("<%s:%s>", ta.Characters(), sub)
}
func (ta TypeAnnotation) String() string {
return ta.StringSubtype("")
}
func AnnotationOf(v reflect.Value) TypeAnnotation {
t := v.Type()
switch v.Kind() {
default:
fmt.Printf("unknown type '%s' (%s)\n", v, v.Kind())
return InferenceAnnotation
case reflect.Map:
return CompoundAnnotation
case reflect.Slice:
switch t.Elem().Kind() {
case reflect.Int8:
return ByteArrayAnnotation
case reflect.Int32, reflect.Uint16:
return IntArrayAnnotation
case reflect.Int64, reflect.Uint32:
return LongArrayAnnotation
}
switch t {
case reflect.TypeOf(ByteArray{}):
return ByteArrayAnnotation
case reflect.TypeOf(IntArray{}):
return IntArrayAnnotation
case reflect.TypeOf(LongArray{}):
return LongArrayAnnotation
}
fmt.Printf("unknown array type '%s' ([]%s)\n", t, t.Elem())
return InferenceArrayAnnotation
case reflect.Struct:
if t == reflect.TypeOf(List{}) {
return NoAnnotation
}
fmt.Printf("unknown struct type '%s'\n", t)
return InferenceAnnotation
case reflect.Int8:
return ByteAnnotation
case reflect.Int16, reflect.Uint8:
return ShortAnnotation
case reflect.Int32, reflect.Uint16:
return IntAnnotation
case reflect.Int64, reflect.Uint32:
return LongAnnotation
case reflect.Float32:
return FloatAnnotation
case reflect.Float64:
return DoubleAnnotation
case reflect.String:
return StringAnnotation
case reflect.Pointer, reflect.Interface:
return AnnotationOf(v.Elem())
}
}