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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效