/
MessageUtil.scala
170 lines (140 loc) · 6.69 KB
/
MessageUtil.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Copyright © 2015-2018 the contributors (see Contributors.md).
*
* This file is part of Knora.
*
* Knora is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Knora is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with Knora. If not, see <http://www.gnu.org/licenses/>.
*/
package org.knora.webapi.messages.util
import java.time.Instant
import org.apache.commons.text.StringEscapeUtils
import org.knora.webapi.OntologySchema
import org.knora.webapi.messages.SmartIri
import org.knora.webapi.messages.v2.responder.ontologymessages.Cardinality
import scala.reflect.runtime.{universe => ru}
/**
* Utility functions for working with Akka messages.
*/
object MessageUtil {
// Set of case class field names to skip.
private val fieldsToSkip =
Set("stringFormatter",
"base64Decoder",
"knoraIdUtil",
"standoffLinkTagTargetResourceIris",
"knoraSettings",
"featureFactoryConfig")
/**
* Recursively converts a Scala object to Scala source code for constructing the object (with named parameters). This is useful
* for writing tests containing hard-coded Akka messages. It works with case classes, collections ([[Seq]], [[Set]],
* and [[Map]]), [[Option]], enumerations (as long as the enumeration value's `toString` representation is the same
* as its identifier), and primitive types. It doesn't work with classes defined inside methods.
*
* @param obj the object to convert.
* @return a string that can be pasted into Scala source code to construct the object.
*/
def toSource(obj: Any): String = {
def maybeMakeNewLine(elemCount: Int): String = if (elemCount > 1) "\n" else ""
obj match {
// Handle primitive types.
case null => "null"
case short: Short => short.toString
case int: Int => int.toString
case long: Long => long.toString
case float: Float => float.toString
case double: Double => double.toString
case bigDecimal: BigDecimal => bigDecimal.toString
case boolean: Boolean => boolean.toString
case char: Char => char.toString
case byte: Byte => byte.toString
case s: String => "\"" + StringEscapeUtils.escapeJava(s) + "\""
case Some(value) => "Some(" + toSource(value) + ")"
case smartIri: SmartIri => "\"" + StringEscapeUtils.escapeJava(smartIri.toString) + "\".toSmartIri"
case instant: Instant => "Instant.parse(\"" + instant.toString + "\")"
case None => "None"
// Handle enumerations.
case cardinality: Cardinality.Value =>
cardinality match {
case Cardinality.MayHaveOne => "Cardinality.MayHaveOne"
case Cardinality.MayHaveMany => "Cardinality.MayHaveMany"
case Cardinality.MustHaveOne => "Cardinality.MustHaveOne"
case Cardinality.MustHaveSome => "Cardinality.MustHaveSome"
}
case enumVal if enumVal.getClass.getName == "scala.Enumeration$Val" => enumVal.toString
case ontologySchema: OntologySchema =>
ontologySchema.getClass.getSimpleName.stripSuffix("$")
// Handle collections.
case Nil => "Nil"
case list: Seq[Any @unchecked] =>
val maybeNewLine = maybeMakeNewLine(list.size)
list.map(elem => toSource(elem)).mkString("Vector(" + maybeNewLine, ", " + maybeNewLine, maybeNewLine + ")")
case set: Set[Any @unchecked] =>
val maybeNewLine = maybeMakeNewLine(set.size)
set.map(elem => toSource(elem)).mkString("Set(" + maybeNewLine, ", " + maybeNewLine, maybeNewLine + ")")
case map: Map[Any @unchecked, Any @unchecked] =>
val maybeNewLine = maybeMakeNewLine(map.size)
map
.map {
case (key, value) => toSource(key) + " -> " + toSource(value)
}
.mkString("Map(" + maybeNewLine, ", " + maybeNewLine, maybeNewLine + ")")
// Handle case classes.
case caseClass: Product =>
val objClass = obj.getClass
val objClassName = objClass.getSimpleName
val fieldMap: Map[String, Any] = caseClass.getClass.getDeclaredFields.foldLeft(Map[String, Any]()) {
(acc, field) =>
val fieldName = field.getName.trim
if (fieldName.contains("$") || fieldName.endsWith("Format") || fieldsToSkip.contains(fieldName)) { // ridiculous hack
acc
} else {
field.setAccessible(true)
acc + (field.getName -> field.get(caseClass))
}
}
val members: Iterable[String] = fieldMap.map {
case (fieldName, fieldValue) =>
val fieldValueString = toSource(fieldValue)
s"$fieldName = $fieldValueString"
}
val maybeNewLine = maybeMakeNewLine(members.size)
members.mkString(objClassName + "(" + maybeNewLine, ", " + maybeNewLine, maybeNewLine + ")")
// Handle other classes.
case _ =>
// Generate a named parameter initializer for each of the class's non-method fields.
val objClass = obj.getClass
val objClassName = objClass.getSimpleName
val runtimeMirror: ru.Mirror = ru.runtimeMirror(objClass.getClassLoader)
val instanceMirror = runtimeMirror.reflect(obj)
val objType: ru.Type = runtimeMirror.classSymbol(objClass).toType
val members: Iterable[String] = objType.members.filter(member => !member.isMethod).flatMap { member =>
val memberName = member.name.toString.trim
if (!(memberName.contains("$") || memberName.endsWith("Format") || fieldsToSkip.contains(memberName))) {
val fieldMirror = try {
instanceMirror.reflectField(member.asTerm)
} catch {
case e: Exception => throw new Exception(s"Can't format member $memberName in class $objClassName", e)
}
val memberValue = fieldMirror.get
val memberValueString = toSource(memberValue)
Some(s"$memberName = $memberValueString")
} else {
None
}
}
val maybeNewLine = maybeMakeNewLine(members.size)
members.mkString(objClassName + "(" + maybeNewLine, ", " + maybeNewLine, maybeNewLine + ")")
}
}
}