-
Notifications
You must be signed in to change notification settings - Fork 44
/
ConfigValue.scala
136 lines (111 loc) · 4.35 KB
/
ConfigValue.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
/*
* 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.Origin.Scope
import laika.ast.{ Element, Path }
/** The base trait for all configuration values.
*
* This data structure is quite similar to those found in common
* JSON libraries (HOCON is a JSON superset after all).
*
* The only exception is one special type: the `ASTValue` which
* can hold an instance of a document AST obtained from parsing
* text markup.
*
* This can be useful in scenarios where substitution variables
* in templates or markup want to refer to other AST elements
* and include them into their AST tree as is.
*
* @author Jens Halm
*/
sealed trait ConfigValue extends Product with Serializable
object ConfigValue {
/** Base trait for all simple configuration values. */
sealed trait SimpleValue extends ConfigValue {
def render: String
}
case object NullValue extends SimpleValue {
val render: String = null
}
case class BooleanValue(value: Boolean) extends SimpleValue {
def render: String = value.toString
}
case class DoubleValue(value: Double) extends SimpleValue {
def render: String = value.toString
}
case class LongValue(value: Long) extends SimpleValue {
def render: String = value.toString
}
case class StringValue(value: String) extends SimpleValue {
def render: String = value
}
/** A value containing an AST element obtained from text markup or templates.
*
* Such a value can be used in scenarios where substitution variables
* in templates or markup want to refer to other AST elements
* and include them into their AST tree as is.
*/
case class ASTValue(value: Element) extends ConfigValue
case class ArrayValue(values: Seq[ConfigValue]) extends ConfigValue {
def isEmpty: Boolean = values.isEmpty
}
case class ObjectValue(values: Seq[Field]) extends ConfigValue {
def merge(other: ObjectValue): ObjectValue = {
def mergeValues(cbv1: Field, cbv2: Field): Field = (cbv1, cbv2) match {
case (Field(name, o1: ObjectValue, origin), Field(_, o2: ObjectValue, _)) =>
Field(name, o1.merge(o2), origin)
case (v1, _) => v1
}
val mergedFields = (values ++ other.values).groupBy(_.key).toSeq.map { case (_, fields) =>
fields.reduce(mergeValues)
}
ObjectValue(mergedFields)
}
def toConfig: Config = {
val origin =
if (values.isEmpty) Origin.root
else values.groupBy(_.origin).toSeq.maxBy(_._2.size)._1
new ObjectConfig(this, origin)
}
}
}
/** A value tagged with its origin. */
case class Traced[T](value: T, origin: Origin)
/** The origin of a configuration value.
*
* 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.
*
* @param scope the scope of the containing config instance
* @param path the virtual path of containing config instance in a document tree
* (not the key inside the configuration)
* @param sourcePath the path in the file system this configuration originates from,
* empty if it was constructed in memory
*/
case class Origin(scope: Scope, path: Path, sourcePath: Option[String] = None)
object Origin {
val root: Origin = Origin(GlobalScope, Path.Root)
sealed trait Scope
case object GlobalScope extends Scope
case object TreeScope extends Scope
case object DocumentScope extends Scope
case object TemplateScope extends Scope
case object DirectiveScope extends Scope
}
/** A single field of an object value. */
case class Field(key: String, value: ConfigValue, origin: Origin = Origin.root)