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_cbor, #to_proc, #|

Class Method Details

.decode(tag, value, alternatives) ⇒ Object



160
161
162
163
164
165
166
# File 'lib/dhall/binary.rb', line 160

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

.from(alts, tag, value) ⇒ Object



887
888
889
890
891
892
893
894
895
896
897
# File 'lib/dhall/ast.rb', line 887

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



934
935
936
937
938
939
940
# File 'lib/dhall/ast.rb', line 934

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

#extractObject



903
904
905
906
907
908
909
# File 'lib/dhall/ast.rb', line 903

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

#normalizeObject



336
337
338
339
340
341
342
343
344
# File 'lib/dhall/normalize.rb', line 336

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



911
912
913
914
915
916
# File 'lib/dhall/ast.rb', line 911

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



918
919
920
921
922
923
924
925
# File 'lib/dhall/ast.rb', line 918

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

#syntaxObject



927
928
929
930
931
932
# File 'lib/dhall/ast.rb', line 927

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

#to_sObject



899
900
901
# File 'lib/dhall/ast.rb', line 899

def to_s
	extract.to_s
end