custom_builtins

package
v1.12.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 17, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var GetHeaderDecl = &ast.Builtin{
	Name: "get_header",
	Decl: types.NewFunction(
		types.Args(
			types.S,
			types.A,
		),
		types.S,
	),
}

GetHeader returns the first value corresponding (in case-insensitive mode) to the headerKey in the headers of the request, otherwise return an empty string if does not exist.

View Source
var GetHeaderFunction = rego.Function2(
	&rego.Function{
		Name: GetHeaderDecl.Name,
		Decl: GetHeaderDecl.Decl,
	},
	func(_ rego.BuiltinContext, a, b *ast.Term) (*ast.Term, error) {
		var headerKey string
		var headers http.Header
		if err := ast.As(a.Value, &headerKey); err != nil {
			return nil, err
		}
		if err := ast.As(b.Value, &headers); err != nil {
			return nil, err
		}
		return ast.StringTerm(headers.Get(headerKey)), nil
	},
)
View Source
var MongoFindMany = rego.Function2(
	&rego.Function{
		Name: MongoFindManyDecl.Name,
		Decl: MongoFindManyDecl.Decl,
	},
	func(ctx rego.BuiltinContext, collectionNameTerm, queryTerm *ast.Term) (*ast.Term, error) {
		mongoClient, err := GetMongoClientFromContext(ctx.Context)
		if err != nil {
			return nil, err
		}
		if mongoClient == nil {
			return nil, fmt.Errorf("mongo client not set")
		}

		var collectionName string
		if err := ast.As(collectionNameTerm.Value, &collectionName); err != nil {
			return nil, err
		}

		query := make(map[string]interface{})
		if err := ast.As(queryTerm.Value, &query); err != nil {
			return nil, err
		}

		result, err := mongoClient.FindMany(ctx.Context, collectionName, query)
		if err != nil {
			return nil, err
		}

		t, err := ast.InterfaceToValue(result)
		if err != nil {
			return nil, err
		}

		return ast.NewTerm(t), nil
	},
)
View Source
var MongoFindManyDecl = &ast.Builtin{
	Name: "find_many",
	Decl: types.NewFunction(
		types.Args(
			types.S,
			types.A,
		),
		types.A,
	),
}
View Source
var MongoFindOne = rego.Function2(
	&rego.Function{
		Name: MongoFindOneDecl.Name,
		Decl: MongoFindOneDecl.Decl,
	},
	func(ctx rego.BuiltinContext, collectionNameTerm, queryTerm *ast.Term) (*ast.Term, error) {
		mongoClient, err := GetMongoClientFromContext(ctx.Context)
		if err != nil {
			return nil, err
		}
		if mongoClient == nil {
			return nil, fmt.Errorf("mongo client not set")
		}

		var collectionName string
		if err := ast.As(collectionNameTerm.Value, &collectionName); err != nil {
			return nil, err
		}

		query := make(map[string]interface{})
		if err := ast.As(queryTerm.Value, &query); err != nil {
			return nil, err
		}

		result, err := mongoClient.FindOne(ctx.Context, collectionName, query)
		if err != nil {
			return nil, err
		}

		t, err := ast.InterfaceToValue(result)
		if err != nil {
			return nil, err
		}

		return ast.NewTerm(t), nil
	},
)
View Source
var MongoFindOneDecl = &ast.Builtin{
	Name: "find_one",
	Decl: types.NewFunction(
		types.Args(
			types.S,
			types.A,
		),
		types.A,
	),
}

Functions

func WithMongoClient added in v1.10.0

func WithMongoClient(ctx context.Context, mongoClient IMongoClient) context.Context

Types

type IMongoClient added in v1.10.0

type IMongoClient interface {
	FindOne(ctx context.Context, collectionName string, query map[string]interface{}) (interface{}, error)
	FindMany(ctx context.Context, collectionName string, query map[string]interface{}) ([]interface{}, error)
}

func GetMongoClientFromContext added in v1.10.0

func GetMongoClientFromContext(ctx context.Context) (IMongoClient, error)

func NewMongoClient added in v1.10.0

func NewMongoClient(logger logging.Logger, mongoClient types.MongoClient) (IMongoClient, error)

type MongoClient added in v1.10.0

type MongoClient struct {
	// contains filtered or unexported fields
}

func (*MongoClient) FindMany added in v1.10.0

func (mongoClient *MongoClient) FindMany(ctx context.Context, collectionName string, query map[string]interface{}) ([]interface{}, error)

func (*MongoClient) FindOne added in v1.10.0

func (mongoClient *MongoClient) FindOne(ctx context.Context, collectionName string, query map[string]interface{}) (interface{}, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL