简体   繁体   English

Golang:类型声明错误问题

[英]Golang: Type Assertion Error issue

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. 我在以下位置进行类型断言itr = itr。(* DbIterator).Iterator和key:= itr.Key()。(* Key).Slice和value:= itr.Value()。(* Value)。切片。 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 我有一个Interator界面

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

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. 但是,是的,就像我说的那样,当您到达该代码时, itr的类型不是代码所期望的,因此它不会以期望的结果结尾,您必须在此处进行一些恢复/错误处理才能使其正常工作最好还是先弄清楚您是如何传递错误的类型的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM