简体   繁体   中英

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. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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