dongqigu0429 2015-04-01 20:23
浏览 247
已采纳

Golang抽象接口切片转换

I have a list of objects (olievere/Elastic SearchResult.Hits to be exact). Each of these has a json.RawMessage object and I'm looking to create an abstractable method that takes in an interface slice of any struct, Unmarshal's each individual hits' json.RawMessage into said struct, and appends it to the passed in []interface.

This func is not supposed to have any logic or insight into the desired business layer struct, and the DB Call is interfaced pretty heavily, and as such has no visibility into the Elastic package mentioned above. Example of what I was attempting to do...

foo.go    
import (bar, package)
type TestStruct struct {    
    Slice []*package.Struct // package.Struct has a value of Source which is a    
                            // json.RawMessage    
}    

func GetData() bar.Test {
    return &TestStruct{*package.GetData()}
}

func (result TestStruct) UnmarshalStruct(v []interface{}) {    
    for _, singleStruct := range result.Slice {     
        append(json.Unmarshal(singleStruct, &v))
    }

Second File

bar.go
type Handler interface {
    GetData() Test
}

type Test interface {
    UnmarshalStruct
}

type OtherType struct {
   foo string
   bar string
} 

func RetrieveData() []OtherType {
    handler := New(Handler)
    test := handler.GetData()
    var typeSlice []OtherType    
    test.UnmarshalStruct(&typeSlice)
}

I'm looking to hand something of type []OtherType, or any other new struct I decide to create, to UnmarshalStruct, and have it return to me that same struct, just full of data

As an example, I have two different types of data I'll be searching for from Elastic. I will be receiving a list of ONE of the following two objects.

{ 'foo': '',
  'id': 
}

And in a different index

{ 'bar': '',
  'baz': '',
  'eee': ''
}     

Each of these will naturally require two different structs.
However, I desire a single method to be able to decode either of these lists. I'll be given below, and using the same function I want to be able to convert this to a bar struct, and the other type to a foo struct.

{ 'source': [
    { 'bar': '',
      'baz': '',
      'eee': ''
    },
    { 'bar': '',
      'baz': '',
      'eee': ''
    },
    { 'bar': '',
      'baz': '',
      'eee': ''
    }    
  ]
}
  • 写回答

3条回答 默认 最新

  • douyou2048 2015-04-02 14:33
    关注

    There's really no way to do what you want without reflection. I would personally structure this differently, so that you unmarshal into more generic types, like a map[string]string, or as @ThunderCat shows, get rid of the intermediary state and unamrshal directly into the correct types. But it can be done...

    (I moved the json.RawMessage directly into TestStruct to get rid of one level of indirection and make the example more clear)

    type TestStruct struct {
        Slice []json.RawMessage
    }
    
    func (t TestStruct) UnmarshalStruct(v interface{}) error {
        // get the a Value for the underlying slice
        slice := reflect.ValueOf(v).Elem()
        // make sure we have adequate capacity
        slice.Set(reflect.MakeSlice(slice.Type(), len(t.Slice), len(t.Slice)))
    
        for i, val := range t.Slice {
            err := json.Unmarshal(val, slice.Index(i).Addr().Interface())
            if err != nil {
                return err
            }
        }
    
        return nil
    }
    

    You can then call it like so

    var others []OtherType
    err := ts.UnmarshalStruct(&others)
    if err != nil {
        log.Fatal(err)
    }
    

    http://play.golang.org/p/dgly2OOXDG

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?