blob: 7793db09d33799013babb45d2e9753c37ecc6e8e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# frozen_string_literal: true
module Gitlab
module Ci
class Config
class Extendable
class Entry
InvalidExtensionError = Class.new(Extendable::ExtensionError)
CircularDependencyError = Class.new(Extendable::ExtensionError)
NestingTooDeepError = Class.new(Extendable::ExtensionError)
MAX_NESTING_LEVELS = 10
attr_reader :key
def initialize(key, context, parent = nil)
@key = key
@context = context
@parent = parent
unless @context.key?(@key)
raise StandardError, 'Invalid entry key!'
end
end
def extensible?
value.is_a?(Hash) && value.key?(:extends)
end
def value
@value ||= @context.fetch(@key)
end
def base_hash!
@base ||= Extendable::Entry
.new(extends_key, @context, self)
.extend!
end
def extends_key
value.fetch(:extends).to_s.to_sym if extensible?
end
def ancestors
@ancestors ||= Array(@parent&.ancestors) + Array(@parent&.key)
end
def extend!
return value unless extensible?
if unknown_extension?
raise Entry::InvalidExtensionError,
"#{key}: unknown key in `extends`"
end
if invalid_base?
raise Entry::InvalidExtensionError,
"#{key}: invalid base hash in `extends`"
end
if nesting_too_deep?
raise Entry::NestingTooDeepError,
"#{key}: nesting too deep in `extends`"
end
if circular_dependency?
raise Entry::CircularDependencyError,
"#{key}: circular dependency detected in `extends`"
end
@context[key] = base_hash!.deep_merge(value)
end
private
def nesting_too_deep?
ancestors.count > MAX_NESTING_LEVELS
end
def circular_dependency?
ancestors.include?(key)
end
def unknown_extension?
!@context.key?(extends_key)
end
def invalid_base?
!@context[extends_key].is_a?(Hash)
end
end
end
end
end
end
|