subtype_gen

package
v1.9.3 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

README

Rules

A rules file contains zero or more rules. Each rule consists of two properties:

  • The super-type (super) for which this rule applies to, and
  • The predicate (predicate) defining the conditions that needs to be satisfied by a type to be a subtype of this super-type.
rules :
  - super: T1
    predicate: P1

  - super: T2
    predicate: P2
    
  ...

Predicates

A predicate is a condition that would become either true or false depending on the inputs. Below are the different types of predicates that can be used to define the subtype rules for a given super-type.

Always Predicate (always)

Represents a condition that is always true.

Never Predicate (never)

Represents a condition that is always false.

Not Predicate (not)

Negates a nested predicate.

And Predicate (and)

Represent a list of nested predicates that needs to be satisfied. An and predicate becomes true only if all of its nested predicates are also true. e.g:

and:
  - P1
  - P2
Or Predicate (or)

Represent a list of nested predicate that could be satisfied. An or predicate becomes true if at least one of its nested predicates are also true. e.g:

or:
  - P1
  - P2
Equals Predicate (equals)

Checks the equality between two values using the == operator. Consist of two properties, source and target, both of which can be any expression.

Example 1:

equals:
  source: super
  target: sub

Example 2:

equals:
  source: sub
  target:
    oneOf: [PrivatePathType, PublicPathType]
Subtype Predicate (subtype)

Check whether one value is a subtype of another. Consist of two fields, source and target, both can be any expression.

Example 1:

equals:
  source: super
  target: sub

Example 2:

equals:
  source: sub
  target:
    oneOf: [PrivatePathType, PublicPathType]
Type-assertion Predicate (mustType)

Uses mustType keyword. Can be used to assert that a value belong to a certain type. e.g:

- mustType:
    source: super
    type: CompositeType
Set-Contains Predicate (setContains)

Check whether a set contains a given value. e.g:

- setContains:
    set: super.EffectiveInterfaceConformanceSet
    element: sub
Permits Predicate (permits)

Check whether one set of authorization permits the access of the second set of authorization. e.g:

- permits:
    super: super.Authorization
    sub: sub.Authorization
Is-Intersection-Subset Predicate (isIntersectionSubset)

Check whether the interfaces of sub is a subset of the interface set of the super. e.g:

- isIntersectionSubset:
    super: super
    sub: sub
Other function specific predicates

There are some more predicates that checks for specific conditions:

isResource

Checks whether the type provided is a resource type. e.g:

isResource: sub
isAttachment

Checks whether the type provided is an attachment type. e.g:

isAttachment: sub
isHashableStruct

Checks whether the type provided is a hashable struct type. e.g:

isHashableStruct: sub
isStorable

Checks whether the type provided is a storable type. e.g:

isStorable: sub

Variables

Defining custom variables to hold values inside a rule is currently not supported. However, there are two variables that are supported by default: super and sub.

super variable

Holds the type-value of the super-type passed on to the function. The super-type will always have the same type as the type T specified in the super field of a rule.

For example:

  - super: AnyStructType
    predicate:
      equals:
        source: super  # super is a AnyStructType
        target: nil

In the above, super variable inside equals predicate contains a type-value of type AnyStructType.

Further, if super was used inside a type-assertion predicate (mustType) having target type as R, then any subsequent predicates that are combined with this type-assertion (using an and predicate), would see the type of super as R.

  - super: AnyStructType
    predicate:
      and:
      - mustType:              # type assertion for `super`
          source: super
          type: CompositeType
      - equals:
          source: super        # `super` is a `CompositeType`
          target: nil
sub variable

Holds the type-value of the subtype passed on to the function. Unlike super, the subtype will always belong to root type of the type implementation, at the start.

For example:

  - super: AnyStructType
    predicate:
      equals:
        source: sub  # `sub` is a `Type`
        target: nil

In the above, sub variable inside equals predicate contains a type-value of the root type Type.

However, if sub was used inside a type-assertion predicate (mustType) having target type as R, then any subsequent predicates that are combined with this type-assertion (using an and predicate), would see the type of super as R.

  - super: AnyStructType
    predicate:
      and:
      - mustType:              # type assertion for `sub`
          source: sub
          type: CompositeType
      - equals:
          source: sub         # `sub` is a `CompositeType`
          target: nil

Expressions

Predicates have properties/fields, and these properties sometimes require referring to different values, types, functions, fields, to properly define the subtyping rules. Expressions can be used to represent/access such information.

Identifier expression

