-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to use multiple dialects (#3)
* feat: add dialect functionality and basic spring implementation for it * feat: add request parameters * fix: correct imports, allow spring controllers to use annotations * fix: move generation of converters to generated services * fix: allow services to be loaded properly * fix: correct code smells * feat: add javadocs * feat: add docs for dialects * feat: add docs for dialects * fix: correct javadoc
- Loading branch information
Showing
125 changed files
with
1,305 additions
and
456 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,11 @@ | ||
# Custom dialects | ||
|
||
Defining a new dialect is possible by depending on rest-ahead-processor. Required steps are as following: | ||
|
||
1. Create a class implementing `io.github.zskamljic.restahead.polyglot.Dialect`. | ||
2. Create a file in `main/resources/META-INF/services` named `io.github.zskamljic.restahead.polyglot.Dialect` (note that | ||
this is a file name, not a Java package) | ||
3. In created file add the line with FQCN of the class created in #1. | ||
4. In project where you want the dialect to be used add the dependency with at least `provided` scope. | ||
|
||
For sample implementation see Spring Dialect. |
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
31 changes: 31 additions & 0 deletions
31
...ahead-client/src/main/java/io/github/zskamljic/restahead/conversion/MapFormConverter.java
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,31 @@ | ||
package io.github.zskamljic.restahead.conversion; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
import java.net.URLEncoder; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* The default converter for Map classes. | ||
*/ | ||
public final class MapFormConverter { | ||
private MapFormConverter() { | ||
} | ||
|
||
/** | ||
* Encodes the given value in format [key]=[URL encoded value] separated by & | ||
* @param value the values to encode | ||
* @param <K> type of the key | ||
* @param <V> type of the value | ||
* @return the {@link InputStream} with encoded data | ||
*/ | ||
public static <K, V> InputStream formEncode(Map<K, V> value) { | ||
var stringValue = value.entrySet() | ||
.stream() | ||
.map(entry -> entry.getKey() + "=" + URLEncoder.encode(String.valueOf(entry.getValue()), StandardCharsets.UTF_8)) | ||
.collect(Collectors.joining("&")); | ||
return new ByteArrayInputStream(stringValue.getBytes(StandardCharsets.UTF_8)); | ||
} | ||
} |
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
4 changes: 2 additions & 2 deletions
4
...head-processor/src/main/java/io/github/zskamljic/restahead/encoding/FormBodyEncoding.java
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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
package io.github.zskamljic.restahead.encoding; | ||
|
||
import io.github.zskamljic.restahead.encoding.generation.GenerationStrategy; | ||
import io.github.zskamljic.restahead.encoding.generation.FormConversionStrategy; | ||
|
||
/** | ||
* Specifies that the type should use form encoding. | ||
* | ||
* @param parameterName the name of parameter to encode | ||
* @param strategy the strategy to use when generating the conversion code | ||
*/ | ||
public record FormBodyEncoding(String parameterName, GenerationStrategy strategy) implements BodyEncoding { | ||
public record FormBodyEncoding(String parameterName, FormConversionStrategy strategy) implements BodyEncoding { | ||
} |
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
48 changes: 48 additions & 0 deletions
48
...rc/main/java/io/github/zskamljic/restahead/encoding/generation/MapConversionStrategy.java
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,48 @@ | ||
package io.github.zskamljic.restahead.encoding.generation; | ||
|
||
import io.github.zskamljic.restahead.modeling.TypeValidator; | ||
|
||
import javax.annotation.processing.Messager; | ||
import javax.lang.model.type.DeclaredType; | ||
import javax.lang.model.type.TypeMirror; | ||
import javax.lang.model.util.Elements; | ||
import javax.lang.model.util.Types; | ||
import javax.tools.Diagnostic; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Generates a form converter for maps. | ||
*/ | ||
public record MapConversionStrategy(TypeMirror type) implements FormConversionStrategy { | ||
|
||
/** | ||
* Checks if provided type is a Map or one of the subclasses that has string representable keys and values. | ||
* | ||
* @param elements the elements to fetch type information from | ||
* @param types the types utility to use for typing info | ||
* @param mirror the type for which to find a strategy | ||
* @return a strategy if data is valid, empty otherwise | ||
*/ | ||
public static Optional<FormConversionStrategy> getIfSupported(Messager messager, Elements elements, Types types, TypeMirror mirror) { | ||
var type = elements.getTypeElement(Map.class.getCanonicalName()) | ||
.asType(); | ||
if (!types.isAssignable(types.erasure(mirror), type)) { | ||
return Optional.empty(); | ||
} | ||
var mapType = (DeclaredType) mirror; | ||
var genericArguments = mapType.getTypeArguments(); | ||
if (genericArguments.size() != 2) return Optional.empty(); | ||
|
||
var stringValidator = new TypeValidator(elements, types); | ||
var key = genericArguments.get(0); | ||
var value = genericArguments.get(1); | ||
|
||
if (stringValidator.isUnsupportedType(key) || stringValidator.isUnsupportedType(value)) { | ||
messager.printMessage(Diagnostic.Kind.ERROR, "Maps must consist of string representable values to be formEncoded", mapType.asElement()); | ||
return Optional.empty(); | ||
} | ||
|
||
return Optional.of(new MapConversionStrategy(types.erasure(type))); | ||
} | ||
} |
Oops, something went wrong.