|
1 | 1 | package thrift
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "fmt" |
5 | 4 | "reflect"
|
6 |
| - "sync" |
7 |
| -) |
8 |
| - |
9 |
| -var ( |
10 |
| - defaultStructTypeRegistry = &structTypeRegistry{ |
11 |
| - types: make(map[string]reflect.Type), |
12 |
| - defs: make(map[string]StructDefinition), |
13 |
| - } |
14 | 5 |
|
15 |
| - defaultServiceRegistry = &serviceRegistry{ |
16 |
| - defs: make(map[string]ServiceDefinition), |
17 |
| - } |
| 6 | + "github.com/upfluence/thrift/lib/go/thrift/internal/reflection" |
18 | 7 | )
|
19 | 8 |
|
20 |
| -type serviceRegistry struct { |
21 |
| - mu sync.RWMutex |
22 |
| - |
23 |
| - defs map[string]ServiceDefinition |
24 |
| -} |
25 |
| - |
26 |
| -func (str *serviceRegistry) registerService(sd ServiceDefinition) { |
27 |
| - str.mu.Lock() |
28 |
| - defer str.mu.Unlock() |
29 |
| - |
30 |
| - str.defs[sd.CanonicalName()] = sd |
31 |
| -} |
32 |
| - |
33 |
| -type structTypeRegistry struct { |
34 |
| - mu sync.RWMutex |
35 |
| - |
36 |
| - defs map[string]StructDefinition |
37 |
| - types map[string]reflect.Type |
38 |
| -} |
39 |
| - |
40 |
| -func (str *structTypeRegistry) registerStructType(rs RegistrableStruct) { |
41 |
| - t := reflect.TypeOf(rs) |
42 |
| - |
43 |
| - if t.Kind() == reflect.Ptr { |
44 |
| - t = t.Elem() |
45 |
| - } |
46 |
| - |
47 |
| - sd := rs.StructDefinition() |
48 |
| - n := sd.CanonicalName() |
49 |
| - |
50 |
| - str.mu.Lock() |
51 |
| - defer str.mu.Unlock() |
52 |
| - |
53 |
| - str.types[n] = t |
54 |
| - str.defs[n] = sd |
55 |
| -} |
56 |
| - |
57 |
| -func (str *structTypeRegistry) structType(n string) (reflect.Type, bool) { |
58 |
| - str.mu.Lock() |
59 |
| - defer str.mu.Unlock() |
60 |
| - |
61 |
| - t, ok := str.types[n] |
62 |
| - |
63 |
| - return t, ok |
64 |
| -} |
65 |
| - |
66 |
| -type AnnotatedDefintion struct { |
67 |
| - Name string |
68 |
| - |
69 |
| - LegacyAnnotations map[string]string |
70 |
| - SturcturedAnnotations []RegistrableStruct |
71 |
| -} |
72 |
| - |
73 |
| -type FieldDefinition struct { |
74 |
| - AnnotatedDefintion |
75 |
| -} |
76 |
| - |
77 |
| -type StructDefinition struct { |
78 |
| - AnnotatedDefintion |
79 |
| - |
80 |
| - Namespace string |
81 |
| - |
82 |
| - IsException bool |
83 |
| - IsUnion bool |
84 |
| - |
85 |
| - Fields []FieldDefinition |
86 |
| -} |
87 |
| - |
88 |
| -func (sd StructDefinition) CanonicalName() string { |
89 |
| - return fmt.Sprintf("%s.%s", sd.Namespace, sd.Name) |
90 |
| -} |
91 |
| - |
92 |
| -type FunctionDefinition struct { |
93 |
| - AnnotatedDefintion |
94 |
| - |
95 |
| - IsOneway bool |
96 |
| - |
97 |
| - Args StructDefinition |
98 |
| - Result *StructDefinition |
99 |
| -} |
100 |
| - |
101 |
| -type ServiceDefinition struct { |
102 |
| - AnnotatedDefintion |
103 |
| - |
104 |
| - Namespace string |
105 |
| - |
106 |
| - Functions []FunctionDefinition |
107 |
| -} |
108 |
| - |
109 |
| -func (sd ServiceDefinition) CanonicalName() string { |
110 |
| - return fmt.Sprintf("%s.%s", sd.Namespace, sd.Name) |
111 |
| -} |
112 |
| - |
113 |
| -type RegistrableStruct interface { |
114 |
| - TStruct |
115 |
| - StructDefinition() StructDefinition |
116 |
| -} |
| 9 | +type AnnotatedDefinition = reflection.AnnotatedDefinition |
| 10 | +type FieldDefinition = reflection.FieldDefinition |
| 11 | +type StructDefinition = reflection.StructDefinition |
| 12 | +type FunctionDefinition = reflection.FunctionDefinition |
| 13 | +type ServiceDefinition = reflection.ServiceDefinition |
| 14 | +type RegistrableStruct = reflection.RegistrableStruct |
117 | 15 |
|
118 | 16 | func RegisterService(def ServiceDefinition) {
|
119 |
| - defaultServiceRegistry.registerService(def) |
| 17 | + reflection.RegisterService(def) |
120 | 18 | }
|
121 | 19 |
|
122 | 20 | func GetServiceDefinition(n string) (ServiceDefinition, bool) {
|
123 |
| - def, ok := defaultServiceRegistry.defs[n] |
124 |
| - |
125 |
| - return def, ok |
| 21 | + return reflection.GetServiceDefinition(n) |
126 | 22 | }
|
127 | 23 |
|
128 | 24 | func RegisterStruct(rs RegistrableStruct) {
|
129 |
| - defaultStructTypeRegistry.registerStructType(rs) |
| 25 | + reflection.RegisterStruct(rs) |
130 | 26 | }
|
131 | 27 |
|
132 | 28 | func StructType(n string) (reflect.Type, bool) {
|
133 |
| - return defaultStructTypeRegistry.structType(n) |
| 29 | + return reflection.StructType(n) |
| 30 | +} |
| 31 | + |
| 32 | +func ExtractCanonicalNames(ns string, def AnnotatedDefinition) []string { |
| 33 | + return reflection.ExtractCanonicalNames(ns, def) |
134 | 34 | }
|
0 commit comments