-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from outfoxx/feature/path-encoders
Add support for path parameter encoders
- Loading branch information
Showing
8 changed files
with
210 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2020 Outfox, Inc. | ||
* | ||
* 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 io.outfoxx.sunday | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
import kotlin.reflect.KClass | ||
|
||
typealias PathEncoder = (Any) -> String | ||
typealias PathEncoderMap = Map<KClass<*>, PathEncoder> | ||
|
||
object PathEncoders { | ||
|
||
val default: PathEncoderMap | ||
get() = mapOf( | ||
Enum::class to { encodeEnum(it as Enum<*>) } | ||
) | ||
|
||
fun <E : Enum<E>> encodeEnum(value: Enum<E>) = | ||
value.javaClass | ||
.getField(value.name) | ||
.getAnnotation(JsonProperty::class.java) | ||
?.value | ||
?: value.name | ||
|
||
} | ||
|
||
inline fun <reified T : Any> PathEncoderMap.add( | ||
type: KClass<T>, | ||
crossinline encoder: (T) -> String | ||
): PathEncoderMap { | ||
return this + mapOf(type to { encoder.invoke(it as T) }) | ||
} | ||
|
||
inline fun <reified T : Any> PathEncoderMap.add( | ||
crossinline encoder: (T) -> String | ||
): PathEncoderMap { | ||
return add(T::class, encoder) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2020 Outfox, Inc. | ||
* | ||
* 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. | ||
*/ | ||
|
||
import io.outfoxx.sunday.PathEncoders | ||
import io.outfoxx.sunday.add | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.hamcrest.Matchers | ||
import org.junit.jupiter.api.Test | ||
import java.util.UUID | ||
|
||
class PathEncodersTest { | ||
|
||
@Test | ||
fun `adding implicitly typed encoders`() { | ||
|
||
val encoders = PathEncoders.default.add(UUID::toString) | ||
assertThat(encoders, Matchers.aMapWithSize(2)) | ||
} | ||
|
||
@Test | ||
fun `adding explicitly typed encoders`() { | ||
|
||
val encoders = PathEncoders.default.add(UUID::class, UUID::toString) | ||
assertThat(encoders, Matchers.aMapWithSize(2)) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2020 Outfox, Inc. | ||
* | ||
* 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. | ||
*/ | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
import io.outfoxx.sunday.PathEncoder | ||
import io.outfoxx.sunday.PathEncoders | ||
import io.outfoxx.sunday.URITemplate | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.hamcrest.Matchers.equalTo | ||
import org.junit.jupiter.api.Test | ||
import java.util.UUID | ||
import kotlin.reflect.KClass | ||
|
||
class URITemplateTest { | ||
|
||
enum class TestEnum { | ||
@JsonProperty("test-value") | ||
TestValue | ||
} | ||
|
||
@Test | ||
fun `test enum encoding`() { | ||
|
||
val path = | ||
URITemplate("http://example.com/{enum}", mapOf("enum" to TestEnum.TestValue)) | ||
.resolve(encoders = PathEncoders.default) | ||
.toURI() | ||
.toString() | ||
|
||
assertThat(path, equalTo("http://example.com/test-value")) | ||
} | ||
|
||
@Test | ||
fun `test custom encoding`() { | ||
|
||
val encoders: Map<KClass<*>, PathEncoder> = | ||
mapOf( | ||
UUID::class to { (it as UUID).toString().replace("-", "") } | ||
) | ||
|
||
val id = UUID.randomUUID() | ||
val path = | ||
URITemplate("http://example.com/objects/{id}", mapOf("id" to id, "none" to null)) | ||
.resolve(encoders = encoders) | ||
.toURI() | ||
.toString() | ||
|
||
assertThat( | ||
path, | ||
equalTo("http://example.com/objects/${id.toString().replace("-", "")}") | ||
) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters