From 75a92efe9ee1f9b0d7ca34f9f81af25e7921020e Mon Sep 17 00:00:00 2001 From: Derek Perez Date: Sun, 4 Aug 2019 18:46:18 -0700 Subject: [PATCH] introduces FieldType.inferFrom and a simplified OptionSpec.Builder.setValue which makes use of it. --- java/protopoet/FieldType.java | 51 ++++++++++++++++++++++++++++------ java/protopoet/OptionSpec.java | 10 +++++++ 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/java/protopoet/FieldType.java b/java/protopoet/FieldType.java index 24d9801..2d56144 100644 --- a/java/protopoet/FieldType.java +++ b/java/protopoet/FieldType.java @@ -16,9 +16,9 @@ package protopoet; -/** - * All standard scalar types a field may represent. - * Learn more: https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#fields +/** + * All standard scalar types a field may represent. Learn more: + * https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#fields */ public enum FieldType { DOUBLE, @@ -39,6 +39,41 @@ public enum FieldType { MESSAGE, ENUM; + /** + * Attempts to guess at the FieldType based on the provided value. Note, this outputs INT32 + * forInteger and INT64 for Long, if you need more specific types for numerics, don't use this. + */ + public static FieldType inferFrom(Object value) { + if (value instanceof Double) { + return DOUBLE; + } + if (value instanceof Float) { + return FLOAT; + } + if (value instanceof Integer) { + return INT32; + } + if (value instanceof Long) { + return INT64; + } + if (value instanceof Boolean) { + return BOOL; + } + if (value instanceof String) { + return STRING; + } + if (value instanceof byte[]) { + return BYTES; + } + if (value instanceof Enum) { + return ENUM; + } + if (value instanceof Object) { + return MESSAGE; + } + throw new IllegalArgumentException("unable to express FieldType for value"); + } + @Override public String toString() { return name().toLowerCase(); @@ -46,11 +81,11 @@ public String toString() { String formatValue(Object value) { switch (this) { - case STRING: - case BYTES: - return String.format("\"%s\"", value); - default: - return value.toString(); + case STRING: + case BYTES: + return String.format("\"%s\"", value); + default: + return value.toString(); } } } diff --git a/java/protopoet/OptionSpec.java b/java/protopoet/OptionSpec.java index 4c2bff8..675248d 100644 --- a/java/protopoet/OptionSpec.java +++ b/java/protopoet/OptionSpec.java @@ -252,6 +252,16 @@ public Builder setOptionComment(String... lines) { return setOptionComment(ImmutableList.copyOf(lines)); } + /** + * Sets value and infers the field type based on passed in value. Check {@link FieldType} for + * more info on how it does it, if you need more granularity or predictibility, don't use this. + */ + public Builder setValue(Object value) { + optionValueType = FieldType.inferFrom(value); + optionValue = value; + return this; + } + /** Sets an integer-based value (eg: int32, uint32, fixed32, sfixed32). */ public Builder setValue(FieldType valueType, int intValue) { switch (valueType) {