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 all 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
Expand Up @@ -4,9 +4,12 @@
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.generated.java.UserDto;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
Expand All @@ -15,11 +18,13 @@

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 +65,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 @@ -8,6 +8,7 @@
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.uploadImageByName;
import static org.junit.jupiter.api.Assertions.assertEquals;

class UserTest {
Expand Down Expand Up @@ -47,6 +48,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 @@ -19,6 +19,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.WirespecType
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.WsInteger
import community.flock.wirespec.compiler.core.tokenize.types.WsNumber
import community.flock.wirespec.compiler.core.tokenize.types.WsString
Expand Down Expand Up @@ -246,6 +247,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 @@ -20,6 +20,7 @@ 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.WsBytes
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
Expand Down Expand Up @@ -83,20 +84,30 @@ 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
else -> Reference.Primitive.Type.Precision.P64
}),
type = Reference.Primitive.Type.Integer(
when (value) {
"Integer32" -> Reference.Primitive.Type.Precision.P32
else -> Reference.Primitive.Type.Precision.P64
}
),
isIterable = isIterable,
isDictionary = isDict
)

is WsNumber -> Reference.Primitive(
type = Reference.Primitive.Type.Number(when(value) {
"Number32" -> Reference.Primitive.Type.Precision.P32
else -> Reference.Primitive.Type.Precision.P64
}),
type = Reference.Primitive.Type.Number(
when (value) {
"Number32" -> Reference.Primitive.Type.Precision.P32
else -> Reference.Primitive.Type.Precision.P64
}
),
isIterable = isIterable,
isDictionary = isDict
)
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 @@ -150,6 +150,7 @@ private fun WsPrimitiveType.consume() =
WsPrimitiveType.Integer -> Reference.Primitive.Type.Integer()
WsPrimitiveType.Number -> Reference.Primitive.Type.Number()
WsPrimitiveType.Boolean -> Reference.Primitive.Type.Boolean
WsPrimitiveType.Bytes -> Reference.Primitive.Type.Bytes
}


Expand Down Expand Up @@ -233,6 +234,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.Bytes
}

private fun Endpoint.Method.produce() = when (this) {
Expand Down Expand Up @@ -375,7 +377,7 @@ data class WsPrimitive(
) : WsReference

@JsExport
enum class WsPrimitiveType { String, Integer, Number, Boolean }
enum class WsPrimitiveType { String, Integer, Number, Boolean, Bytes }

@JsExport
data class WsRequest(val content: WsContent?)
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
@@ -1,6 +1,12 @@
package community.flock.wirespec.ide.intellij

import com.intellij.openapi.editor.DefaultLanguageHighlighterColors.*
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors.BRACKETS
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors.COMMA
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors.DOC_COMMENT_MARKUP
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors.IDENTIFIER
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors.KEYWORD
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors.PARAMETER
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors.SEMICOLON
import com.intellij.openapi.fileTypes.SyntaxHighlighterBase
import com.intellij.openapi.fileTypes.SyntaxHighlighterFactory
import com.intellij.openapi.project.Project
Expand Down
Loading
Loading