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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog