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

#&, #*, #+, #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



975
976
977
978
979
980
981
982
983
984
985
# File 'lib/dhall/ast.rb', line 975

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



1022
1023
1024
1025
1026
1027
1028
# File 'lib/dhall/ast.rb', line 1022

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

#extractObject



991
992
993
994
995
996
997
# File 'lib/dhall/ast.rb', line 991

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

#normalizeObject



362
363
364
365
366
367
368
369
370
# File 'lib/dhall/normalize.rb', line 362

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



999
1000
1001
1002
1003
1004
# File 'lib/dhall/ast.rb', line 999

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



1006
1007
1008
1009
1010
1011
1012
1013
# File 'lib/dhall/ast.rb', line 1006

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

#syntaxObject



1015
1016
1017
1018
1019
1020
# File 'lib/dhall/ast.rb', line 1015

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

#to_sObject



987
988
989
# File 'lib/dhall/ast.rb', line 987

def to_s
	extract.to_s
end