Skip to content

Commit

Permalink
fix(sdk/java): handle float type (and others) (dagger#9647)
Browse files Browse the repository at this point in the history
Signed-off-by: Yves Brissaud <gh@lgtd.io>
  • Loading branch information
eunomie authored Feb 20, 2025
1 parent 2699209 commit 11b7794
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -609,10 +609,22 @@ static CodeBlock typeDef(TypeInfo ti) throws ClassNotFoundException {
}

static TypeInfo tiFromName(String name) {
if (name.equals("int")) {
return new TypeInfo(name, TypeKind.INT.name());
} else if (name.equals("boolean")) {
if (name.equals("boolean")) {
return new TypeInfo(name, TypeKind.BOOLEAN.name());
} else if (name.equals("byte")) {
return new TypeInfo(name, TypeKind.BYTE.name());
} else if (name.equals("short")) {
return new TypeInfo(name, TypeKind.SHORT.name());
} else if (name.equals("int")) {
return new TypeInfo(name, TypeKind.INT.name());
} else if (name.equals("long")) {
return new TypeInfo(name, TypeKind.LONG.name());
} else if (name.equals("char")) {
return new TypeInfo(name, TypeKind.CHAR.name());
} else if (name.equals("float")) {
return new TypeInfo(name, TypeKind.FLOAT.name());
} else if (name.equals("double")) {
return new TypeInfo(name, TypeKind.DOUBLE.name());
} else if (name.equals("void")) {
return new TypeInfo(name, TypeKind.VOID.name());
} else {
Expand All @@ -623,10 +635,22 @@ static TypeInfo tiFromName(String name) {
static CodeBlock typeName(TypeInfo ti) {
try {
TypeKind tk = TypeKind.valueOf(ti.kindName());
if (tk == TypeKind.INT) {
return CodeBlock.of("$T", int.class);
} else if (tk == TypeKind.BOOLEAN) {
if (tk == TypeKind.BOOLEAN) {
return CodeBlock.of("$T", boolean.class);
} else if (tk == TypeKind.BYTE) {
return CodeBlock.of("$T", byte.class);
} else if (tk == TypeKind.SHORT) {
return CodeBlock.of("$T", short.class);
} else if (tk == TypeKind.INT) {
return CodeBlock.of("$T", int.class);
} else if (tk == TypeKind.LONG) {
return CodeBlock.of("$T", long.class);
} else if (tk == TypeKind.CHAR) {
return CodeBlock.of("$T", char.class);
} else if (tk == TypeKind.FLOAT) {
return CodeBlock.of("$T", float.class);
} else if (tk == TypeKind.DOUBLE) {
return CodeBlock.of("$T", double.class);
} else if (tk == TypeKind.VOID) {
return CodeBlock.of("$T", void.class);
} else if (tk == TypeKind.ARRAY) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ private ModuleID register() throws ExecutionException, DaggerQueryException,
dag.function("defaultPlatform",
dag.typeDef().withScalar("Platform"))
.withDescription("return the default platform as a Scalar value"))
.withFunction(
dag.function("addFloat",
dag.typeDef().withKind(TypeDefKind.FLOAT_KIND))
.withArg("a", dag.typeDef().withKind(TypeDefKind.FLOAT_KIND))
.withArg("b", dag.typeDef().withKind(TypeDefKind.FLOAT_KIND)))
.withField("source", dag.typeDef().withObject("Directory"), new TypeDef.WithFieldArguments().withDescription("Project source directory"))
.withField("version", dag.typeDef().withKind(TypeDefKind.STRING_KIND)));
return module.id();
Expand Down Expand Up @@ -230,6 +235,18 @@ private JSON invoke(JSON parentJson, String parentName, String fnName,
Method fn = clazz.getMethod("defaultPlatform");
Platform res = (Platform) fn.invoke(obj);
return JsonConverter.toJSON(res);
} else if (fnName.equals("addFloat")) {
float a = 0;
if (inputArgs.get("a") != null) {
a = (float) JsonConverter.fromJSON(dag, inputArgs.get("a"), float.class);
}
float b = 0;
if (inputArgs.get("b") != null) {
b = (float) JsonConverter.fromJSON(dag, inputArgs.get("b"), float.class);
}
Method fn = clazz.getMethod("addFloat", float.class, float.class);
float res = (float) fn.invoke(obj, a, b);
return JsonConverter.toJSON(res);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,13 @@ public String nullableDefault(@Nullable @Default("Foo") String stringArg) {

/** return the default platform as a Scalar value */
@Function
public Platform defaultPlatform() throws InterruptedException, ExecutionException, DaggerQueryException {
public Platform defaultPlatform()
throws InterruptedException, ExecutionException, DaggerQueryException {
return dag.defaultPlatform();
}

@Function
public float addFloat(float a, float b) {
return a + b;
}
}

0 comments on commit 11b7794

Please sign in to comment.