dsqbh42082 2016-09-22 21:26
浏览 70
已采纳

Golang:类型声明错误问题

I am getting bitten by the type assertion related error in the below the code snippet. I am not sure what I am missing. I am doing type assertions in the following places itr = itr.(*DbIterator).Iterator and key := itr.Key().(*Key).Slice and value := itr.Value().(*Value).Slice. I am wondering if there is a better way to do this without type assertions everywhere in the code base or better design patterns to handle this kind of scenario. The code snippet is part of a larger code base. I have pulled the most relevant bits for this question. Any help in this regard is much appreciated.

package rocksdb

import (
    "github.com/tecbot/gorocksdb"
)

func (s *RocksDB) GetCFIterator(cfHandler *gorocksdb.ColumnFamilyHandle) db.Iterator {
    opt := gorocksdb.NewDefaultReadOptions()
    opt.SetFillCache(true)
    defer opt.Destroy()
    return &DbIterator{s.DB.NewIteratorCF(opt, cfHandler)}
}

type DbIterator struct {
    *gorocksdb.Iterator
}

type Key struct {
    *gorocksdb.Slice
}

type Value struct {
    *gorocksdb.Slice
}

func (iterator *DbIterator) Key() db.Keyer {
    return &Key{iterator.Iterator.Key()}
}

func (iterator *DbIterator) Value() db.Valuer {
    return &Value{iterator.Iterator.Value()}
}

type RocksDB struct {
    DB *gorocksdb.DB
}

I have an interator interface

package db

import (
    "bytes"
    "testing"
)

type Iterator interface {
    Valid() bool
    Next()
    Close()
    SeekToFirst()
    SeekToLast()
    Seek(key []byte)
    Key() Keyer
    Value() Valuer
}

type Keyer interface {
}

type Valuer interface {
}

func testIterator(t *testing.T, itr db.Iterator, expectedValues map[string][]byte) {
    itrResults := make(map[string][]byte)
    itr = itr.(*DbIterator).Iterator //Line 270 which the error throws
    itr.SeekToFirst()
    for ; itr.Valid(); itr.Next() {
        key := itr.Key().(*Key).Slice
        value := itr.Value().(*Value).Slice
        k := makeCopy(key.Data())
        v := makeCopy(value.Data())
        itrResults[string(k)] = v
    }
    if len(itrResults) != len(expectedValues) {
        t.Fatalf("Expected [%d] results from iterator, found [%d]", len(expectedValues), len(itrResults))
    }
    for k, v := range expectedValues {
        if !bytes.Equal(itrResults[k], v) {
            t.Fatalf("Wrong value for key [%s]. Expected [%s], found [%s]", k, itrResults[k], v)
        }
    }
}

Error Message

github.com/hyperledger/fabric/core/db/rocksdb
core/db/rocksdb/rocksdb_test.go:270: cannot use itr.(*DbIterator).Iterator (type *gorocksdb.Iterator) as type db.Iterator in assignment:
*gorocksdb.Iterator does not implement db.Iterator (wrong type for Key method)
have Key() *gorocksdb.Slice
want Key() db.Keyer
  • 写回答

1条回答 默认 最新

  • douniewei6346 2016-09-22 21:31
    关注

    The problem isn't the type asserts, you're gonna break there regardless because the instance you're working with doesn't implement that interface. That being said you can handle this gracefully by doing;

     itr, ok = itr.(*DbIterator)
     if !ok {
         //try to recover
     }
    

    But yeah, like I said when you get to that code the type of itr is not what the code is expecting so it's not going to end with the desired result, you'll have to do some recovery/error handling here to make it work better or figure out how you're getting the wrong type passed down there in the first place.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图