convert

package
v0.0.1 Latest Latest
Warning

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

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

Documentation

Overview

Package convert is responsible for all things related to implicit conversions. This includes inserting necessary implicit conversions into the model at parse time and finding the exact match overload at run time in the interpreter.

Index

Constants

This section is empty.

Variables

View Source
var ErrAmbiguousMatch = errors.New("ambiguous match")

ErrAmbiguousMatch is returned when two or more overloads were matched with the same score.

View Source
var ErrNoMatch = errors.New("no matching overloads")

ErrNoMatch is returned when no overloads were matched.

Functions

func DeDuplicate

func DeDuplicate(ts []types.IType) (types.IType, error)

DeDuplicate finds a minimal choice type given a list of types. Duplicates are removed and Choice types are recursively flattened. No implicit conversions are applied. Ex: [Integer, String, Choice<Integer, Quantity>, String] will return Choice<Integer, Quantity, String>

func ExactOverloadMatch

func ExactOverloadMatch[F any](invoked []types.IType, overloads []Overload[F], modelinfo *modelinfo.ModelInfos, name string) (F, error)

ExactOverloadMatch returns F on a match, and an error if there is no match or if the match is ambiguous. Matches must be exact meaning the invoked operands are equal or a subtype of the matched overload. If there are two exact matches an ambiguous error is returned.

func Intersect

func Intersect(left types.IType, right types.IType) (types.IType, error)

Intersect finds the intersection of two types. Choice types are flattened and no implicit conversions are applied. Ex Integer, Choice<Integer, Quantity> will return Integer.

func OperandsToString

func OperandsToString(operands []model.IExpression) string

OperandsToString returns a print friendly representation of the operands.

func OperandsToTypes

func OperandsToTypes(operands []model.IExpression) []types.IType

OperandsToTypes returns the types of the ResultType of the operands.

Types

type ConvertedOperand

type ConvertedOperand struct {
	// Matched is true if the invoked type can be converted to the declared type.
	Matched bool
	// Score is the least converting score of the conversion based on the CQL conversion precedence.
	// The score does not take into account multiple conversions.
	// https://cql.hl7.org/03-developersguide.html#conversion-precedence
	Score int
	// WrappedOperand is the operand wrapped in all necessary system operators and function refs to
	// convert it.
	WrappedOperand model.IExpression
}

ConvertedOperand is the result of OperandImplicitConverter.

func OperandImplicitConverter

func OperandImplicitConverter(invokedType types.IType, declaredType types.IType, opToWrap model.IExpression, mi *modelinfo.ModelInfos) (ConvertedOperand, error)

OperandImplicitConverter wraps the operand in any system operators or FHIRHelper function references needed to convert the operand from invokedType to declaredType. For example, if going from Integer --> Decimal the operand will be wrapped in ToDecimal(operand). operandImplicitConverter may apply multiple conversions, and always returns the least converting path.

OperandImplicitConverter may be called with a nil opToWrap if the caller only cares about the score.

Implementation note, invokedType may not be the same as opToWrap.GetResultType().The two diverge on some recursive calls. opToWrap.GetResultType() should not be used in the implementation of operandImplicitConverter.

type Generic

type Generic string

Generic types are used to define generic overloads, the overloads with T in https://cql.hl7.org/09-b-cqlreference.html. The only place generics are used is to define system operators in the Parser. The ResultType in model should never be a Generic type. The interpreter should never deal with generic types.

const (
	// GenericType represents a generic CQL type, shown as T in the CQL reference. Never nest a
	// GenericType in a real type (ex List<GenericType>). Instead use the GenericList below.
	GenericType Generic = "GenericType"
	// GenericInterval represents a generic interval type, shown as Interval<T> in the CQL reference.
	GenericInterval Generic = "GenericInterval"
	// GenericList represents a generic list type, shown as List<T> in the CQL reference.
	GenericList Generic = "GenericList"
)

func (Generic) Equal

func (s Generic) Equal(a types.IType) bool

Equal is a strict equal. X.Equal(Y) is true when X and Y are the exact same types.

func (Generic) MarshalJSON

func (s Generic) MarshalJSON() ([]byte, error)

MarshalJSON should never be called for Generics.

func (Generic) ModelInfoName

func (s Generic) ModelInfoName() (string, error)

ModelInfoName should never be called for Generics.

func (Generic) String

func (s Generic) String() string

String returns the model info based name for the type, and implements fmt.Stringer for easy printing.

type Infered

type Infered struct {
	// PuntedToChoice is true if all types could not be implicitly converted to a uniform type and we
	// instead converted to a Choice type.
	PuntedToChoice bool
	// UniformType is the type that all invoked types were converted to.
	UniformType types.IType
	// WrappedOperands are the invoked operands wrapped in all necessary system operators and function
	// refs to convert them.
	WrappedOperands []model.IExpression
}

Infered is the result of the InferMixedType function.

func InferMixed

func InferMixed(invoked []model.IExpression, modelinfo *modelinfo.ModelInfos) (Infered, error)

InferMixed wraps the invoked operands in all necessary system operators and function refs to convert them to the same type. As a last resort, InferMixed will convert all operands to a Choice types consisting of the set of types of the operands. The least converting path is chosen and on ambiguous paths any of the least converting paths is chosen. This function is used to infer the type of mixed lists and case expressions in the parser.

[4, 4.5] --> [ToDecimal(4), 4.5] ['str', 4] --> [As{'str', Choice<String, Integer>}, As{4, Choice<String, Integer>}]

type MatchedOverload

type MatchedOverload[F any] struct {
	// Result is the result of the overload that was matched.
	Result F
	// WrappedOperands are the operands wrapped in all necessary system operators and function refs to
	// convert them to the matched overload.
	WrappedOperands []model.IExpression
}

MatchedOverload returns the result of the OverloadMatch function.

func OverloadMatch

func OverloadMatch[F any](invoked []model.IExpression, overloads []Overload[F], modelinfo *modelinfo.ModelInfos, name string) (MatchedOverload[F], error)

OverloadMatch returns MatchedOverload on a match, and an error if there is no match or if the match is ambiguous. OverloadMatch returns the least converting match by summing the conversion score of each of the arguments. Ambiguous is returned if the least converting match has the same conversion score. Example of an ambiguous match:

Declared: Foo(Long), Foo(Decimal) Invocation: Foo(Integer)

Name is the function name and is only used for error messages. https://cql.hl7.org/03-developersguide.html#function-resolution https://cql.hl7.org/03-developersguide.html#conversion-precedence

type Overload

type Overload[F any] struct {
	Operands []types.IType
	// Result is what is returned by OverloadMatch.
	Result F
}

Overload holds the the declared operands and the result returned if those operands are matched by an invocation.

Jump to

Keyboard shortcuts

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