zhihu/norm

norm generate error statement

Closed this issue · 2 comments

When I used norm to generate insert statements, it generated the wrong statement, The following code:
`
package main

import (
"fmt"
"log"
"time"

"github.com/zhihu/norm"
"github.com/zhihu/norm/constants"
"github.com/zhihu/norm/dialectors"

)

func main() {
db := newGdb()
prepare(db)

insertData(db)

}

func newGdb() *norm.DB {
dalector := dialectors.MustNewNebulaDialector(dialectors.DialectorConfig{
Addresses: []string{"127.0.0.1:9669"},
Timeout: time.Second * 5,
Space: "test_data",
Username: "root",
Password: "test",
})
db := norm.MustOpen(dalector, norm.Config{})
return db
}

func prepare(db *norm.DB) {
// 创建 tag
createSchema := "" +
"CREATE TAG IF NOT EXISTS User(id int, name string, lastTime timestamp);" +
"CREATE TAG IF NOT EXISTS Game(id int, name string, userCount int, createTime timestamp);" +
"CREATE EDGE IF NOT EXISTS play(channel string, lastTime timestamp, createTime timestamp);"
_, err := db.Execute(createSchema)
if err != nil {
log.Fatalf("exec %s error: %v", createSchema, err)
panic(err)
}
}

type VUser struct {
norm.VModel
Id int64 norm:"id"
Name string norm:"name"
LastTime int64 norm:"lastTime"
}

func (*VUser) TagName() string {
return "User"
}

func (a *VUser) GetVid() interface{} {
return fmt.Sprintf("u:%d", a.Id)
}

func insertData(db *norm.DB) {
now := time.Now().Unix()
for i := 1; i <= 10; i++ {
err := db.Debug().InsertVertex(&VUser{
VModel: norm.VModel{
Policy: constants.PolicyNothing,
},
Id: int64(i),
LastTime: now,
})
if err != nil {
panic(err)
}
}
}
`
console output:
2021/12/13 21:07:16 [INFO] connection pool is initialized successfully
2021/12/13 21:07:16 [INFO] insert vertex User(id,lastTime) values '':(1,1639400836)
2021/12/13 21:07:16 [INFO] insert vertex User(lastTime,id) values '':(1639400836,2)
2021/12/13 21:07:16 [INFO] insert vertex User(id,lastTime) values '':(3,1639400836)
2021/12/13 21:07:16 [INFO] insert vertex User(id,lastTime) values '':(4,1639400836)
2021/12/13 21:07:16 [INFO] insert vertex User(id,lastTime) values '':(5,1639400836)
2021/12/13 21:07:16 [INFO] insert vertex User(id,lastTime) values '':(6,1639400836)
2021/12/13 21:07:16 [INFO] insert vertex User(id,lastTime) values '':(7,1639400836)
2021/12/13 21:07:16 [INFO] insert vertex User(id,lastTime) values '':(8,1639400836)
2021/12/13 21:07:16 [INFO] insert vertex User(id,lastTime) values '':(9,1639400836)
2021/12/13 21:07:16 [INFO] insert vertex User(id,lastTime) values '':(10,1639400836)

Here, values '' should not appear. I think the method GetVidWithPolicy don't be forced implement.

It's because norm use GetVidWithPolicy general vid, Default implement by VModel. In this case, though we overwrite VUser.GetVid(), but VModel.GetVidWithPolicy() still use VModel.GetVid(). Detail please see InsertVertex.

You can implement GetVidWithPolicy to solve this problem.

Now i am finding a way that can implement c++'s virtual function in go, Just like this,

type IVertex interface {
    GetVID() string
    GetHashVid() string
}
 
type Vertex struct {
    IVertex
}
func (*Vertex) GetVID() string {
    panic("not set")
}
func (v *Vertex) GetHashVid() string {
    if v.IVertex != nil {
        return "hash(" + v.IVertex.GetVID() + ")"
    }
    return "hash(" + v.GetVID() + ")"
}
 
type UserVertex struct {
    Vertex
}
func (*UserVertex) GetVID() string {
    return "user_123"
}
 
func main() {
    user := &UserVertex{}
    user.IVertex = user
    fmt.Println(user.GetHashVid())
}

But i am not sure is there have a better way?
Fogive my poor English.

Has remove method GetVidWithPolicy.