2016-06-16 23:09:34 +05:30
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
class Config
|
2017-08-17 22:00:37 +05:30
|
|
|
module Entry
|
2016-06-16 23:09:34 +05:30
|
|
|
##
|
|
|
|
# Base abstract class for each configuration entry node.
|
|
|
|
#
|
2017-08-17 22:00:37 +05:30
|
|
|
class Node
|
|
|
|
InvalidError = Class.new(StandardError)
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
attr_reader :config, :metadata
|
2016-08-24 12:49:21 +05:30
|
|
|
attr_accessor :key, :parent, :description
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def initialize(config, **metadata)
|
2016-08-24 12:49:21 +05:30
|
|
|
@config = config
|
2016-09-13 17:45:13 +05:30
|
|
|
@metadata = metadata
|
|
|
|
@entries = {}
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
@validator = self.class.validator.new(self)
|
2016-09-13 17:45:13 +05:30
|
|
|
@validator.validate(:new)
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
def [](key)
|
2017-08-17 22:00:37 +05:30
|
|
|
@entries[key] || Entry::Undefined.new
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def compose!(deps = nil)
|
2016-06-16 23:09:34 +05:30
|
|
|
return unless valid?
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
yield if block_given?
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def leaf?
|
|
|
|
@entries.none?
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def descendants
|
|
|
|
@entries.values
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def ancestors
|
|
|
|
@parent ? @parent.ancestors + [@parent] : []
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid?
|
|
|
|
errors.none?
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def errors
|
2016-09-13 17:45:13 +05:30
|
|
|
@validator.messages + descendants.flat_map(&:errors)
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def value
|
|
|
|
if leaf?
|
|
|
|
@config
|
|
|
|
else
|
2016-09-13 17:45:13 +05:30
|
|
|
meaningful = @entries.select do |_key, value|
|
|
|
|
value.specified? && value.relevant?
|
|
|
|
end
|
|
|
|
|
|
|
|
Hash[meaningful.map { |key, entry| [key, entry.value] }]
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def specified?
|
2016-08-24 12:49:21 +05:30
|
|
|
true
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def relevant?
|
|
|
|
true
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def inspect
|
|
|
|
val = leaf? ? config : descendants
|
|
|
|
unspecified = specified? ? '' : '(unspecified) '
|
|
|
|
"#<#{self.class.name} #{unspecified}{#{key}: #{val.inspect}}>"
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def self.default
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def self.validator
|
|
|
|
Validator
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|