Skip to content

Commit

Permalink
add option: host
Browse files Browse the repository at this point in the history
  • Loading branch information
uxc committed Sep 28, 2022
1 parent 4074a1a commit c739c83
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
1 change: 1 addition & 0 deletions tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var supportTag = map[string]int{
"-": 1,
"initial": 1,
"url": 2,
"host": 2,
"assign_to": 2,
"assign_type": 2,
}
Expand Down
11 changes: 6 additions & 5 deletions tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
)

type Model struct {
Id int
Name string `ts:"-"`
Sex int8 `ts:"assign_to(IsMan);assign_type(bool)"`
IsMan bool
Image string `ts:"url(http)"`
Id int
Name string `ts:"-"`
Sex int8 `ts:"assign_to(IsMan);assign_type(bool)"`
IsMan bool
Image string `ts:"url(http)"`
Avatar string `ts:"host(cdn)"`

Json string `ts:"assign_to(Object);assign_type(unmarshal)"`
Object interface{}
Expand Down
18 changes: 17 additions & 1 deletion tagsugar.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ import (
)

var (
Http = ""
Http = ""
hostMap = make(map[string]string, 0)
)

func AddHost(key string, host string) {
hostMap[key] = host
}

func Lick(data interface{}) {
v := reflect.ValueOf(data)
k := v.Kind()
Expand Down Expand Up @@ -120,6 +125,17 @@ func changeField(v reflect.Value, field reflect.Value, options tagOptions) error
}
}

host := options["host"]
if host != "" {
if field.CanSet() {
s := field.String()
value := hostMap[host]
if value != "" {
field.Set(reflect.ValueOf(value + s))
}
}
}

cName := options["assign_to"]
if cName != "" {
cFiled := v.FieldByName(cName)
Expand Down
8 changes: 8 additions & 0 deletions tagsugar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ func TestArrayJsonUnmarshal(t *testing.T) {
Lick(&model)
log.Print(model.Array)
}

func TestHost(t *testing.T) {
AddHost("cdn", "https://cdn.example.com/")
model := Model{Id: 5, Avatar: "5.png"}
log.Print(model.Avatar)
Lick(&model)
log.Print(model.Avatar)
}

0 comments on commit c739c83

Please sign in to comment.