提问人:van neilsen 提问时间:6/23/2023 最后编辑:van neilsen 更新时间:6/28/2023 访问量:176
收到错误:undefined:rego。尝试读取 rego 文件时的 ReadFile
Getting Error: undefined: rego.ReadFile while trying to read rego file
问:
当我尝试编译代码时,出现以下错误。我已经导入了“github.com/open-policy-agent/opa/rego”,但仍然出现错误。
-bash-4.2$ go build main.go
# command-line-arguments
./main.go:20:26: undefined: rego.ReadFile
文件名: discount_policy.rego
package discounts
rule "Bronze Discount"
salience 10
when
Customer loyaltyLevel == "Bronze"
then
Discount.percent = 5;
Discount.message = "You have earned a 5% discount!";
end
rule "Default Discount"
salience 0
then
Discount.percent = 0;
Discount.message = "Sorry, no discount available.";
end
文件名: main.go
package main
import (
"context"
"fmt"
"github.com/open-policy-agent/opa/rego"
)
type Customer struct {
LoyaltyLevel string `json:"loyaltyLevel"`
}
type Discount struct {
Percent int `json:"percent"`
Message string `json:"message"`
}
func LoadPolicy() (*rego.Rego, error) {
regoPolicy, err := rego.ReadFile("discount_policy.rego")
if err != nil {
return nil, fmt.Errorf("failed to load policy file: %w", err)
}
return rego.New(
rego.Query("data.discounts.Discount"),
rego.Compiler(regoPolicy),
), nil
}
func main() {
// Load the GRules policy
r, err := LoadPolicy()
if err != nil {
fmt.Println("Policy loading error:", err)
return
}
}
注意:我只复制了此错误所需的代码,因为文件的代码很长。如果需要,我可以共享其余代码。我是 Go 编程的新手。
-bash-4.2$ uname -a
Linux dev-rh72-tas-1.in.intinfra.com 3.10.0-327.el7.x86_64 #1 SMP Thu Oct 29 17:29:29 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux
答:
0赞
Devoops
6/23/2023
#1
该文件看起来不像任何类似于 Rego 的文件。它从何而来?discount_policy.rego
评论
0赞
van neilsen
6/26/2023
这里discount_policy.rego文件包含GRules。我想在main.go中读取相同的文件并测试定义的规则是否有效,但不幸的是被Readfile错误卡住了。
评论
rego
ReadFile
rego
ReadFile
go build main.go
main.go