Class: Dhall::Union

Inherits:
Expression show all
Defined in:
lib/dhall/ast.rb,
lib/dhall/binary.rb,
lib/dhall/normalize.rb

Direct Known Subclasses

Enum

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Expression

#&, #*, #+, #annotate, #as_dhall, #cache_key, #call, #concat, #deep_merge, #deep_merge_type, #dhall_eq, #digest, #fetch, #fusion, #merge, #resolve, #shift, #slice, #substitute, #to_binary, #to_cbor, #to_proc, #|

Class Method Details

.decode(tag, value, alternatives) ⇒ Object



184
185
186
187
188
189
190
# File 'lib/dhall/binary.rb', line 184

def self.decode(tag, value, alternatives)
	new(
		tag:          tag,
		value:        Dhall.decode(value),
		alternatives: UnionType.decode(alternatives)
	)
end

.from(alts, tag, value) ⇒ Object



1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
# File 'lib/dhall/ast.rb', line 1024

def self.from(alts, tag, value)
	if value.nil?
		Enum.new(tag: tag, alternatives: alts.without(tag))
	else
		new(
			tag:          tag,
			value:        TypeAnnotation.new(value: value, type: alts[tag]),
			alternatives: alts.without(tag)
		)
	end
end

Instance Method Details

#as_jsonObject



1071
1072
1073
1074
1075
1076
1077
# File 'lib/dhall/ast.rb', line 1071

def as_json
	if value.respond_to?(:type)
		syntax.as_json
	else
		[12, tag, value&.as_json, alternatives.as_json.last]
	end
end

#extractObject



1040
1041
1042
1043
1044
1045
1046
# File 'lib/dhall/ast.rb', line 1040

def extract
	if value.is_a?(TypeAnnotation)
		value.value
	else
		value
	end
end

#normalizeObject



378
379
380
381
382
383
384
385
386
# File 'lib/dhall/normalize.rb', line 378

def normalize
	val = if value.is_a?(TypeAnnotation)
		value.with(ExpressionVisitor.new(&:normalize).visit(value))
	else
		value&.normalize
	end

	with(value: val, alternatives: alternatives.normalize)
end

#reduce(handlers) ⇒ Object



1048
1049
1050
1051
1052
1053
# File 'lib/dhall/ast.rb', line 1048

def reduce(handlers)
	handlers = handlers.to_h
	handler = handlers.fetch(tag.to_sym) { handlers.fetch(tag) }
	(handler.respond_to?(:to_proc) ? handler.to_proc : handler)
		.call(extract)
end

#selection_syntaxObject



1055
1056
1057
1058
1059
1060
1061
1062
# File 'lib/dhall/ast.rb', line 1055

def selection_syntax
	RecordSelection.new(
		record:   alternatives.merge(
			UnionType.new(alternatives: { tag => value&.type })
		),
		selector: tag
	)
end

#syntaxObject



1064
1065
1066
1067
1068
1069
# File 'lib/dhall/ast.rb', line 1064

def syntax
	Application.new(
		function: selection_syntax,
		argument: value.is_a?(TypeAnnotation) ? value.value : value
	)
end

#to_sObject



1036
1037
1038
# File 'lib/dhall/ast.rb', line 1036

def to_s
	extract.to_s
end