equivalentchecks

package
v0.3.3-0...-ea14b49 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package equivalentchecks finds the checks in a BatchCheck call that are certain to get the same answer, so an SDK can send each of them once and copy the answer back to every original check.

Two kinds of checks merge:

  • Aliases: a relation defined as just another relation on the same object, like define can_share: can_view. A chain of aliases is followed to its final relation, the first relation that isn't an alias.
  • Exact repeats: the same check passed more than once.

Checks merge only if their final relation, user, object, context and contextual tuples all match. A context matches only if the server would get exactly the same values: numbers are compared as doubles, including the sign of zero; key order in objects doesn't matter; order and repeats in lists do. No context doesn't match an empty one, and no contextual tuples don't match an empty list. Contextual tuples can be in any order. A value that can't be compared exactly is not merged.

Only plain JSON values can be compared: maps with string keys, slices, arrays, numbers, bools, nil and strings in valid UTF-8. A context holding anything else, like a struct, time.Time, json.RawMessage or a type with its own MarshalJSON, is not merged.

Every group is sent with its final relation, a group of one too. These checks are sent exactly as written, with the caller's relation, and are never merged:

  • A check whose object type, relation, user type or userset relation isn't in the model.
  • A userset on the same object whose relation is anywhere in the chain, like document:1#can_view checking can_share on document:1.
  • A check whose contextual tuples repeat a user, relation and object.
  • A check whose user, relation or object, or a contextual tuple's user, relation, object or condition name, isn't valid UTF-8. JSON encoding turns invalid UTF-8 into U+FFFD, so two different strings could reach the server as the same one.

Read each model once with NewAliases and keep the result. Aliases.Resolve doesn't change its input, and the same model and checks, in the same order, always give the same groups and correlation IDs.

The rules are in the RFC: https://github.com/openfga/rfcs/blob/main/20261106-sdk-batch-check-merge-equivalent-checks.md Every SDK runs the shared cases in tests/data/equivalent-checks-cases.json, so ports of this package give the same groups.

The package imports only the standard library, so SDKs can use it without pulling in gRPC or protobuf.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Aliases

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

Aliases knows where every relation of an authorization model ends after following its aliases. Make it once per model with NewAliases and keep it: Resolve only reads it, so it's safe for concurrent use.

func NewAliases

func NewAliases(modelJSON []byte) (*Aliases, error)

NewAliases reads an authorization model in its JSON form, as the API returns it, and works out where every relation ends after following its aliases. Do this once per model.

It reads only type_definitions[].type and type_definitions[].relations, where each relation is a userset with this, computedUserset, tupleToUserset, union, intersection or difference. Other fields are ignored, and a key set to null counts as absent. If a type is defined twice, the last definition is used. It returns an error if modelJSON isn't a JSON object, or if a field it reads has the wrong JSON type.

func (*Aliases) Resolve

func (a *Aliases) Resolve(checks []Check) []Group

Resolve groups the checks that are certain to get the same answer. It returns one group per check to send, in the order of each group's first check, and every check is in exactly one group.

Resolve doesn't change checks or a. It doesn't validate correlation IDs: they must be unique, or a merged group could get an ID a caller uses.

Example
package main

import (
	"fmt"

	"github.com/openfga/language/pkg/go/equivalentchecks"
)

func main() {
	// define viewer: [user]
	// define can_view: viewer
	// define can_share: can_view
	model := `{
	  "schema_version": "1.1",
	  "type_definitions": [
	    {"type": "user"},
	    {
	      "type": "document",
	      "relations": {
	        "viewer": {"this": {}},
	        "can_view": {"computedUserset": {"relation": "viewer"}},
	        "can_share": {"computedUserset": {"relation": "can_view"}}
	      }
	    }
	  ]
	}`

	aliases, err := equivalentchecks.NewAliases([]byte(model))
	if err != nil {
		fmt.Println(err)

		return
	}

	groups := aliases.Resolve([]equivalentchecks.Check{
		{CorrelationID: "view", User: "user:anne", Relation: "can_view", Object: "document:roadmap"},
		{CorrelationID: "share", User: "user:anne", Relation: "can_share", Object: "document:roadmap"},
		{CorrelationID: "bob-share", User: "user:bob", Relation: "can_share", Object: "document:roadmap"},
		{CorrelationID: "edit", User: "user:anne", Relation: "can_edit", Object: "document:roadmap"},
	})

	for _, group := range groups {
		fmt.Println(group.CorrelationID, group.Relation, group.Members, group.AsIs)
	}

}
Output:
merged-1 viewer [0 1] false
bob-share viewer [2] false
edit can_edit [3] true

type Check

type Check struct {
	CorrelationID string
	User          string
	Relation      string
	Object        string

	// Context is nil, or a nil map or pointer, for no context. Otherwise it's compared by
	// its JSON encoding, and only a map with string keys holding plain JSON values can be
	// compared: nil, bools, numbers, json.Number, strings in valid UTF-8, and slices,
	// arrays, maps and pointers holding these. A check whose context holds anything else,
	// like a struct, time.Time, []byte, json.RawMessage or a type with a MarshalJSON or
	// MarshalText method, isn't merged.
	Context any

	// ContextualTuples is nil for no contextual tuples. A non-nil empty slice is an empty
	// list, which doesn't match nil.
	ContextualTuples []Tuple
}

Check is one check of a BatchCheck call.

type Condition

type Condition struct {
	Name string

	// Context is compared like Check.Context.
	Context any
}

Condition is the condition of a contextual tuple.

type Group

type Group struct {
	// CorrelationID is the ID to send. A group of one, and every check sent as is, keeps
	// the caller's ID. A merged group gets merged-1, merged-2 and so on, in the order of
	// the groups, skipping any ID a caller already uses.
	CorrelationID string

	// Relation is the relation to send: the final relation, or the caller's relation when
	// AsIs is true.
	Relation string

	// Members are the indexes of the checks in the group, in ascending order. The check at
	// Members[0] gives the user, object, context and contextual tuples to send.
	Members []int

	// AsIs is true for a check sent exactly as written: with the caller's relation and ID,
	// and never merged, not even with an exact repeat.
	AsIs bool
}

Group is one check to send, and the checks its answer is copied to.

type Tuple

type Tuple struct {
	User     string
	Relation string
	Object   string

	// Condition is nil for a tuple without a condition.
	Condition *Condition
}

Tuple is a contextual tuple of a check.

Jump to

Keyboard shortcuts

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