Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions conv/j2t/conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,25 @@ func TestConvJSON2Thrift(t *testing.T) {
require.Equal(t, exp, act)
}

func TestConvJSON2Thrift_ForceHashMapAsFieldNameMap(t *testing.T) {
desc := getExampleDescByName("ExampleMethod", true, thrift.Options{
ForceHashMapAsFieldNameMap: true,
})
data := getExampleData()
cv := NewBinaryConv(conv.Options{})
out, err := cv.Do(context.Background(), desc, data)
require.NoError(t, err)

exp := example3.NewExampleReq()
err = json.Unmarshal(data, exp)
require.NoError(t, err)

act := example3.NewExampleReq()
_, err = act.FastRead(out)
require.NoError(t, err)
require.Equal(t, exp, act)
}

func TestConvHTTP2Thrift(t *testing.T) {
desc := getExampleDesc()
data := getExampleData()
Expand Down
16 changes: 8 additions & 8 deletions internal/util/fieldmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ type FieldNameMap struct {
maxKeyLength int
all []caching.Pair
trie *caching.TrieTree
hash map[string]unsafe.Pointer
hash *caching.HashMap
}

func NewFieldNameMap() *FieldNameMap {
return &FieldNameMap{
hash: make(map[string]unsafe.Pointer, defaultMapSize),
hash: caching.NewHashMap(defaultMapSize, defaultHashMapLoadFactor),
}
}

Expand Down Expand Up @@ -68,7 +68,7 @@ func (ft FieldNameMap) Get(k string) unsafe.Pointer {
if ft.trie != nil {
return (unsafe.Pointer)(ft.trie.Get(k))
} else if ft.hash != nil {
return (unsafe.Pointer)(ft.hash[k])
return (unsafe.Pointer)(ft.hash.Get(k))
}
return nil
}
Expand All @@ -81,7 +81,7 @@ func (ft FieldNameMap) All() []caching.Pair {
// Size returns the size of the map
func (ft FieldNameMap) Size() int {
if ft.hash != nil {
return len(ft.hash)
return ft.hash.Size()
} else if ft.trie != nil {
return ft.trie.Size()
}
Expand Down Expand Up @@ -162,18 +162,18 @@ func (ft *FieldNameMap) Build(noTrieTree bool) {

// no ideal position or force use hash map
ft.trie = nil
ft.hash = make(map[string]unsafe.Pointer, len(ft.all))
ft.hash = caching.NewHashMap(len(ft.all), defaultHashMapLoadFactor)
// set all key-values to the trie tree
for _, v := range ft.all {
// caching.HashMap does not support duplicate key, so must check if the key exists before set
// WARN: if the key exists, the value WON'T be replaced
o := ft.hash[v.Key]
o := ft.hash.Get(v.Key)
if o == nil {
ft.hash[v.Key] = v.Val
ft.hash.Set(v.Key, v.Val)
}
}
if empty != nil {
ft.hash[""] = empty
ft.hash.Set("", empty)
}
}

Expand Down
Loading