Skip to content

Commit b60279b

Browse files
lib/go: Handle the PreviouslyKnownAs annotation seamlessly
1 parent 69fb5d5 commit b60279b

File tree

11 files changed

+289
-124
lines changed

11 files changed

+289
-124
lines changed

lib/go/go.mod

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module github.com/upfluence/thrift/lib/go
2+
3+
go 1.23.0
4+
5+
require (
6+
github.com/stretchr/testify v1.9.0
7+
github.com/upfluence/errors v0.2.11
8+
)
9+
10+
require (
11+
github.com/davecgh/go-spew v1.1.1 // indirect
12+
github.com/pmezard/go-difflib v1.0.0 // indirect
13+
gopkg.in/yaml.v3 v3.0.1 // indirect
14+
)

lib/go/go.sum

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
6+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
7+
github.com/upfluence/errors v0.2.11 h1:v0siQatgm/ovihotNy++k2AXYk136VLVi4cdxAIFo1s=
8+
github.com/upfluence/errors v0.2.11/go.mod h1:XmrnFoB1O343aOni1tqU1wZNQuRhWNDHXa/cqnFzDzE=
9+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
10+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
11+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
12+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

lib/go/test/tests/go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/upfluence/thrift/lib/go/test/tests
2+
3+
go 1.23.0

lib/go/thrift/definition.go

+15-115
Original file line numberDiff line numberDiff line change
@@ -1,134 +1,34 @@
11
package thrift
22

33
import (
4-
"fmt"
54
"reflect"
6-
"sync"
7-
)
8-
9-
var (
10-
defaultStructTypeRegistry = &structTypeRegistry{
11-
types: make(map[string]reflect.Type),
12-
defs: make(map[string]StructDefinition),
13-
}
145

15-
defaultServiceRegistry = &serviceRegistry{
16-
defs: make(map[string]ServiceDefinition),
17-
}
6+
"github.com/upfluence/thrift/lib/go/thrift/internal/reflection"
187
)
198

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
11715

11816
func RegisterService(def ServiceDefinition) {
119-
defaultServiceRegistry.registerService(def)
17+
reflection.RegisterService(def)
12018
}
12119

12220
func GetServiceDefinition(n string) (ServiceDefinition, bool) {
123-
def, ok := defaultServiceRegistry.defs[n]
124-
125-
return def, ok
21+
return reflection.GetServiceDefinition(n)
12622
}
12723

12824
func RegisterStruct(rs RegistrableStruct) {
129-
defaultStructTypeRegistry.registerStructType(rs)
25+
reflection.RegisterStruct(rs)
13026
}
13127

13228
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)
13434
}

lib/go/thrift/exception_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ func TestPrependError(t *testing.T) {
3030
if !ok {
3131
t.Fatal("Couldn't cast error TApplicationException")
3232
}
33-
if err2.Error() != "Prepend: original error" {
34-
t.Fatal("Unexpected error string")
33+
if err2.Error() != "Prepend: : original error" {
34+
t.Fatalf("Unexpected error string, has %q", err2.Error())
3535
}
3636
if err2.TypeId() != INTERNAL_ERROR {
3737
t.Fatal("Unexpected type error")
@@ -42,7 +42,7 @@ func TestPrependError(t *testing.T) {
4242
if !ok {
4343
t.Fatal("Couldn't cast error TProtocolException")
4444
}
45-
if err4.Error() != "Prepend: original error" {
45+
if err4.Error() != "Prepend: : original error" {
4646
t.Fatal("Unexpected error string")
4747
}
4848
if err4.TypeId() != INVALID_DATA {
@@ -54,7 +54,7 @@ func TestPrependError(t *testing.T) {
5454
if !ok {
5555
t.Fatal("Couldn't cast error TTransportException")
5656
}
57-
if err6.Error() != "Prepend: original error" {
57+
if err6.Error() != "Prepend: : original error" {
5858
t.Fatal("Unexpected error string")
5959
}
6060
if err6.TypeId() != TIMED_OUT {
@@ -63,7 +63,7 @@ func TestPrependError(t *testing.T) {
6363

6464
err7 := errors.New("original error")
6565
err8 := PrependError("Prepend: ", err7)
66-
if err8.Error() != "Prepend: original error" {
66+
if err8.Error() != "Prepend: : original error" {
6767
t.Fatal("Unexpected error string")
6868
}
6969
}

lib/go/thrift/field.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
package thrift
2121

22+
import "strconv"
23+
2224
// Helper class that encapsulates field metadata.
2325
type field struct {
2426
name string
@@ -55,7 +57,7 @@ func (p *field) String() string {
5557
if p == nil {
5658
return "<nil>"
5759
}
58-
return "<TField name:'" + p.name + "' type:" + string(p.typeId) + " field-id:" + string(p.id) + ">"
60+
return "<TField name:'" + p.name + "' type:" + string(p.typeId) + " field-id:" + strconv.Itoa(p.id) + ">"
5961
}
6062

6163
var ANONYMOUS_FIELD *field

0 commit comments

Comments
 (0)