-
Notifications
You must be signed in to change notification settings - Fork 44
/
ConfigParser.scala
148 lines (125 loc) · 5.09 KB
/
ConfigParser.scala
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package laika.api.config
import laika.api.config.Config.IncludeMap
import ConfigError.{ ParsingFailed, ResourceLoadingFailed }
import laika.internal.parse.hocon.*
import laika.parse.{ Failure, Success }
/** A parser for obtaining a Config instance from a HOCON string.
*
* The HOCON format expected by this parsers is specified at
* [[https://github.com/lightbend/config/blob/master/HOCON.md]]
*
* @author Jens Halm
*/
trait ConfigParser {
/** Extracts all unresolved requested includes the parsed configuration contains,
* including those nested inside other objects.
*
* Since the pure core module does not support effectful IO, this hook
* can be used by a higher level API to resolve includes and pass them
* to the `resolve` method.
*/
private[laika] def includes: Seq[IncludeResource]
/** The unresolved result of the parser which can be used
* for processing inclusions that will be resolved in the
* context of the hosting configuration.
*/
def unresolved: Either[ConfigError, ObjectBuilderValue]
/** Parses and resolves the parsed HOCON input.
* This includes the resolving of substitution references
* and the merging of concatenated objects, arrays and strings.
*
* Failures may be caused by both, the parser and resolver step.
*
* The specified origin will be attached to every field.
* Origins can be used to distinguish values from a specific Config
* instance from those which were inherited from a fallback, which
* might be relevant in scenarios where relative paths need to be
* resolved for example.
*
* The specified fallback will be used for resolving keys which are
* not present in the configuration created by this parser.
*
* If an entire object is requested in the resulting Config instance,
* the keys will be merged from this parser with those present in the fallback.
* Simple values on the other hand will always override values with the same
* key in the fallback.
*/
def resolve(
origin: Origin = Origin.root,
fallback: Config = EmptyConfig
): Either[ConfigError, Config] = resolve(origin, fallback, ConfigParser.includesNotSupported)
/** The specified includes contain all resources that could be loaded
* based on the requested resources exposed by the `includes` property.
* Keeping the actual loading separate allows the resource loading step
* to be separate and in a module designed for effectful operations.
*/
private[laika] def resolve(
origin: Origin,
fallback: Config,
includes: IncludeMap
): Either[ConfigError, Config]
}
object ConfigParser {
private[laika] val includesNotSupported: IncludeMap =
Map.empty.withDefaultValue(
Left(
ResourceLoadingFailed(
"Loading of includes is not supported in pure mode, use parsers or transformers in laika-io for this purpose."
)
)
)
/** Creates a new parser for the specified HOCON input.
*/
def parse(input: String): ConfigParser = new ConfigParser {
lazy val unresolved: Either[ConfigError, ObjectBuilderValue] =
HoconParsers.rootObject.parse(input) match {
case Success(builderRoot, _) => Right(builderRoot)
case f: Failure => Left(ParsingFailed(f))
}
private[laika] lazy val includes: Seq[IncludeResource] = {
def extractIncludes(obj: ObjectBuilderValue): Seq[IncludeResource] = obj.values.flatMap {
case BuilderField(_, IncludeBuilderValue(resource)) => Seq(resource)
case BuilderField(_, child: ObjectBuilderValue) => extractIncludes(child)
case _ => Nil
}
unresolved.fold(_ => Nil, extractIncludes)
}
private[laika] def resolve(
origin: Origin,
fallback: Config,
includes: IncludeMap
): Either[ConfigError, Config] =
unresolved.flatMap(
ConfigResolver
.resolve(_, origin, fallback, includes)
.map(new ObjectConfig(_, origin, fallback))
)
}
/** Creates a new parser that will produce an empty result.
*/
def empty: ConfigParser = new ConfigParser {
val includes: Seq[IncludeResource] = Nil
val unresolved: Either[ConfigError, ObjectBuilderValue] = Right(ObjectBuilderValue(Nil))
private[laika] def resolve(
origin: Origin,
fallback: Config,
includes: IncludeMap
): Either[ConfigError, Config] =
Right(ConfigBuilder.withFallback(fallback, origin).build)
}
}