Skip to content

Commit

Permalink
Properly map List -> list and Map -> dict for ApiExporter
Browse files Browse the repository at this point in the history
Starlark.fromJava() converts these into their Starlark equivalents.

PiperOrigin-RevId: 511823824
Change-Id: If35f8d5f3327b8bae19d75e100c468da4fafc024
  • Loading branch information
DavidGoldman authored and copybara-github committed Feb 23, 2023
1 parent 023c2ec commit c65860b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public String getReturnTypeExtraMessage() {

@Override
public String getReturnType() {
return Starlark.classType(method.getReturnType());
return Starlark.classTypeFromJava(method.getReturnType());
}

@Override
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/net/starlark/java/eval/Starlark.java
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,42 @@ public static String classType(Class<?> c) {
}
}

/**
* Returns the name of the type of instances of {@code c} after being converted to Starlark values
* by {@link #fromJava}, or "unknown" for {@code Object.class}, since that is used as a wildcard
* type by evaluation machinery.
*
* <p>Note that {@code void.class} is treated as "NoneType" since void methods will return None to
* Starlark.
*
* @throws InvalidStarlarkValueException if {@code c} is not {@code Object.class} and {@link
* #fromJava} would throw for instances of {@code c}.
*/
public static String classTypeFromJava(Class<?> c) {
if (c.equals(
void.class) // Method.invoke on void-returning methods returns null; we treat it as None
|| c.equals(String.class)
|| c.equals(boolean.class)
|| c.equals(Boolean.class)
|| StarlarkValue.class.isAssignableFrom(c)
|| c.equals(Object.class)) {
return classType(c);
} else if (c.equals(int.class)
|| c.equals(Integer.class)
|| c.equals(long.class)
|| c.equals(Long.class)
|| BigInteger.class.isAssignableFrom(c)) {
return classType(StarlarkInt.class);
} else if (c.equals(double.class) || c.equals(Double.class)) {
return classType(StarlarkFloat.class);
} else if (List.class.isAssignableFrom(c)) {
return classType(StarlarkList.class);
} else if (Map.class.isAssignableFrom(c)) {
return classType(Dict.class);
}
throw new InvalidStarlarkValueException(c);
}

/**
* The ordering relation over (some) Starlark values.
*
Expand Down

0 comments on commit c65860b

Please sign in to comment.