Class Literal
- java.lang.Object
-
- software.amazon.smithy.rulesengine.language.util.MandatorySourceLocation
-
- software.amazon.smithy.rulesengine.language.syntax.expr.Expression
-
- software.amazon.smithy.rulesengine.language.syntax.expr.Literal
-
- All Implemented Interfaces:
FromSourceLocation,ToNode,TypeCheck
public final class Literal extends Expression
Literals allow rules and properties to define arbitrarily nested JSON structures (e.g.for properties)They support template strings, but _do not_ support template objects since that creates ambiguity.
Templates are a basic example of literals–literal strings. Literals can also be booleans, objects, integers or tuples.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceLiteral.Vistor<T>
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> Taccept(Literal.Vistor<T> visitor)Invokes the providedLiteral.Vistor.<R> Raccept(ExpressionVisitor<R> visitor)Invoke theExpressionVisitorfunctions for this expression.java.util.Optional<java.lang.Boolean>asBool()Attempts to convert the literal to aBooleanif possible.java.util.Optional<java.lang.Integer>asInteger()Attempts to convert the literal to anLiteral.Integer.java.util.Optional<java.util.Map<Identifier,Literal>>asRecord()Attempts to convert the literal to a map ofIdentifiertoLiteral.java.util.Optional<Template>asString()Attempts to convert the literal to aTemplateif possible.java.util.Optional<java.util.List<Literal>>asTuple()Attempts to convert the literal to a list ofLiteralvalues.static Literalbool(boolean value)Constructs a bool literal from a boolean value.booleanequals(java.lang.Object o)Valueevaluate(RuleEvaluator evaluator)java.lang.StringexpectLiteralString()Attempts to convert the literal to aString.static LiteralfromNode(Node node)Constructs a literal from aNodebased on the Node's type.SourceLocationgetSourceLocation()Gets the source location of a value.inthashCode()static Literalinteger(int value)Constructs an integer literal from an integer value.static Literalrecord(java.util.Map<Identifier,Literal> record)Constructs a record literal of values.static Literalstring(Template value)Constructs a string literal from aTemplatevalue.NodetoNode()Converts a value to aNode.java.lang.StringtoString()Returns the string representation of the literal.static Literaltuple(java.util.List<Literal> values)Constructs a tuple literal of values.TypetypeCheckLocal(Scope<Type> scope)
-
-
-
Method Detail
-
tuple
public static Literal tuple(java.util.List<Literal> values)
Constructs a tuple literal of values.- Parameters:
values- the values.- Returns:
- the tuple literal.
-
record
public static Literal record(java.util.Map<Identifier,Literal> record)
Constructs a record literal of values.- Parameters:
record- a map of values to be converted to a record.- Returns:
- the record literal.
-
string
public static Literal string(Template value)
Constructs a string literal from aTemplatevalue.- Parameters:
value- the template value.- Returns:
- the string literal.
-
integer
public static Literal integer(int value)
Constructs an integer literal from an integer value.- Parameters:
value- the integer value.- Returns:
- the integer literal.
-
bool
public static Literal bool(boolean value)
Constructs a bool literal from a boolean value.- Parameters:
value- the boolean value.- Returns:
- the bool literal.
-
fromNode
public static Literal fromNode(Node node)
Constructs a literal from aNodebased on the Node's type.- Parameters:
node- a node to construct as a literal.- Returns:
- the literal representation of the node.
-
expectLiteralString
public java.lang.String expectLiteralString()
Attempts to convert the literal to aString. Otherwise throws an exception.- Returns:
- the literal as a string.
-
asBool
public java.util.Optional<java.lang.Boolean> asBool()
Attempts to convert the literal to aBooleanif possible. Otherwise, returns an empty optional.- Returns:
- an optional boolean.
-
asString
public java.util.Optional<Template> asString()
Attempts to convert the literal to aTemplateif possible. Otherwise, returns an empty optional.- Returns:
- an optional boolean.
-
asRecord
public java.util.Optional<java.util.Map<Identifier,Literal>> asRecord()
Attempts to convert the literal to a map ofIdentifiertoLiteral. Otherwise, returns an empty optional.- Returns:
- an optional map.
-
asTuple
public java.util.Optional<java.util.List<Literal>> asTuple()
Attempts to convert the literal to a list ofLiteralvalues. Otherwise, returns an empty optional.- Returns:
- the optional list.
-
asInteger
public java.util.Optional<java.lang.Integer> asInteger()
Attempts to convert the literal to anLiteral.Integer. Otherwise, returns an empty optional.- Returns:
- the optional integer.
-
accept
public <T> T accept(Literal.Vistor<T> visitor)
Invokes the providedLiteral.Vistor.- Type Parameters:
T- the visitor return type.- Parameters:
visitor- the visitor.- Returns:
- the return value of the vistor.
-
toString
public java.lang.String toString()
Returns the string representation of the literal.- Overrides:
toStringin classjava.lang.Object- Returns:
- the string representation.
-
getSourceLocation
public SourceLocation getSourceLocation()
Description copied from interface:FromSourceLocationGets the source location of a value.- Specified by:
getSourceLocationin interfaceFromSourceLocation- Overrides:
getSourceLocationin classMandatorySourceLocation- Returns:
- Returns the source location of the value.
-
accept
public <R> R accept(ExpressionVisitor<R> visitor)
Description copied from class:ExpressionInvoke theExpressionVisitorfunctions for this expression.- Specified by:
acceptin classExpression- Type Parameters:
R- the visitor return type.- Parameters:
visitor- the visitor to be invoked.- Returns:
- the return value of the visitor.
-
hashCode
public int hashCode()
- Specified by:
hashCodein classExpression
-
equals
public boolean equals(java.lang.Object o)
- Specified by:
equalsin classExpression
-
typeCheckLocal
public Type typeCheckLocal(Scope<Type> scope)
- Specified by:
typeCheckLocalin classExpression
-
evaluate
public Value evaluate(RuleEvaluator evaluator)
- Parameters:
evaluator- the rule-set evaluator.- Returns:
- the resulting value.
-
-