Skip to content

Commit e5b25fb

Browse files
authored
Merge pull request #40 from RobWin/context-prefix
Support multiple URIs wihtout a prefix in the Context.
2 parents fe91721 + a6ec6db commit e5b25fb

File tree

5 files changed

+16
-42
lines changed

5 files changed

+16
-42
lines changed

kotlin-wot-reflection/src/main/kotlin/reflection/ExposedThingBuilder.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ object ExposedThingBuilder {
7373
title = thingAnnotation.title
7474
objectType = Type(thingAnnotation.type)
7575
val contexts = clazz.findAnnotations<Context>()
76-
if(contexts.isNotEmpty()){
76+
if (contexts.isNotEmpty()) {
7777
val context = org.eclipse.thingweb.thing.schema.Context(DEFAULT_CONTEXT)
7878
contexts.forEach {
79-
context.addContext(it.prefix, it.url)
79+
if (it.prefix.isEmpty()) context.addContext(it.url) else context.addContext(it.prefix, it.url)
8080
}
8181
objectContext = context
8282
}

kotlin-wot-reflection/src/main/kotlin/reflection/annotations/Annotations.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ annotation class Thing(val id: String, val title: String, val description: Strin
1515
@Target(AnnotationTarget.CLASS)
1616
@Retention(AnnotationRetention.RUNTIME)
1717
@Repeatable
18-
annotation class Context(val prefix: String, val url : String)
18+
annotation class Context(val prefix: String = "", val url : String)
1919

2020
@Target(AnnotationTarget.CLASS)
2121
@Retention(AnnotationRetention.RUNTIME)

kotlin-wot/src/main/kotlin/thing/ContextSerializer.kt

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ internal class ContextSerializer : JsonSerializer<Context>() {
2323
gen: JsonGenerator,
2424
serializers: SerializerProvider
2525
) {
26-
val defaultUrl: String? = context.defaultUrl
26+
val defaultUrls: List<String> = context.defaultUrls
2727
val prefixedUrls: Map<String, String> = context.prefixedUrls
28-
val hasDefaultUrl = defaultUrl != null
28+
val hasDefaultUrls = defaultUrls.isNotEmpty()
2929
val hasPrefixedUrls = prefixedUrls.isNotEmpty()
30-
if (hasDefaultUrl && hasPrefixedUrls) {
30+
if (hasDefaultUrls && hasPrefixedUrls) {
3131
gen.writeStartArray()
3232
}
33-
if (hasDefaultUrl) {
34-
gen.writeString(defaultUrl)
33+
defaultUrls.forEach {
34+
gen.writeString(it)
3535
}
3636
if (hasPrefixedUrls) {
3737
gen.writeObject(prefixedUrls)
3838
}
39-
if (hasDefaultUrl && hasPrefixedUrls) {
39+
if (hasDefaultUrls && hasPrefixedUrls) {
4040
gen.writeEndArray()
4141
}
4242
}

kotlin-wot/src/main/kotlin/thing/ThingDescription.kt

-19
Original file line numberDiff line numberDiff line change
@@ -126,25 +126,6 @@ data class ThingDescription @JsonCreator constructor(
126126
securityDefinitions[name] = securityScheme
127127
}
128128

129-
fun getPropertiesByObjectType(objectType: String): Map<String, PropertyAffordance<*>> {
130-
return getPropertiesByExpandedObjectType(getExpandedObjectType(objectType))
131-
}
132-
133-
private fun getPropertiesByExpandedObjectType(objectType: String): Map<String, PropertyAffordance<*>> {
134-
return properties.filter { (_, property) ->
135-
property.objectType?.defaultType?.let { getExpandedObjectType(it) } == objectType
136-
}.toMap()
137-
}
138-
139-
fun getExpandedObjectType(objectType: String): String {
140-
141-
val parts = objectType.split(":", limit = 2)
142-
val prefix = if (parts.size == 2) parts[0] else null
143-
val suffix = parts.last()
144-
145-
return objectContext?.getUrl(prefix)?.let { "$it#$suffix" } ?: objectType
146-
}
147-
148129
fun toJson() : String{
149130
return try {
150131
JsonMapper.instance.writeValueAsString(this)

kotlin-wot/src/main/kotlin/thing/schema/Context.kt

+7-14
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,28 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize
1616
*/
1717
@JsonDeserialize(using = ContextDeserializer::class)
1818
@JsonSerialize(using = ContextSerializer::class)
19-
//@Serializable(with = ContextSerializer::class)
20-
data class Context(private val urls: MutableMap<String?, String> = HashMap()) {
19+
data class Context(val defaultUrls: MutableList<String> = mutableListOf(), private val prefixeUrls: MutableMap<String, String> = HashMap()) {
2120

2221
constructor(url: String) : this() {
2322
addContext(url)
2423
}
2524

26-
constructor(prefix: String?, url: String) : this() {
25+
constructor(prefix: String, url: String) : this() {
2726
addContext(prefix, url)
2827
}
2928

3029
fun addContext(url: String): Context {
31-
return addContext(null, url)
32-
}
33-
34-
fun getUrl(prefix: String?): String? {
35-
return urls[prefix]
30+
defaultUrls.add(url)
31+
return this
3632
}
3733

38-
fun addContext(prefix: String?, url: String): Context {
39-
urls[prefix] = url
34+
fun addContext(prefix: String, url: String): Context {
35+
prefixeUrls[prefix] = url
4036
return this
4137
}
4238

43-
val defaultUrl: String?
44-
get() = urls[null] // Directly accessing the map
45-
4639
val prefixedUrls: Map<String, String>
47-
get() = urls.entries
40+
get() = prefixeUrls.entries
4841
.filter { (key, _) -> key != null }
4942
.associate { (key, value) -> key!! to value }
5043
}

0 commit comments

Comments
 (0)