Any name is an identifier expression. e.g: sub and super are identifiers that refer to variables.

Type expression

Special kind of identifier expression that refer to types. They always ends with the Type suffix. e.g:

  - super: AnyStructType
    predicate:
      and:
      - mustType:
          source: sub
          type: CompositeType  # `CompositeType` is a type-expression, referring to a type.

Member expression

Can be used to access fields of a variable. e.g:

  - super: DictionaryType
    predicate:
      and:
      - equals:
          source: super.KeyType    # Refers to the `keyType` field/property of `DictionaryType`.
          target: String

One-Of-Types expression

Used to represent that the value of the expression can be a one-of multiple types. It's a convenient way to represent an or relationship in a concise way. e.g:

  - super: DictionaryType
    predicate:
      and:
      - equals:
          source: super.KeyType
          target:
            oneOf: [Int, String, Bool]  # Says the `keyType` field/property of `DictionaryType`.
                                        # can be one of Int, String, Bool.

Documentation

Index

Constants

View Source
const (
	TypePlaceholderSuffix = "Type"

	TypePlaceholderStorable      = "Storable"
	TypePlaceholderOptional      = "Optional"
	TypePlaceholderReference     = "Reference"
	TypePlaceholderDictionary    = "Dictionary"
	TypePlaceholderVariableSized = "VariableSized"
	TypePlaceholderConstantSized = "ConstantSized"
	TypePlaceholderIntersection  = "Intersection"
	TypePlaceholderFunction      = "Function"
	TypePlaceholderComposite     = "Composite"
	TypePlaceholderInterface     = "Interface"
	TypePlaceholderParameterized = "Parameterized"
	TypePlaceholderConforming    = "Conforming"

	FieldNameReferencedType = "ReferencedType"
)

Variables

This section is empty.

Functions

func WriteGoFile

func WriteGoFile(outFile *os.File, decls []dst.Decl, packagePath string)

Types

type AlwaysPredicate

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

AlwaysPredicate represents an always-true condition.

func (AlwaysPredicate) Append

func (p AlwaysPredicate) Append(comments string) description

func (AlwaysPredicate) Description

func (p AlwaysPredicate) Description() string

type AndPredicate

type AndPredicate struct {
	Predicates []Predicate `yaml:"and"`
	// contains filtered or unexported fields
}

AndPredicate represents a logical AND predicate.

func (AndPredicate) Append

func (p AndPredicate) Append(comments string) description

func (AndPredicate) Description

func (p AndPredicate) Description() string

type ComplexType

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

func (ComplexType) Name

func (t ComplexType) Name() string

type Config

type Config struct {
	// Prefixes and the suffixes to be added to the type-placeholder
	// to customize the type-names to match the naming conventions.
	// e.g: `PrimitiveStaticTypeString` at runtime, vs `StringType` at checking time.
	SimpleTypePrefix  string
	SimpleTypeSuffix  string
	ComplexTypePrefix string
	ComplexTypeSuffix string

	// Extra parameters to be added to the generated function signatures,
	// other than the common parameters.
	// For e.g: runtime generated function takes a `TypeConverter` as an extra argument.
	ExtraParams []ExtraParam

	// Types to be skipped from generating a subtype-check.
	// For e.g: Runtime doesn't have a `StorableType`
	SkipTypes map[string]struct{}

	// A set indicating what complex types needed to be treated as
	// non-pointer types in the generated Go code.
	NonPointerTypes map[string]struct{}

	// A mapping to customize the generated names.
	// For e.g: A field named `Foo` in the `rules.yaml`,
	// can be generated as `Bar` in the generated Go code,
	// by adding a mapping from `Foo -> Bar`.
	NameMapping map[string]string

	// List of arguments to be passed on to the `ElementType` method on array-type.
	ArrayElementTypeMethodArgs []any
}

type DeepEqualsPredicate

type DeepEqualsPredicate struct {
	Source Expression `yaml:"source"`
	Target Expression `yaml:"target"`
	// contains filtered or unexported fields
}

DeepEqualsPredicate represents a deep equality check, defined with `Equals` method.

func (DeepEqualsPredicate) Append

func (p DeepEqualsPredicate) Append(comments string) description

func (DeepEqualsPredicate) Description

func (p DeepEqualsPredicate) Description() string

type EqualsPredicate

type EqualsPredicate struct {
	Source Expression `yaml:"source"`
	Target Expression `yaml:"target"`
	// contains filtered or unexported fields
}

EqualsPredicate represents an equality check using `==` operator.

func (EqualsPredicate) Append

func (p EqualsPredicate) Append(comments string) description

func (EqualsPredicate) Description

func (p EqualsPredicate) Description() string

type Expression

type Expression interface {
	// contains filtered or unexported methods
}

type ExtraParam

type ExtraParam struct {
	Name    string
	Type    string
	PkgPath string
}

type ForAllPredicate

type ForAllPredicate struct {
	Source    Expression `yaml:"source"`
	Target    Expression `yaml:"target"`
	Predicate Predicate  `yaml:"predicate"`
	// contains filtered or unexported fields
}

func (ForAllPredicate) Append

func (p ForAllPredicate) Append(comments string) description

func (ForAllPredicate) Description

func (p ForAllPredicate) Description() string

type IdentifierExpression

type IdentifierExpression struct {
	Name string
}

type IsAttachmentPredicate

type IsAttachmentPredicate struct {
	Expression Expression `yaml:"isAttachment"`
	// contains filtered or unexported fields
}

IsAttachmentPredicate represents an attachment type check.

func (IsAttachmentPredicate) Append

func (p IsAttachmentPredicate) Append(comments string) description

func (IsAttachmentPredicate) Description

func (p IsAttachmentPredicate) Description() string

type IsHashableStructPredicate

type IsHashableStructPredicate struct {
	Expression Expression `yaml:"isHashableStruct"`
	// contains filtered or unexported fields
}

IsHashableStructPredicate represents a hashable struct type check.

func (IsHashableStructPredicate) Append

func (p IsHashableStructPredicate) Append(comments string) description

func (IsHashableStructPredicate) Description

func (p IsHashableStructPredicate) Description() string

type IsIntersectionSubsetPredicate

type IsIntersectionSubsetPredicate struct {
	Sub   Expression `yaml:"sub"`
	Super Expression `yaml:"super"`
	// contains filtered or unexported fields
}

func (IsIntersectionSubsetPredicate) Append

func (p IsIntersectionSubsetPredicate) Append(comments string) description

func (IsIntersectionSubsetPredicate) Description

func (p IsIntersectionSubsetPredicate) Description() string

type IsParameterizedSubtypePredicate

type IsParameterizedSubtypePredicate struct {
	Sub   Expression `yaml:"sub"`
	Super Expression `yaml:"super"`
	// contains filtered or unexported fields
}

func (IsParameterizedSubtypePredicate) Append

func (p IsParameterizedSubtypePredicate) Append(comments string) description

func (IsParameterizedSubtypePredicate) Description

func (p IsParameterizedSubtypePredicate) Description() string

type IsResourcePredicate

type IsResourcePredicate struct {
	Expression Expression `yaml:"isResource"`
	// contains filtered or unexported fields
}

IsResourcePredicate represents a resource type check.

func (IsResourcePredicate) Append

func (p IsResourcePredicate) Append(comments string) description

func (IsResourcePredicate) Description

func (p IsResourcePredicate) Description() string

type IsStorablePredicate

type IsStorablePredicate struct {
	Expression Expression `yaml:"isStorable"`
	// contains filtered or unexported fields
}

IsStorablePredicate represents a storable type check.

func (IsStorablePredicate) Append

func (p IsStorablePredicate) Append(comments string) description

func (IsStorablePredicate) Description

func (p IsStorablePredicate) Description() string

type KeyValues

type KeyValues = map[string]ast.Node

type MemberExpression

type MemberExpression struct {
	Parent     Expression
	MemberName string
}

type NeverPredicate

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

NeverPredicate represents a never-true condition.

func (NeverPredicate) Append

func (p NeverPredicate) Append(comments string) description

func (NeverPredicate) Description

func (p NeverPredicate) Description() string

type NotPredicate

type NotPredicate struct {
	Predicate Predicate `yaml:"not"`
	// contains filtered or unexported fields
}

NotPredicate represents a logical NOT predicate.

func (NotPredicate) Append

func (p NotPredicate) Append(comments string) description

func (NotPredicate) Description

func (p NotPredicate) Description() string

type OneOfExpression

type OneOfExpression struct {
	Expressions []Expression
}

type OrPredicate

type OrPredicate struct {
	Predicates []Predicate `yaml:"or"`
	// contains filtered or unexported fields
}

OrPredicate represents a logical OR predicate.

func (OrPredicate) Append

func (p OrPredicate) Append(comments string) description

func (OrPredicate) Description

