完善es engine
parent
3a49c9f4c9
commit
92c76111b8
|
@ -10,44 +10,43 @@ import (
|
||||||
"gopkg.in/olivere/elastic.v7"
|
"gopkg.in/olivere/elastic.v7"
|
||||||
"qiniupkg.com/x/log.v7"
|
"qiniupkg.com/x/log.v7"
|
||||||
)
|
)
|
||||||
const(
|
|
||||||
|
const (
|
||||||
ERROR_PTR = "null pointer error"
|
ERROR_PTR = "null pointer error"
|
||||||
INPUT_TYPE_ERROR = "wrong input parameter"
|
INPUT_TYPE_ERROR = "wrong input parameter"
|
||||||
CREATED_ERROR = "create error"
|
CREATED_ERROR = "create error"
|
||||||
DELETE_ERROR = "delete error"
|
DELETE_ERROR = "delete error"
|
||||||
INDEX_EXISTED = "index existed"
|
INDEX_EXISTED = "index existed"
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ElkEngine struct {
|
type ElkEngine struct {
|
||||||
cli *elastic.Client
|
cli *elastic.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *ElkEngine) Create(index string, types string, id string, data interface{}) error {
|
||||||
|
if nil != p {
|
||||||
func (p *ElkEngine)Create(index string,types string,id string,data interface{}) (error) {
|
if (reflect.TypeOf(data).Kind() == reflect.String) || (reflect.TypeOf(data).Kind() == reflect.Struct) {
|
||||||
if nil != p{
|
|
||||||
if (reflect.TypeOf(data).Kind() == reflect.String) || (reflect.TypeOf(data).Kind() == reflect.Struct){
|
|
||||||
resp, err := p.cli.Index().
|
resp, err := p.cli.Index().
|
||||||
Index(index).
|
Index(index).
|
||||||
BodyJson(data).
|
BodyJson(data).
|
||||||
Do(context.Background())
|
Do(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print("create error",err)
|
log.Print("create error", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Print(resp)
|
log.Print(resp)
|
||||||
}else{
|
} else {
|
||||||
log.Print(reflect.TypeOf(data).Kind())
|
log.Print(reflect.TypeOf(data).Kind())
|
||||||
return errors.New(INPUT_TYPE_ERROR)
|
return errors.New(INPUT_TYPE_ERROR)
|
||||||
}
|
}
|
||||||
}else{
|
} else {
|
||||||
return errors.New(ERROR_PTR)
|
return errors.New(ERROR_PTR)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ElkEngine)Delete(query elastic.Query,index string) error{
|
func (p *ElkEngine) Delete(query elastic.Query, index string) error {
|
||||||
if nil != p{
|
if nil != p {
|
||||||
_, err := p.cli.DeleteByQuery().Index(index).Query(query).
|
_, err := p.cli.DeleteByQuery().Index(index).Query(query).
|
||||||
Do(context.Background())
|
Do(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -55,64 +54,63 @@ func (p *ElkEngine)Delete(query elastic.Query,index string) error{
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
}else{
|
} else {
|
||||||
return errors.New(ERROR_PTR)
|
return errors.New(ERROR_PTR)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
|
|
||||||
*/
|
func (p *ElkEngine) Query(index string, query elastic.Query, typ reflect.Type,
|
||||||
func (p *ElkEngine)Query(index string,query elastic.Query,typ reflect.Type,
|
limit int, offset int) ([]interface{}, []string, error) {
|
||||||
limit int,offset int) ([]interface{},[]string,error) {
|
|
||||||
rets := []interface{}{}
|
rets := []interface{}{}
|
||||||
if nil != p{
|
if nil != p {
|
||||||
if(limit == 0){
|
if limit == 0 {
|
||||||
res, err := p.cli.Search(index).Query(query).Do(context.Background())
|
res, err := p.cli.Search(index).Query(query).Do(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
print(err)
|
print(err)
|
||||||
return nil,nil,err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
id := []string{}
|
id := []string{}
|
||||||
for _,vs := range res.Hits.Hits{
|
for _, vs := range res.Hits.Hits {
|
||||||
id = append(id,vs.Id)
|
id = append(id, vs.Id)
|
||||||
}
|
}
|
||||||
return rets,id,nil
|
return rets, id, nil
|
||||||
}else{
|
} else {
|
||||||
res, err := p.cli.Search(index).Query(query).Size(limit).From(limit*offset).Do(context.Background())
|
res, err := p.cli.Search(index).Query(query).Size(limit).From(limit * offset).Do(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
print(err)
|
print(err)
|
||||||
return nil,nil,err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
id := []string{}
|
id := []string{}
|
||||||
for _,vs := range res.Hits.Hits{
|
for _, vs := range res.Hits.Hits {
|
||||||
id = append(id,vs.Id)
|
id = append(id, vs.Id)
|
||||||
data,e := vs.Source.MarshalJSON()
|
data, e := vs.Source.MarshalJSON()
|
||||||
if nil != e{
|
if nil != e {
|
||||||
log.Print(e.Error())
|
log.Print(e.Error())
|
||||||
}
|
}
|
||||||
obj := utils.ReflectMakeNew(typ)
|
|
||||||
mapobj := map[string]interface{}{}
|
mapobj := map[string]interface{}{}
|
||||||
e = json.Unmarshal(data,&mapobj)
|
e = json.Unmarshal(data, &mapobj)
|
||||||
if nil != e{
|
if nil != e {
|
||||||
|
log.Print(e.Error())
|
||||||
|
}
|
||||||
|
obj, e := utils.UnmarshalJson2StructGen(typ, mapobj)
|
||||||
|
if nil != e {
|
||||||
log.Print(e.Error())
|
log.Print(e.Error())
|
||||||
}
|
}
|
||||||
// for k,_ := range mapobj{
|
// for k,_ := range mapobj{
|
||||||
// value := reflect.ValueOf(obj)
|
// value := reflect.ValueOf(obj)
|
||||||
// }
|
// }
|
||||||
log.Print(obj)
|
rets = append(rets, obj)
|
||||||
rets = append(rets,obj)
|
|
||||||
log.Print(string(data))
|
|
||||||
}
|
}
|
||||||
return rets,id,nil
|
return rets, id, nil
|
||||||
}
|
}
|
||||||
}else{
|
} else {
|
||||||
return nil,nil,errors.New(ERROR_PTR)
|
return nil, nil, errors.New(ERROR_PTR)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ElkEngine)Update(index string,types string,id string,data map[string]interface{}) error {
|
func (p *ElkEngine) Update(index string, types string, id string, data map[string]interface{}) error {
|
||||||
if nil != p {
|
if nil != p {
|
||||||
_, err := p.cli.Update().
|
_, err := p.cli.Update().
|
||||||
Index(index).
|
Index(index).
|
||||||
|
@ -126,7 +124,8 @@ func (p *ElkEngine)Update(index string,types string,id string,data map[string]in
|
||||||
}
|
}
|
||||||
return errors.New(ERROR_PTR)
|
return errors.New(ERROR_PTR)
|
||||||
}
|
}
|
||||||
func (p *ElkEngine)CreateIndex(index string,typemaping string) error{
|
|
||||||
|
func (p *ElkEngine) CreateIndex(index string, typemaping string) error {
|
||||||
if nil != p {
|
if nil != p {
|
||||||
exists, err := p.cli.IndexExists(index).Do(context.Background())
|
exists, err := p.cli.IndexExists(index).Do(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -134,7 +133,7 @@ func (p *ElkEngine)CreateIndex(index string,typemaping string) error{
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if exists{
|
if exists {
|
||||||
return errors.New(INDEX_EXISTED)
|
return errors.New(INDEX_EXISTED)
|
||||||
}
|
}
|
||||||
createIndex, err := p.cli.CreateIndex(index).Body(typemaping).Do(context.Background())
|
createIndex, err := p.cli.CreateIndex(index).Body(typemaping).Do(context.Background())
|
||||||
|
@ -151,19 +150,18 @@ func (p *ElkEngine)CreateIndex(index string,typemaping string) error{
|
||||||
return errors.New(ERROR_PTR)
|
return errors.New(ERROR_PTR)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ElkEngine)IndexExisted(index string) (bool,error ){
|
func (p *ElkEngine) IndexExisted(index string) (bool, error) {
|
||||||
if nil != p {
|
if nil != p {
|
||||||
exists, err := p.cli.IndexExists(index).Do(context.Background())
|
exists, err := p.cli.IndexExists(index).Do(context.Background())
|
||||||
if exists{
|
if exists {
|
||||||
return true,nil
|
return true, nil
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Handle error
|
// Handle error
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
return false,err
|
return false, err
|
||||||
}
|
}
|
||||||
return false,nil
|
return false, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
return false,nil
|
return false, nil
|
||||||
}
|
}
|
|
@ -55,22 +55,21 @@ func TestPortDocToElastic(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReflect(t *testing.T) {
|
||||||
func TestReflect(t *testing.T){
|
type XX struct {
|
||||||
type XX struct{
|
|
||||||
A int16 `json:"bb"`
|
A int16 `json:"bb"`
|
||||||
B string `json: "cc" xml:"test"`
|
B string `json: "cc" xml:"test"`
|
||||||
}
|
}
|
||||||
unmar := map[string] interface{}{
|
unmar := map[string]interface{}{
|
||||||
"bb" : 2,
|
"bb": 2,
|
||||||
"cc": "hello",
|
"cc": "hello",
|
||||||
}
|
}
|
||||||
xx := XX{}
|
xx := XX{}
|
||||||
utils.UnmarshalJson(&xx,unmar)
|
utils.UnmarshalJson2Struct(&xx, unmar)
|
||||||
log.Print(xx)
|
log.Print(xx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestQueryDoc(t *testing.T){
|
func TestQueryDoc(t *testing.T) {
|
||||||
InitConfig()
|
InitConfig()
|
||||||
InitLogs()
|
InitLogs()
|
||||||
InitRedisConfig()
|
InitRedisConfig()
|
||||||
|
@ -78,11 +77,18 @@ func TestQueryDoc(t *testing.T){
|
||||||
db.InitELK()
|
db.InitELK()
|
||||||
|
|
||||||
query := elastic.NewTermQuery("content", "title")
|
query := elastic.NewTermQuery("content", "title")
|
||||||
//_ = elastic.NewQueryStringQuery(fieldValue) //关键字查询
|
searchResult, titles, e := db.GetElastic().Query("doc", query, reflect.TypeOf(model.Doc{}), 10, 0)
|
||||||
searchResult,titles,e := db.GetElastic().Query("doc",query,reflect.TypeOf(model.Doc{}),10,0)
|
if nil != e {
|
||||||
if nil != e{
|
|
||||||
log.Print(e.Error())
|
log.Print(e.Error())
|
||||||
}
|
}
|
||||||
log.Print(searchResult)
|
log.Print(searchResult)
|
||||||
log.Print(titles)
|
log.Print(titles)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestChangeStructFieldThroughStruct(t *testing.T) {
|
||||||
|
type XX struct {
|
||||||
|
A int16 `json:"bb"`
|
||||||
|
B string `json: "cc" xml:"test"`
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
205
utils/reflect.go
205
utils/reflect.go
|
@ -4,45 +4,54 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ErrorTypeError = "error input interface type error"
|
||||||
|
TagNoExisted = "error remap tag no existed"
|
||||||
|
)
|
||||||
|
|
||||||
func ReflectMakeNew(t reflect.Type) interface{} {
|
func reflectMakeNew(t reflect.Type) interface{} {
|
||||||
retptr := reflect.New(t)
|
retptr := reflect.New(t)
|
||||||
sval := retptr.Elem().Interface()
|
sval := retptr.Elem().Interface()
|
||||||
return sval
|
return sval
|
||||||
}
|
}
|
||||||
|
|
||||||
type TagMap map[string] string
|
type TagMap map[string]string
|
||||||
|
|
||||||
func ReflectTagMap(t reflect.Type) map[string] TagMap{
|
func ReflectTagMap(t reflect.Type) map[string]TagMap {
|
||||||
log.Print(t.Name())
|
// log.Print(t.Kind(), " and ", t.Name())
|
||||||
ret := make(map[string] TagMap)
|
if t.Kind() != reflect.Struct {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
ret := make(map[string]TagMap)
|
||||||
num := t.NumField()
|
num := t.NumField()
|
||||||
for i := 0;i < num;i++{
|
for i := 0; i < num; i++ {
|
||||||
sub := strings.Split(string(t.Field(i).Tag),"\"")
|
sub := strings.Split(string(t.Field(i).Tag), "\"")
|
||||||
for k,v := range(sub){
|
for k, v := range sub {
|
||||||
if len(v) > 0{
|
if len(v) > 0 {
|
||||||
if k%2 == 0{
|
if k%2 == 0 {
|
||||||
v = strings.Trim(v," ")
|
v = strings.Trim(v, " ")
|
||||||
}
|
}
|
||||||
if v[len(v) - 1] == ':'{
|
if v[len(v)-1] == ':' {
|
||||||
if _,ok := ret[v[:len(v) - 1]];ok{
|
if _, ok := ret[v[:len(v)-1]]; ok {
|
||||||
if ((k + 1) < len(sub) - 1) && ((sub[k + 1][len(sub[k + 1]) - 1] ) == ':'){
|
if ((k + 1) < len(sub)-1) && ((sub[k+1][len(sub[k+1])-1]) == ':') {
|
||||||
continue;
|
continue
|
||||||
}else{
|
} else {
|
||||||
ret[v[:len(v) - 1]][sub[k + 1]] = string(t.Field(i).Name)
|
ret[v[:len(v)-1]][sub[k+1]] = string(t.Field(i).Name)
|
||||||
}
|
}
|
||||||
}else{
|
} else {
|
||||||
ret[v[:len(v) - 1]] = make(TagMap)
|
ret[v[:len(v)-1]] = make(TagMap)
|
||||||
log.Print()
|
log.Print()
|
||||||
if ((k + 1) < len(sub) - 1) && ((sub[k + 1][len(sub[k + 1]) - 1] ) == ':'){
|
if ((k + 1) < len(sub)-1) && ((sub[k+1][len(sub[k+1])-1]) == ':') {
|
||||||
continue;
|
continue
|
||||||
}else{
|
} else {
|
||||||
ret[v[:len(v) - 1]][sub[k + 1]] = string(t.Field(i).Name)
|
ret[v[:len(v)-1]][sub[k+1]] = string(t.Field(i).Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}else{
|
} else {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,77 +61,77 @@ func ReflectTagMap(t reflect.Type) map[string] TagMap{
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func SameKind(typ1 reflect.Kind,typ2 reflect.Kind) bool{
|
func SameKind(typ1 reflect.Kind, typ2 reflect.Kind) bool {
|
||||||
switch typ1{
|
switch typ1 {
|
||||||
case reflect.Int:
|
case reflect.Int:
|
||||||
if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){
|
if typ2 == reflect.Int || typ2 == reflect.Int16 || typ2 == reflect.Int32 || typ2 == reflect.Int64 || typ2 == reflect.Int8 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){
|
if typ2 == reflect.Uint || typ2 == reflect.Uint16 || typ2 == reflect.Uint32 || typ2 == reflect.Uint64 || typ2 == reflect.Uint8 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
case reflect.Int8:
|
case reflect.Int8:
|
||||||
if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){
|
if typ2 == reflect.Int || typ2 == reflect.Int16 || typ2 == reflect.Int32 || typ2 == reflect.Int64 || typ2 == reflect.Int8 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){
|
if typ2 == reflect.Uint || typ2 == reflect.Uint16 || typ2 == reflect.Uint32 || typ2 == reflect.Uint64 || typ2 == reflect.Uint8 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
case reflect.Int16:
|
case reflect.Int16:
|
||||||
if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){
|
if typ2 == reflect.Int || typ2 == reflect.Int16 || typ2 == reflect.Int32 || typ2 == reflect.Int64 || typ2 == reflect.Int8 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){
|
if typ2 == reflect.Uint || typ2 == reflect.Uint16 || typ2 == reflect.Uint32 || typ2 == reflect.Uint64 || typ2 == reflect.Uint8 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
case reflect.Int32:
|
case reflect.Int32:
|
||||||
if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){
|
if typ2 == reflect.Int || typ2 == reflect.Int16 || typ2 == reflect.Int32 || typ2 == reflect.Int64 || typ2 == reflect.Int8 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){
|
if typ2 == reflect.Uint || typ2 == reflect.Uint16 || typ2 == reflect.Uint32 || typ2 == reflect.Uint64 || typ2 == reflect.Uint8 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
case reflect.Uint:
|
case reflect.Uint:
|
||||||
if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){
|
if typ2 == reflect.Int || typ2 == reflect.Int16 || typ2 == reflect.Int32 || typ2 == reflect.Int64 || typ2 == reflect.Int8 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){
|
if typ2 == reflect.Uint || typ2 == reflect.Uint16 || typ2 == reflect.Uint32 || typ2 == reflect.Uint64 || typ2 == reflect.Uint8 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
case reflect.Uint8:
|
case reflect.Uint8:
|
||||||
if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){
|
if typ2 == reflect.Int || typ2 == reflect.Int16 || typ2 == reflect.Int32 || typ2 == reflect.Int64 || typ2 == reflect.Int8 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){
|
if typ2 == reflect.Uint || typ2 == reflect.Uint16 || typ2 == reflect.Uint32 || typ2 == reflect.Uint64 || typ2 == reflect.Uint8 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
case reflect.Uint16:
|
case reflect.Uint16:
|
||||||
if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){
|
if typ2 == reflect.Int || typ2 == reflect.Int16 || typ2 == reflect.Int32 || typ2 == reflect.Int64 || typ2 == reflect.Int8 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){
|
if typ2 == reflect.Uint || typ2 == reflect.Uint16 || typ2 == reflect.Uint32 || typ2 == reflect.Uint64 || typ2 == reflect.Uint8 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
case reflect.Uint32:
|
case reflect.Uint32:
|
||||||
if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){
|
if typ2 == reflect.Int || typ2 == reflect.Int16 || typ2 == reflect.Int32 || typ2 == reflect.Int64 || typ2 == reflect.Int8 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){
|
if typ2 == reflect.Uint || typ2 == reflect.Uint16 || typ2 == reflect.Uint32 || typ2 == reflect.Uint64 || typ2 == reflect.Uint8 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
case reflect.Uint64:
|
case reflect.Uint64:
|
||||||
if(typ2 == reflect.Int ||typ2 == reflect.Int16 ||typ2 == reflect.Int32 ||typ2 == reflect.Int64||typ2 == reflect.Int8){
|
if typ2 == reflect.Int || typ2 == reflect.Int16 || typ2 == reflect.Int32 || typ2 == reflect.Int64 || typ2 == reflect.Int8 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if(typ2 == reflect.Uint ||typ2 == reflect.Uint16 ||typ2 == reflect.Uint32 ||typ2 == reflect.Uint64||typ2 == reflect.Uint8){
|
if typ2 == reflect.Uint || typ2 == reflect.Uint16 || typ2 == reflect.Uint32 || typ2 == reflect.Uint64 || typ2 == reflect.Uint8 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
case reflect.Float32:
|
case reflect.Float32:
|
||||||
if(typ2 == reflect.Float32 ||typ2 == reflect.Float64){
|
if typ2 == reflect.Float32 || typ2 == reflect.Float64 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
case reflect.Float64:
|
case reflect.Float64:
|
||||||
if(typ2 == reflect.Float32 ||typ2 == reflect.Float64){
|
if typ2 == reflect.Float32 || typ2 == reflect.Float64 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
@ -131,26 +140,26 @@ func SameKind(typ1 reflect.Kind,typ2 reflect.Kind) bool{
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func UnmarshalWithTag(value interface{}, maps map[string]interface{},tag string){
|
func UnmarshalWithTag(value interface{}, maps map[string]interface{}, tag string) {
|
||||||
if "" == tag{
|
if "" == tag {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
valueof := reflect.ValueOf(value)
|
valueof := reflect.ValueOf(value)
|
||||||
if !valueof.Elem().CanAddr(){
|
if !valueof.Elem().CanAddr() {
|
||||||
log.Print("should be addr")
|
log.Print("should be addr")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
remap := ReflectTagMap(valueof.Elem().Type())
|
remap := ReflectTagMap(valueof.Elem().Type())
|
||||||
_,ok := remap[tag]
|
_, ok := remap[tag]
|
||||||
if !ok{
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for k,v := range(maps){
|
for k, v := range maps {
|
||||||
log.Print(k)
|
log.Print(k)
|
||||||
if filedName,ok := remap["json"][k];ok{
|
if filedName, ok := remap["json"][k]; ok {
|
||||||
log.Print(valueof.Elem().FieldByName(filedName).Kind(),reflect.TypeOf(v).Kind().String())
|
log.Print(valueof.Elem().FieldByName(filedName).Kind(), reflect.TypeOf(v).Kind().String())
|
||||||
if SameKind(valueof.Elem().FieldByName(filedName).Kind(),reflect.TypeOf(v).Kind()){
|
if SameKind(valueof.Elem().FieldByName(filedName).Kind(), reflect.TypeOf(v).Kind()) {
|
||||||
if(valueof.Elem().FieldByName(filedName).CanSet()){
|
if valueof.Elem().FieldByName(filedName).CanSet() {
|
||||||
valueof.Elem().FieldByName(filedName).Set(reflect.ValueOf(v).Convert(valueof.Elem().FieldByName(filedName).Type()))
|
valueof.Elem().FieldByName(filedName).Set(reflect.ValueOf(v).Convert(valueof.Elem().FieldByName(filedName).Type()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -158,29 +167,81 @@ func UnmarshalWithTag(value interface{}, maps map[string]interface{},tag string)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
an generaly unmarshal function implement
|
||||||
func UnmarshalJson(value interface{}, maps map[string]interface{}){
|
accept only pointer to struct or pointer to interface contains struct
|
||||||
|
*/
|
||||||
|
func UnmarshalJson2Struct(value interface{}, maps map[string]interface{}) error {
|
||||||
|
log.Print("UnmarshalJson kind is ", reflect.ValueOf(value).Kind())
|
||||||
|
log.Print("UnmarshalJson kind1 is ", reflect.ValueOf(value).Elem().Kind())
|
||||||
|
// log.Print("UnmarshalJson kind2 is ", reflect.ValueOf(value).Elem().Elem().Kind())
|
||||||
valueof := reflect.ValueOf(value)
|
valueof := reflect.ValueOf(value)
|
||||||
if !valueof.Elem().CanAddr(){
|
var opStruct reflect.Value
|
||||||
log.Print("should be addr")
|
if valueof.Kind() == reflect.Ptr {
|
||||||
return
|
// if it is ptr to interface,the interface must contains a struct type
|
||||||
|
if valueof.Elem().Kind() == reflect.Interface {
|
||||||
|
if valueof.Elem().Elem().Kind() == reflect.Struct {
|
||||||
|
opStruct = valueof.Elem().Elem()
|
||||||
|
} else {
|
||||||
|
return errors.New(ErrorTypeError)
|
||||||
}
|
}
|
||||||
remap := ReflectTagMap(valueof.Elem().Type())
|
} else if valueof.Elem().Kind() == reflect.Struct {
|
||||||
_,ok := remap["json"]
|
// simply it is ptr to struct
|
||||||
if !ok{
|
opStruct = valueof.Elem()
|
||||||
return
|
} else {
|
||||||
|
return errors.New(ErrorTypeError)
|
||||||
}
|
}
|
||||||
for k,v := range(maps){
|
} else {
|
||||||
|
return errors.New(ErrorTypeError)
|
||||||
|
}
|
||||||
|
|
||||||
|
remap := ReflectTagMap(opStruct.Type())
|
||||||
|
_, ok := remap["json"]
|
||||||
|
if !ok {
|
||||||
|
return errors.New(TagNoExisted)
|
||||||
|
}
|
||||||
|
for k, v := range maps {
|
||||||
log.Print(k)
|
log.Print(k)
|
||||||
if filedName,ok := remap["json"][k];ok{
|
if filedName, ok := remap["json"][k]; ok {
|
||||||
log.Print(valueof.Elem().FieldByName(filedName).Kind(),reflect.TypeOf(v).Kind().String())
|
log.Print(opStruct.FieldByName(filedName).Kind(), reflect.TypeOf(v).Kind().String())
|
||||||
if SameKind(valueof.Elem().FieldByName(filedName).Kind(),reflect.TypeOf(v).Kind()){
|
if SameKind(opStruct.FieldByName(filedName).Kind(), reflect.TypeOf(v).Kind()) {
|
||||||
if(valueof.Elem().FieldByName(filedName).CanSet()){
|
if opStruct.FieldByName(filedName).CanSet() {
|
||||||
valueof.Elem().FieldByName(filedName).Set(reflect.ValueOf(v).Convert(valueof.Elem().FieldByName(filedName).Type()))
|
opStruct.FieldByName(filedName).Set(reflect.ValueOf(v).Convert(opStruct.FieldByName(filedName).Type()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
an generaly unmarshal function implement
|
||||||
|
accept only pointer to struct or pointer to interface contains struct
|
||||||
|
*/
|
||||||
|
func UnmarshalJson2StructGen(t reflect.Type, maps map[string]interface{}) (interface{}, error) {
|
||||||
|
if t.Kind() != reflect.Struct {
|
||||||
|
return nil, errors.New(ErrorTypeError)
|
||||||
|
}
|
||||||
|
remap := ReflectTagMap(t)
|
||||||
|
_, ok := remap["json"]
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New(TagNoExisted)
|
||||||
|
}
|
||||||
|
ret := reflect.New(t)
|
||||||
|
// log.Print(ret.Kind())
|
||||||
|
opStruct := ret.Elem()
|
||||||
|
// log.Print(opStruct.Kind())
|
||||||
|
for k, v := range maps {
|
||||||
|
log.Print(k)
|
||||||
|
if filedName, ok := remap["json"][k]; ok {
|
||||||
|
// log.Print(opStruct.FieldByName(filedName).Kind(), reflect.TypeOf(v).Kind().String())
|
||||||
|
if SameKind(opStruct.FieldByName(filedName).Kind(), reflect.TypeOf(v).Kind()) {
|
||||||
|
// log.Print(opStruct.FieldByName(filedName).CanAddr(), opStruct.FieldByName(filedName).CanSet())
|
||||||
|
if opStruct.FieldByName(filedName).CanSet() {
|
||||||
|
opStruct.FieldByName(filedName).Set(reflect.ValueOf(v).Convert(opStruct.FieldByName(filedName).Type()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret.Elem().Interface(), nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue