google-gemini / generative-ai-android

The official Android library for the Google Gemini API
https://ai.google.dev/gemini-api/docs/get-started/tutorial?lang=android
Apache License 2.0
734 stars 160 forks source link

A generic serializer for enum with caches. Work with @SerialName annotation. #181

Closed karloti closed 5 months ago

karloti commented 5 months ago

extension function from caches. HashMap implementation: O(1) require(USA.serialName == "UNITED_STATES_OF_AMERICA") require("USA".enumByName() == USA) require("USA".enumBySerialName() == null) require("OTHER".enumByName() == OTHER) require("OTHER".enumBySerialName() == null) require("UNITED_STATES_OF_AMERICA".enumByName() == null) require("UNITED_STATES_OF_AMERICA".enumBySerialName() == USA) require("MISSING".enumByName() == null) require("MISSING".enumBySerialName() == null)

serialize with @SerialName require(Json.encodeToString(USA) == "\"UNITED_STATES_OF_AMERICA\"")

serialize without @SerialName require(Json.encodeToString(OTHER) == "\"OTHER\"")

deserialize with @SerialName or enumName require(Json.decodeFromString<Country?>("\"UNITED_STATES_OF_AMERICA\"") == USA) require(Json.decodeFromString<Country?>("\"USA\"") == USA) require(Json.decodeFromString<Country?>("\"OTHER\"") == OTHER) require(Json.decodeFromString<Country?>("null") == null)

deserialize missing enum with warning message Unknown enum value found: "MISSING" in Country This usually means the backend was updated, and the SDK needs to be updated to match it. Check if there's a new version for the SDK, otherwise please open an issue on our GitHub to bring it to our attention: https://github.com/google/google-ai-android require(Json.decodeFromString<Country?>("\"MISSING\"") == null)

image

image

image

image

image

google-cla[bot] commented 5 months ago

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.