bilibili / gengine

Other
1.95k stars 272 forks source link

isNil() 问题 #54

Open keroro6 opened 2 years ago

keroro6 commented 2 years ago
const rule2 = `

rule "bot_ask_name" "" salience 12 begin

    println("conversation_list")

    if ( isNil( BaseData.UserProfile)  ||  BaseData.UserProfile.Name == "") && lastConversationTime(Base, "", "", "10", "70", "") {
        return true
    }
    return false

end ` base := BaseStartConversation{ ConversationBaseData: ConversationBaseData{ ConversationLogic: *logicObj, UserId: 9819, NodeId: 9818, Node: &httpparams.GetOneRobotNodeResp{ NodeId: 9818, RobotNodeGeneralProperties: &robotnodedao.RobotNodeGeneralProperties{NickName: "node nick name"}, RobotNodeCharacterProperties: nil, }, }, } apis := make(map[string]interface{}) apis["println"] = fmt.Println apis["index"] = strings.Index pool, err := engine.NewGenginePool(10, 20, 1, rule1, apis)

data := make(map[string]interface{})

data["BaseData"] = base.ConversationBaseData
data["Base"] = &base
data["println"] = fmt.Println
data["lastConversationTime"] = lastConversationTime
err = pool.UpdatePooledRulesIncremental(rule2)
//lastConversationTime()
t.Log(err)
assert.Assert(t, err == nil)
err, result := pool.ExecuteSelectedRules(data, []string{"bot_ask_name"})
t.Log(err)
assert.Assert(t, err == nil)
t.Log(result)

其中UserProfile是BaseData的一个结构体指针成员, 报错信息如下 conversation_list --- FAIL: Test_gengine_pool_bot_inform_city_bot_ask_weather (0.08s) panic: reflect: call of reflect.Value.FieldByName on zero Value [recovered] panic: reflect: call of reflect.Value.FieldByName on zero Value

这个 我测试了,就是isNil执行完后,虽然命中了条件(BaseData.UserProfile==nil),但是还会继续 || 后面的那个BaseData.UserProfile.Name,访问了空指针,想问下这个执行逻辑是什么?isNil 不能搭配 || && 使用吗