policy

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2026 License: Apache-2.0, MIT Imports: 11 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	KindEqual              = "=="   // implemented by equality
	KindNotEqual           = "!="   // implemented by equality
	KindGreaterThan        = ">"    // implemented by equality
	KindGreaterThanOrEqual = ">="   // implemented by equality
	KindLessThan           = "<"    // implemented by equality
	KindLessThanOrEqual    = "<="   // implemented by equality
	KindNot                = "not"  // implemented by negation
	KindAnd                = "and"  // implemented by connective
	KindOr                 = "or"   // implemented by connective
	KindLike               = "like" // implemented by wildcard
	KindAll                = "all"  // implemented by quantifier
	KindAny                = "any"  // implemented by quantifier
)

Variables

This section is empty.

Functions

func ErrInvalidPattern

func ErrInvalidPattern(path string, err error) error

func ErrInvalidSelector

func ErrInvalidSelector(path string, err error) error

func ErrNotAString

func ErrNotAString(path string) error

func ErrNotATuple

func ErrNotATuple(path string) error

func ErrUnrecognizedOperator

func ErrUnrecognizedOperator(path string, op string) error

func ErrUnrecognizedShape

func ErrUnrecognizedShape(path string) error

Types

type Constructor

type Constructor func() (Statement, error)

func All

func All(selector string, cstor Constructor) Constructor

func And

func And(cstors ...Constructor) Constructor

func Any

func Any(selector string, cstor Constructor) Constructor

func Equal

func Equal(selector string, value ipld.Node) Constructor

func GreaterThan

func GreaterThan(selector string, value ipld.Node) Constructor

func GreaterThanOrEqual

func GreaterThanOrEqual(selector string, value ipld.Node) Constructor

func LessThan

func LessThan(selector string, value ipld.Node) Constructor

func LessThanOrEqual

func LessThanOrEqual(selector string, value ipld.Node) Constructor

func Like

func Like(selector string, pattern string) Constructor

func Not

func Not(cstor Constructor) Constructor

func NotEqual

func NotEqual(selector string, value ipld.Node) Constructor

func Or

func Or(cstors ...Constructor) Constructor

type Policy

type Policy []Statement
Example
package main

import (
	"fmt"

	"code.sonr.org/go/ucan/pkg/policy"
	"code.sonr.org/go/ucan/pkg/policy/literal"
)

func main() {
	pol := policy.MustConstruct(
		policy.Equal(".status", literal.String("draft")),
		policy.All(".reviewer",
			policy.Like(".email", "*@example.com"),
		),
		policy.Any(".tags", policy.Or(
			policy.Equal(".", literal.String("news")),
			policy.Equal(".", literal.String("press")),
		)),
	)

	fmt.Println(pol)

}
Output:

[
  ["==", ".status", "draft"],
  ["all", ".reviewer",
    ["like", ".email", "*@example.com"]
  ],
  ["any", ".tags",
    ["or", [
      ["==", ".", "news"],
      ["==", ".", "press"]
    ]]
  ]
]
Example (Accumulate)
package main

import (
	"fmt"

	"code.sonr.org/go/ucan/pkg/policy"
	"code.sonr.org/go/ucan/pkg/policy/literal"
)

func main() {
	var statements []policy.Constructor

	statements = append(statements, policy.Equal(".status", literal.String("draft")))

	statements = append(statements, policy.All(".reviewer",
		policy.Like(".email", "*@example.com"),
	))

	statements = append(statements, policy.Any(".tags", policy.Or(
		policy.Equal(".", literal.String("news")),
		policy.Equal(".", literal.String("press")),
	)))

	pol := policy.MustConstruct(statements...)

	fmt.Println(pol)

}
Output:

[
  ["==", ".status", "draft"],
  ["all", ".reviewer",
    ["like", ".email", "*@example.com"]
  ],
  ["any", ".tags",
    ["or", [
      ["==", ".", "news"],
      ["==", ".", "press"]
    ]]
  ]
]

func Construct

func Construct(cstors ...Constructor) (Policy, error)

func FromDagJson

func FromDagJson(json string) (Policy, error)

func FromIPLD

func FromIPLD(node datamodel.Node) (Policy, error)

func MustConstruct

func MustConstruct(cstors ...Constructor) Policy

func (Policy) Match

func (p Policy) Match(node datamodel.Node) (bool, Statement)

Match determines if the IPLD node satisfies the policy. The first Statement failing to match is returned as well.

func (Policy) PartialMatch

func (p Policy) PartialMatch(node datamodel.Node) (bool, Statement)

PartialMatch returns false IIF one non-optional Statement has the corresponding data and doesn't match. If the data is missing or the non-optional Statement is matching, true is returned.

This allows performing the policy checking in multiple steps, and find immediately if a Statement already failed. A final call to Match is necessary to make sure that the policy is fully matched, with no missing data (apart from optional values).

The first Statement failing to match is returned as well.

func (Policy) String

func (p Policy) String() string

func (Policy) ToIPLD

func (p Policy) ToIPLD() (datamodel.Node, error)

type Statement

type Statement interface {
	Kind() string
	String() string
}

Directories

Path Synopsis
Package literal holds a collection of functions to create IPLD types to use in policies, selector and args.
Package literal holds a collection of functions to create IPLD types to use in policies, selector and args.

Jump to

Keyboard shortcuts

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