Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce primitive type Bytes in ws language #310

Merged
merged 3 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import community.flock.wirespec.generated.java.GetUserByNameEndpoint;
import community.flock.wirespec.generated.java.GetUsersEndpoint;
import community.flock.wirespec.generated.java.PostUserEndpoint;
import community.flock.wirespec.generated.java.UploadImageEndpoint;
import org.springframework.stereotype.Component;

import java.util.List;
Expand Down Expand Up @@ -59,6 +60,15 @@ public User deleteUserByName(final String name) {
};
}

@Override
public void uploadImage(String name, byte[] bytes) {
var res = complete(client.uploadImage(new UploadImageEndpoint.Request(name, bytes)));
switch (res) {
case UploadImageEndpoint.Response201 ignored -> {}
case UploadImageEndpoint.Response404 ignored -> throw new NotFound.User();
};
}

private <T> T complete(final CompletableFuture<T> future) {
try {
return future.get(10L, SECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import community.flock.wirespec.generated.java.GetUserByNameEndpoint;
import community.flock.wirespec.generated.java.GetUsersEndpoint;
import community.flock.wirespec.generated.java.PostUserEndpoint;
import community.flock.wirespec.generated.java.UploadImageEndpoint;
import community.flock.wirespec.java.Wirespec;
import org.springframework.stereotype.Component;

Expand All @@ -19,13 +20,15 @@ public class LiveUserClient implements UserClient {
private final Wirespec.ClientEdge<GetUserByNameEndpoint.Request, GetUserByNameEndpoint.Response<?>> getUserByName;
private final Wirespec.ClientEdge<PostUserEndpoint.Request, PostUserEndpoint.Response<?>> postUser;
private final Wirespec.ClientEdge<DeleteUserByNameEndpoint.Request, DeleteUserByNameEndpoint.Response<?>> deleteUserByName;
private final Wirespec.ClientEdge<UploadImageEndpoint.Request, UploadImageEndpoint.Response<?>> uploadImage;

public LiveUserClient(final WirespecTransporter transporter,final WirespecSerializer serializer) {
this.transporter = transporter;
this.getUsers = new GetUsersEndpoint.Handler.Handlers().getClient(serializer);
this.getUserByName = new GetUserByNameEndpoint.Handler.Handlers().getClient(serializer);
this.postUser = new PostUserEndpoint.Handler.Handlers().getClient(serializer);
this.deleteUserByName = new DeleteUserByNameEndpoint.Handler.Handlers().getClient(serializer);
this.uploadImage = new UploadImageEndpoint.Handler.Handlers().getClient(serializer);
}

@Override
Expand All @@ -51,4 +54,10 @@ public CompletableFuture<DeleteUserByNameEndpoint.Response<?>> deleteUserByName(
return transporter.transport(deleteUserByName.to(request))
.thenApplyAsync(deleteUserByName::from);
}

@Override
public CompletableFuture<UploadImageEndpoint.Response<?>> uploadImage(UploadImageEndpoint.Request request) {
return transporter.transport(uploadImage.to(request))
.thenApplyAsync(uploadImage::from);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ interface Module {
User saveUser(final User user);

User deleteUserByName(final String name);

void uploadImage(final String name, byte[] bytes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import community.flock.wirespec.generated.java.GetUserByNameEndpoint;
import community.flock.wirespec.generated.java.GetUsersEndpoint;
import community.flock.wirespec.generated.java.PostUserEndpoint;
import community.flock.wirespec.generated.java.UploadImageEndpoint;

public interface UserClient extends
GetUsersEndpoint.Handler,
GetUserByNameEndpoint.Handler,
PostUserEndpoint.Handler,
DeleteUserByNameEndpoint.Handler {
DeleteUserByNameEndpoint.Handler,
UploadImageEndpoint.Handler {
String version = "1.0.0";
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,10 @@ public static User saveUser(final UserContext ctx, final User user) {
public static User deleteUserByName(final UserContext ctx, final String name) {
return ctx.userAdapter().deleteUserByName(name);
}

public static void uploadImageByName(final UserContext ctx, final String name, final byte[] bytes) {
ctx.userAdapter().uploadImage(name, bytes);

}
}
}
5 changes: 5 additions & 0 deletions examples/maven-spring-compile/src/main/wirespec/users.ws
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ endpoint DeleteUserByName DELETE /api/users/{name: String} -> {
200 -> UserDto
404 -> Unit
}

endpoint UploadImage POST Bytes /api/users/{name: String}/image -> {
201 -> Unit
404 -> Unit
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
package community.flock.wirespec.example.maven.custom.app.user;

import community.flock.wirespec.generated.java.DeleteUserByNameEndpoint;
import community.flock.wirespec.generated.java.GetUserByNameEndpoint;
import community.flock.wirespec.generated.java.GetUsersEndpoint;
import community.flock.wirespec.generated.java.PostUserEndpoint;
import community.flock.wirespec.generated.java.UserDto;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import community.flock.wirespec.generated.java.*;

import java.util.*;
import java.util.concurrent.CompletableFuture;

import static java.util.concurrent.CompletableFuture.completedFuture;

public class TestUserClient implements UserClient {

private final Set<UserDto> users = new HashSet<>(Set.of(
public static final Set<UserDto> users = new HashSet<>(Set.of(
new UserDto("name"),
new UserDto("other name")
));

public static final Map<String, byte[]> images = new HashMap<>();

@Override
public CompletableFuture<GetUsersEndpoint.Response<?>> getUsers(GetUsersEndpoint.Request request) {
var filtered = users.stream().filter(it -> Objects.equals(it.name(), request.getQueries().name())).toList();
Expand Down Expand Up @@ -60,4 +56,10 @@ public CompletableFuture<DeleteUserByNameEndpoint.Response<?>> deleteUserByName(

return completedFuture(res);
}

@Override
public CompletableFuture<UploadImageEndpoint.Response<?>> uploadImage(UploadImageEndpoint.Request request) {
images.put(request.getPath().name(), request.getBody());
return completedFuture(new UploadImageEndpoint.Response201());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import org.junit.jupiter.api.Test;

import java.util.Random;
import java.util.function.Consumer;

import static community.flock.wirespec.example.maven.custom.app.user.UserContext.Service.deleteUserByName;
import static community.flock.wirespec.example.maven.custom.app.user.UserContext.Service.getAllUsers;
import static community.flock.wirespec.example.maven.custom.app.user.UserContext.Service.getUserByName;
import static community.flock.wirespec.example.maven.custom.app.user.UserContext.Service.saveUser;
import static community.flock.wirespec.example.maven.custom.app.user.UserContext.Service.*;
wilmveel marked this conversation as resolved.
Show resolved Hide resolved
import static org.junit.jupiter.api.Assertions.assertEquals;

class UserTest {
Expand Down Expand Up @@ -47,6 +45,15 @@ void testDeleteUser() {
});
}

@Test
void testUploadImage() {
testContext(it -> {
byte[] bytes = "Hello World".getBytes();
uploadImageByName(it, "newName", bytes);
assertEquals(bytes, TestUserClient.images.get("newName"));
});
}

private void testContext(Consumer<Context> test) {
// Adapter cannot be inlined. We need only 1 instance per test not multiple.
var adapter = new LiveUserAdapter(new TestUserClient(), new UserConverter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import community.flock.wirespec.compiler.core.tokenize.types.StatusCode
import community.flock.wirespec.compiler.core.tokenize.types.TokenType
import community.flock.wirespec.compiler.core.tokenize.types.WhiteSpaceExceptNewLine
import community.flock.wirespec.compiler.core.tokenize.types.WsBoolean
import community.flock.wirespec.compiler.core.tokenize.types.WsBytes
import community.flock.wirespec.compiler.core.tokenize.types.WsChannelDef
import community.flock.wirespec.compiler.core.tokenize.types.WsComment
import community.flock.wirespec.compiler.core.tokenize.types.WsEndpointDef
Expand Down Expand Up @@ -57,6 +58,7 @@ object WirespecSpec : LanguageSpec {
Regex("^#") to Hash,
Regex("^\\[\\]") to Brackets,
Regex("^String") to WsString,
Regex("^Bytes") to WsBytes,
Regex("^Integer32") to WsInteger,
Regex("^Integer") to WsInteger,
Regex("^Number32") to WsNumber,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ open class JavaEmitter(
Reference.Primitive.Type.Precision.P64 -> "Double"
}
is Reference.Primitive.Type.Boolean -> "Boolean"
is Reference.Primitive.Type.Bytes -> "byte[]"
}

override fun emit(identifier: Identifier) = when (identifier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ open class KotlinEmitter(
Reference.Primitive.Type.Precision.P64 -> "Double"
}
is Reference.Primitive.Type.Boolean -> "Boolean"
is Reference.Primitive.Type.Bytes -> "ByteArray"
}
}
.let { if (isIterable) "List<$it>" else it }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ open class ScalaEmitter(
Reference.Primitive.Type.Precision.P64 -> "Double"
}
is Reference.Primitive.Type.Boolean -> "Boolean"
is Reference.Primitive.Type.Bytes -> "Array[Byte]"
}
}
.let { if (isIterable) "List[$it]" else it }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ open class TypeScriptEmitter(logger: Logger = noLogger) : DefinitionModelEmitter
is Reference.Primitive.Type.Integer -> "number"
is Reference.Primitive.Type.Number -> "number"
is Reference.Primitive.Type.Boolean -> "boolean"
is Reference.Primitive.Type.Bytes -> "ArrayBuffer"
}
}
.let { if (isIterable) "$it[]" else it }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ open class WirespecEmitter(logger: Logger = noLogger) : DefinitionModelEmitter,
else ->"Number"
}
is Reference.Primitive.Type.Boolean -> "Boolean"
is Reference.Primitive.Type.Bytes -> "Bytes"
}
}
.let { if (isIterable) "$it[]" else it }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,7 @@ import arrow.core.Either
import arrow.core.raise.either
import community.flock.wirespec.compiler.core.exceptions.WirespecException
import community.flock.wirespec.compiler.core.exceptions.WirespecException.CompilerException.ParserException.WrongTokenException
import community.flock.wirespec.compiler.core.tokenize.types.Arrow
import community.flock.wirespec.compiler.core.tokenize.types.Brackets
import community.flock.wirespec.compiler.core.tokenize.types.Colon
import community.flock.wirespec.compiler.core.tokenize.types.CustomType
import community.flock.wirespec.compiler.core.tokenize.types.CustomValue
import community.flock.wirespec.compiler.core.tokenize.types.ForwardSlash
import community.flock.wirespec.compiler.core.tokenize.types.Hash
import community.flock.wirespec.compiler.core.tokenize.types.LeftCurly
import community.flock.wirespec.compiler.core.tokenize.types.Method
import community.flock.wirespec.compiler.core.tokenize.types.Path
import community.flock.wirespec.compiler.core.tokenize.types.QuestionMark
import community.flock.wirespec.compiler.core.tokenize.types.RightCurly
import community.flock.wirespec.compiler.core.tokenize.types.StatusCode
import community.flock.wirespec.compiler.core.tokenize.types.WirespecType
import community.flock.wirespec.compiler.core.tokenize.types.WsBoolean
import community.flock.wirespec.compiler.core.tokenize.types.WsInteger
import community.flock.wirespec.compiler.core.tokenize.types.WsNumber
import community.flock.wirespec.compiler.core.tokenize.types.WsString
import community.flock.wirespec.compiler.core.tokenize.types.WsUnit
import community.flock.wirespec.compiler.core.tokenize.types.*
wilmveel marked this conversation as resolved.
Show resolved Hide resolved
import community.flock.wirespec.compiler.utils.Logger

class EndpointParser(logger: Logger) : AbstractParser(logger) {
Expand Down Expand Up @@ -246,6 +228,12 @@ class EndpointParser(logger: Logger) : AbstractParser(logger) {
isDictionary = isDict,
)

is WsBytes -> Reference.Primitive(
type = Reference.Primitive.Type.Bytes,
isIterable = isIterable,
isDictionary = isDict,
)

is WsInteger -> Reference.Primitive(
type = Reference.Primitive.Type.Integer(when(value) {
"Integer32" -> Reference.Primitive.Type.Precision.P32
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ sealed interface Reference : Value<String> {
override val name = "Boolean"
}

data object Bytes : Type {
override val name = "Bytes"
}

}

override val value = type.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,7 @@ import arrow.core.Either
import arrow.core.raise.either
import community.flock.wirespec.compiler.core.exceptions.WirespecException
import community.flock.wirespec.compiler.core.exceptions.WirespecException.CompilerException.ParserException.WrongTokenException
import community.flock.wirespec.compiler.core.tokenize.types.Brackets
import community.flock.wirespec.compiler.core.tokenize.types.Colon
import community.flock.wirespec.compiler.core.tokenize.types.Comma
import community.flock.wirespec.compiler.core.tokenize.types.CustomType
import community.flock.wirespec.compiler.core.tokenize.types.CustomValue
import community.flock.wirespec.compiler.core.tokenize.types.EndOfProgram
import community.flock.wirespec.compiler.core.tokenize.types.Equals
import community.flock.wirespec.compiler.core.tokenize.types.ForwardSlash
import community.flock.wirespec.compiler.core.tokenize.types.LeftCurly
import community.flock.wirespec.compiler.core.tokenize.types.Pipe
import community.flock.wirespec.compiler.core.tokenize.types.QuestionMark
import community.flock.wirespec.compiler.core.tokenize.types.RightCurly
import community.flock.wirespec.compiler.core.tokenize.types.TypeDefinitionStart
import community.flock.wirespec.compiler.core.tokenize.types.WirespecDefinition
import community.flock.wirespec.compiler.core.tokenize.types.WirespecType
import community.flock.wirespec.compiler.core.tokenize.types.WsBoolean
import community.flock.wirespec.compiler.core.tokenize.types.WsInteger
import community.flock.wirespec.compiler.core.tokenize.types.WsNumber
import community.flock.wirespec.compiler.core.tokenize.types.WsString
import community.flock.wirespec.compiler.core.tokenize.types.WsUnit
import community.flock.wirespec.compiler.core.tokenize.types.*
wilmveel marked this conversation as resolved.
Show resolved Hide resolved
import community.flock.wirespec.compiler.utils.Logger

class TypeParser(logger: Logger) : AbstractParser(logger) {
Expand Down Expand Up @@ -83,6 +64,12 @@ class TypeParser(logger: Logger) : AbstractParser(logger) {
isDictionary = isDict
)

is WsBytes -> Reference.Primitive(
type = Reference.Primitive.Type.Bytes,
isIterable = isIterable,
isDictionary = isDict
)

is WsInteger -> Reference.Primitive(
type = Reference.Primitive.Type.Integer(when(value) {
"Integer32" -> Reference.Primitive.Type.Precision.P32
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ data object WsString : WirespecType
data object WsInteger : WirespecType
data object WsNumber : WirespecType
data object WsBoolean : WirespecType
data object WsBytes : WirespecType
data object CustomType : WirespecType
data object WsUnit : WirespecType

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ private fun Reference.Primitive.Type.produce() = when (this) {
is Reference.Primitive.Type.Integer -> WsPrimitiveType.Integer
is Reference.Primitive.Type.Number -> WsPrimitiveType.Number
is Reference.Primitive.Type.Boolean -> WsPrimitiveType.Boolean
is Reference.Primitive.Type.Bytes -> WsPrimitiveType.String
wilmveel marked this conversation as resolved.
Show resolved Hide resolved
}

private fun Endpoint.Method.produce() = when (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ object OpenApiV2Emitter: Emitter(noLogger) {
is Reference.Primitive.Type.Integer -> OpenApiType.INTEGER
is Reference.Primitive.Type.Number -> OpenApiType.NUMBER
is Reference.Primitive.Type.Boolean -> OpenApiType.BOOLEAN
is Reference.Primitive.Type.Bytes -> OpenApiType.STRING
}

private fun Reference.emitType() =
Expand All @@ -218,6 +219,8 @@ object OpenApiV2Emitter: Emitter(noLogger) {
Reference.Primitive.Type.Precision.P64 -> "int64"
}

is Reference.Primitive.Type.Bytes -> "binary"

else -> null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ object OpenApiV3Emitter : Emitter(noLogger) {
is Reference.Primitive.Type.Integer -> OpenApiType.INTEGER
is Reference.Primitive.Type.Number -> OpenApiType.NUMBER
is Reference.Primitive.Type.Boolean -> OpenApiType.BOOLEAN
is Reference.Primitive.Type.Bytes -> OpenApiType.STRING
}

private fun Endpoint.Content.emit(): Pair<MediaType, MediaTypeObject> =
Expand All @@ -246,6 +247,8 @@ object OpenApiV3Emitter : Emitter(noLogger) {
Reference.Primitive.Type.Precision.P64 -> "int64"
}

is Reference.Primitive.Type.Bytes -> "binary"

else -> null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import community.flock.wirespec.compiler.core.tokenize.types.RightCurly
import community.flock.wirespec.compiler.core.tokenize.types.StatusCode
import community.flock.wirespec.compiler.core.tokenize.types.WhiteSpace
import community.flock.wirespec.compiler.core.tokenize.types.WsBoolean
import community.flock.wirespec.compiler.core.tokenize.types.WsBytes
import community.flock.wirespec.compiler.core.tokenize.types.WsChannelDef
import community.flock.wirespec.compiler.core.tokenize.types.WsComment
import community.flock.wirespec.compiler.core.tokenize.types.WsEndpointDef
Expand Down Expand Up @@ -75,6 +76,7 @@ class Lexer : IntellijLexer() {
is WsInteger -> Types.INTEGER
is WsNumber -> Types.NUMBER
is WsBoolean -> Types.BOOLEAN
is WsBytes -> Types.BYTES
is CustomType -> Types.CUSTOM_TYPE
is WsUnit -> Types.UNIT
is Method -> Types.METHOD
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface Types {
val INTEGER = ElementType("INTEGER")
val NUMBER = ElementType("NUMBER")
val BOOLEAN = ElementType("BOOLEAN")
val BYTES = ElementType("BYTES")
val CUSTOM_TYPE = ElementType("CUSTOM_TYPE")
val UNIT = ElementType("UNIT")
val METHOD = ElementType("METHOD")
Expand Down
Loading
Loading