Skip to content

Commit

Permalink
Merge pull request #25 from u0x01/master
Browse files Browse the repository at this point in the history
add: hessian TagIdentifier supportting #issues-19
  • Loading branch information
wongoo authored May 13, 2019
2 parents 1553f0a + e9cd1b5 commit efce029
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 26 deletions.
17 changes: 12 additions & 5 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,22 @@ func (d *Decoder) decClassDef() (interface{}, error) {

func findField(name string, typ reflect.Type) (int, error) {
for i := 0; i < typ.NumField(); i++ {
str := typ.Field(i).Name
if strings.Compare(str, name) == 0 {
// matching tag first, then lowerCamelCase, SameCase, lowerCase

if val, has := typ.Field(i).Tag.Lookup(tagIdentifier); has && strings.Compare(val, name) == 0 {
return i, nil
}
// str1 := strings.Title(name)
str1 := strings.ToLower(str)
if strings.Compare(name, str1) == 0 {

fieldName := typ.Field(i).Name
switch {
case strings.Compare(lowerCamelCase(fieldName), name) == 0:
return i, nil
case strings.Compare(fieldName, name) == 0:
return i, nil
case strings.Compare(strings.ToLower(fieldName), name) == 0:
return i, nil
}

}

return 0, jerrors.Errorf("failed to find field %s", name)
Expand Down
46 changes: 26 additions & 20 deletions object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ func (Department) JavaClassName() string {
}

type WorkerInfo struct {
Name string
Addrress string
Age int
Salary float32
Payload map[string]int32
FalimyMemebers []string
Dpt Department
Name string
Addrress string
Age int
Salary float32
Payload map[string]int32
FamilyMembers []string `hessian:"familyMembers"`
FamilyPhoneNumber string // default convert to => familyPhoneNumber
Dpt Department
}

func (WorkerInfo) JavaClassName() string {
Expand All @@ -52,12 +53,13 @@ func TestEncEmptyStruct(t *testing.T) {

e = NewEncoder()
w = WorkerInfo{
Name: "Trump",
Addrress: "W,D.C.",
Age: 72,
Salary: 21000.03,
Payload: map[string]int32{"Number": 2017061118},
FalimyMemebers: []string{"m1", "m2", "m3"},
Name: "Trump",
Addrress: "W,D.C.",
Age: 72,
Salary: 21000.03,
Payload: map[string]int32{"Number": 2017061118},
FamilyMembers: []string{"m1", "m2", "m3"},
FamilyPhoneNumber: "010-12345678",
// Dpt: Department{
// Name: "Adm",
// },
Expand Down Expand Up @@ -85,17 +87,21 @@ func TestEncStruct(t *testing.T) {

e = NewEncoder()
w = WorkerInfo{
Name: "Trump",
Addrress: "W,D.C.",
Age: 72,
Salary: 21000.03,
Payload: map[string]int32{"Number": 2017061118},
FalimyMemebers: []string{"m1", "m2", "m3"},
Name: "Trump",
Addrress: "W,D.C.",
Age: 72,
Salary: 21000.03,
Payload: map[string]int32{"Number": 2017061118},
FamilyMembers: []string{"m1", "m2", "m3"},
FamilyPhoneNumber: "010-12345678",
Dpt: Department{
Name: "Adm",
},
}
e.Encode(w)
err = e.Encode(w)
if err != nil {
t.Errorf("Encode() = %v", err)
}

d = NewDecoder(e.Buffer())
res, err = d.Decode()
Expand Down
29 changes: 28 additions & 1 deletion pojo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"reflect"
"strings"
"sync"
"unicode"
)

import (
Expand All @@ -30,6 +31,22 @@ const (
InvalidJavaEnum JavaEnum = -1
)

// struct filed tag of hessian
var tagIdentifier = "hessian"

// SetTagIdentifier for customize struct filed tag of hessian, your can use it like:
//
// hessian.SetTagIdentifier("json")
//
// type MyUser struct {
// UserFullName string `json:"user_full_name"`
// FamilyPhoneNumber string // default convert to => familyPhoneNumber
// }
//
// var user MyUser
// hessian.NewEncoder().Encode(user)
func SetTagIdentifier(s string) { tagIdentifier = s }

// POJO interface
// !!! Pls attention that Every field name should be upper case.
// Otherwise the app may panic.
Expand Down Expand Up @@ -133,7 +150,11 @@ func RegisterPOJO(o POJO) int {
n = t.typ.NumField()
b = encInt32(b, int32(n))
for i = 0; i < n; i++ {
f = strings.ToLower(t.typ.Field(i).Name)
if val, has := t.typ.Field(i).Tag.Lookup(tagIdentifier); has {
f = val
} else {
f = lowerCamelCase(t.typ.Field(i).Name)
}
l = append(l, f)
b = encString(b, f)
}
Expand Down Expand Up @@ -277,3 +298,9 @@ func createInstance(goName string) interface{} {

return reflect.New(s.typ).Interface()
}

func lowerCamelCase(s string) string {
runes := []rune(s)
runes[0] = unicode.ToLower(runes[0])
return string(runes)
}

0 comments on commit efce029

Please sign in to comment.