Commit 5c8cfac1 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 5d69ba25
...@@ -281,31 +281,36 @@ var typeTab = make(map[reflect.Type]*zclass) // {} type -> zclass ...@@ -281,31 +281,36 @@ var typeTab = make(map[reflect.Type]*zclass) // {} type -> zclass
// ClassOf returns ZODB class of a Go object. // ClassOf returns ZODB class of a Go object.
// //
// If ZODB class was not registered for obj's type, "" is returned. // XXX // The following is returned:
// XXX -> keep obj IPersistent and add TypeOf(interface{}) ? //
//func ClassOf(obj IPersistent) string { // - if obj's type was registered (RegisterClass) -- corresponding class.
func ClassOf(obj interface{}) string { // - for Broken objects -- ZODB.Broken("<broken-class>").
// - else -- ZODB.Go("<fully-qualified-type(obj)>")
func ClassOf(obj IPersistent) string {
zb, broken := obj.(*Broken) zb, broken := obj.(*Broken)
if broken { if broken {
return fmt.Sprintf("ZODB.Broken(%q)", zb.class) return fmt.Sprintf("ZODB.Broken(%q)", zb.class)
} }
typ := reflect.TypeOf(obj) typ := reflect.TypeOf(obj)
zc, ok := typeTab[typ.Elem()] typ = typ.Elem() // *MyPersistent -> MyPersistent
zc, ok := typeTab[typ]
if ok { if ok {
return zc.class return zc.class
} }
// XXX can we get vvv at all if obj is IPersistent? -> yes, if type was not registered. // the type was not registered to ZODB
fullType := typ.PkgPath()
// not Broken and type not registered to ZODB -> Broken(fullGoType(obj)) if typ.PkgPath() != "" {
fullType := "go:" + typ.PkgPath()
if typ.PkgPath() != "" { // it could be builtin type, e.g. string
fullType += "." fullType += "."
} }
fullType += typ.Name() fullType += typ.Name()
//return fmt.Sprintf("ZODB.Broken(%q)", fullType) if fullType == "" {
return fullType // fallback, since it is possible if the type is anonymous
// XXX not fully qualified
fullType = fmt.Sprintf("*%T", typ)
}
return fmt.Sprintf("ZODB.Go(%q)", fullType)
} }
var rIPersistent = reflect.TypeOf((*IPersistent)(nil)).Elem() // typeof(IPersistent) var rIPersistent = reflect.TypeOf((*IPersistent)(nil)).Elem() // typeof(IPersistent)
......
...@@ -51,6 +51,11 @@ func (o *myObjectState) PySetState(pystate interface{}) error { ...@@ -51,6 +51,11 @@ func (o *myObjectState) PySetState(pystate interface{}) error {
return nil return nil
} }
// Peristent that is not registered to ZODB.
type Unregistered struct {
Persistent
}
func init() { func init() {
t := reflect.TypeOf t := reflect.TypeOf
RegisterClass("t.zodb.MyObject", t(MyObject{}), t(myObjectState{})) RegisterClass("t.zodb.MyObject", t(MyObject{}), t(myObjectState{}))
...@@ -132,6 +137,10 @@ func TestPersistent(t *testing.T) { ...@@ -132,6 +137,10 @@ func TestPersistent(t *testing.T) {
checkObj(obj, nil, 12, InvalidTid, GHOST, 0, nil) checkObj(obj, nil, 12, InvalidTid, GHOST, 0, nil)
assert.Equal(ClassOf(obj), "t.zodb.MyObject") assert.Equal(ClassOf(obj), "t.zodb.MyObject")
// ClassOf(unregistered-obj)
obj2 := &Unregistered{}
assert.Equal(ClassOf(obj2), `ZODB.Go("lab.nexedi.com/kirr/neo/go/zodb.Unregistered")`)
// TODO activate - jar has to load, state changes // TODO activate - jar has to load, state changes
// TODO activate again - refcnt++ // TODO activate again - refcnt++
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment