types

package
v0.0.0-...-0137c81 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package types provides type inference functionality.

Even though Jsonnet doesn't have a concept of static types we can infer for each expression what values it can take. Of course we cannot do this accurately at all times, but even coarse grained information about "types" can help with some bugs. We are mostly interested in simple issues - like using a nonexistent field of an object or treating an array like a function.

Main assumptions:

  • It has to work well with existing programs.
  • It needs to be conservative - strong preference for false negatives over false positives.
  • It must be practical to use with existing Jsonnet code.
  • It should "preserve abstractions". Calling a function with some specific arguments should not cause errors in the definition. In general, reasoning about the definition from usage is not allowed.

First of all type processing split into two very distinct phases: 1) Finding a type - an upper bound for the set of possible values for each expression. 2) Checking all expressions in the AST using this information.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Check

func Check(mainNode ast.Node, roots map[string]ast.Node, vars map[string]map[ast.Node]*common.Variable, importFunc ImportFunc, ec *common.ErrCollector)

Check finds type problems in a given program. It require passing some previously processed data: * root nodes of all (transitively) imported Jsonnet files * resolution of variables in all files * importFunc which allows resolving imports

func Describe

func Describe(t *TypeDesc) string

Describe provides incomplete, but human-readable representation of a type.

Types

type ImportFunc

type ImportFunc func(currentPath, importedPath string) ast.Node

ImportFunc should provide an AST node from a given location. If a node is not available it should return nil.$

type TypeDesc

type TypeDesc struct {
	Bool         bool
	Number       bool
	String       bool
	Null         bool
	FunctionDesc *functionDesc
	ObjectDesc   *objectDesc
	ArrayDesc    *arrayDesc
}

TypeDesc is a representation of a type. This is (way) richer than the basic Jsonnet type system with seven types.

func (*TypeDesc) Any

func (t *TypeDesc) Any() bool

Any returns whether all values are allowed (i.e. we know nothing about it).

func (*TypeDesc) AnyArray

func (t *TypeDesc) AnyArray() bool

AnyArray returns whether the types contain all arrays.

func (*TypeDesc) AnyFunction

func (t *TypeDesc) AnyFunction() bool

AnyFunction returns whether the types contain all functions.

func (*TypeDesc) AnyObject

func (t *TypeDesc) AnyObject() bool

AnyObject returns whether the type contains all objects.

func (*TypeDesc) Array

func (t *TypeDesc) Array() bool

Array returns whether the types contains an array.

func (*TypeDesc) Function

func (t *TypeDesc) Function() bool

Function returns whether the types contains a function.

func (*TypeDesc) Object

func (t *TypeDesc) Object() bool

Object returns whether the types contains an object.

func (*TypeDesc) Void

func (t *TypeDesc) Void() bool

Void returns whether the type is empty (no values are possible).

Jump to

Keyboard shortcuts

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