У меня есть этот обработчик:
func (h Handler) makeGetMany(v PeopleInjection) http.HandlerFunc {
type RespBody struct {
TypeCreatorMeta string `type:"bar",tc_resp_body_type:"true"`
}
type ReqBody struct {
TypeCreatorMeta string `type:"star",tc_req_body_type:"true"`
Handle string
}
return tc.ExtractType(
tc.TypeList{ReqBody{},RespBody{}},
func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(v.People)
})
}
Функция tc.ExtractType выглядит так:
type TypeList = []interface{}
func ExtractType(s TypeList, h http.HandlerFunc) http.HandlerFunc {
m := make(map[string]string)
for _, v := range s {
t := reflect.TypeOf(v)
f, _ := t.FieldByName("TypeCreatorMeta")
fmt.Println("All tags?:",f.Tag);
v, ok := f.Tag.Lookup("type")
if !ok {
fmt.Println("no 'type' tag.");
continue;
}
for _, key := range []string{"tc_req_body_type", "tc_resp_body_type"} {
_, ok := f.Tag.Lookup(key)
fmt.Println(ok,"key:",key) // <<<< important
if ok {
m[key] = v
}
}
}
return func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Req: %s\n", r.URL.Path)
h.ServeHTTP(w, r)
}
}
она может найти тег "type", который указывает на "star", но по какой-то причине он не получает тег "tc_resp_body_type", который указывает на "true".Вот что я выхожу из системы:
All tags?: type:"star",tc_req_body_type:"true"
false key: tc_req_body_type
false key: tc_resp_body_type
Кто-нибудь знает, почему можно найти тег "type", но не удается найти "tc_req_body_type"?