func (p OrPredicate) Description() string

type ParsingError

type ParsingError struct {
	Message string
	Path    string
	ast.Range
}

func NewParsingError

func NewParsingError(message string, node yaml.Node) ParsingError

func (ParsingError) Error

func (p ParsingError) Error() string

type PermitsPredicate

type PermitsPredicate struct {
	Sub   Expression `yaml:"sub"`
	Super Expression `yaml:"super"`
	// contains filtered or unexported fields
}

PermitsPredicate represents a permits check.

func (PermitsPredicate) Append

func (p PermitsPredicate) Append(comments string) description

func (PermitsPredicate) Description

func (p PermitsPredicate) Description() string

type Predicate

type Predicate interface {
	Description() string
	// contains filtered or unexported methods
}

Predicate represents different types of predicates in rules.

type Predicates

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

Predicates is a collection of predicates.

func NewPredicateChain

func NewPredicateChain(predicates []Predicate) *Predicates

type ReturnCovariantPredicate

type ReturnCovariantPredicate struct {
	Source Expression `yaml:"source"`
	Target Expression `yaml:"target"`
	// contains filtered or unexported fields
}

ReturnCovariantPredicate represents a return covariant check.

func (ReturnCovariantPredicate) Append

func (p ReturnCovariantPredicate) Append(comments string) description

func (ReturnCovariantPredicate) Description

func (p ReturnCovariantPredicate) Description() string

type Rule

type Rule struct {
	SuperType Type      `yaml:"super"`
	Predicate Predicate `yaml:"predicate"`
	// contains filtered or unexported fields
}

Rule represents a single subtype rule

func (Rule) Append

func (p Rule) Append(comments string) description

func (Rule) Description

func (p Rule) Description() string

type RulesFile

type RulesFile struct {
	Rules []Rule `yaml:"rules"`
	// contains filtered or unexported fields
}

RulesFile represents the entire YAML configuration

func ParseRules

func ParseRules() (rulesFile RulesFile, err error)

ParseRules reads and parses the YAML rules file

func ParseRulesFromBytes

func ParseRulesFromBytes(yamlContent []byte) (rulesFile RulesFile, err error)

func (RulesFile) Append

func (p RulesFile) Append(comments string) description

func (RulesFile) Description

func (p RulesFile) Description() string

type SetContainsPredicate

type SetContainsPredicate struct {
	Set     Expression `yaml:"set"`
	Element Expression `yaml:"element"`
	// contains filtered or unexported fields
}

func (SetContainsPredicate) Append

func (p SetContainsPredicate) Append(comments string) description

func (SetContainsPredicate) Description

func (p SetContainsPredicate) Description() string

type SimpleType

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

func (SimpleType) Name

func (t SimpleType) Name() string

type SubTypeCheckGenerator

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

func NewSubTypeCheckGenerator

func NewSubTypeCheckGenerator(config Config) *SubTypeCheckGenerator

func (*SubTypeCheckGenerator) GenerateCheckSubTypeWithoutEqualityFunction

func (gen *SubTypeCheckGenerator) GenerateCheckSubTypeWithoutEqualityFunction(rules RulesFile) []dst.Decl

GenerateCheckSubTypeWithoutEqualityFunction generates the complete checkSubTypeWithoutEquality function.

type SubTypeGenError

type SubTypeGenError struct {
	Code []byte
	Err  error
}

func (*SubTypeGenError) Error

func (e *SubTypeGenError) Error() string

type SubtypePredicate

type SubtypePredicate struct {
	Sub   Expression `yaml:"sub"`
	Super Expression `yaml:"super"`
	// contains filtered or unexported fields
}

SubtypePredicate represents a subtype check.

func (SubtypePredicate) Append

func (p SubtypePredicate) Append(comments string) description

func (SubtypePredicate) Description

func (p SubtypePredicate) Description() string

type Type

type Type interface {
	Name() string
}

Type represents parsed type information using Cadence types

type TypeAssertionPredicate

type TypeAssertionPredicate struct {
	Source Expression `yaml:"source"`
	Type   Type       `yaml:"type"`
	// contains filtered or unexported fields
}

TypeAssertionPredicate represents a type assertion.

func (TypeAssertionPredicate) Append

func (p TypeAssertionPredicate) Append(comments string) description

func (TypeAssertionPredicate) Description

func (p TypeAssertionPredicate) Description() string

type TypeExpression

type TypeExpression struct {
	Type Type
}

Jump to

Keyboard shortcuts

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