/** Returns the type to import for {@code type}. */ private @Nullable ProtoType importedType(ProtoType type) { // Map key type is always scalar. if (type.isMap()) type = type.valueType(); return type.isScalar() ? null : type; }
/** Creates a map type. */ ProtoType(ProtoType keyType, ProtoType valueType, String string) { checkNotNull(keyType, "keyType == null"); checkNotNull(valueType, "valueType == null"); checkNotNull(string, "string == null"); checkArgument( keyType.isScalar() && !keyType.equals(BYTES) && !keyType.equals(DOUBLE) && !keyType.equals( FLOAT), "map key must be non-byte, non-floating point scalar: %s", keyType); this.isScalar = false; this.string = string; this.isMap = true; this.keyType = keyType; // TODO restrict what's allowed here this.valueType = valueType; }
private String adapterString(ProtoType type) { if (type.isScalar()) { return ProtoAdapter.class.getName() + '#' + type.toString().toUpperCase(Locale.US); } AdapterConstant adapterConstant = profile.getAdapter(type); if (adapterConstant != null) { return reflectionName(adapterConstant.className) + "#" + adapterConstant.memberName; } return reflectionName((ClassName) typeName(type)) + "#ADAPTER"; }
void validateImport(Location location, ProtoType type) { // Map key type is always scalar. No need to validate it. if (type.isMap()) type = type.valueType(); if (type.isScalar()) return; String path = location.path(); String requiredImport = get(type).location().path(); if (!path.equals(requiredImport) && !imports.containsEntry(path, requiredImport)) { addError("%s needs to import %s", path, requiredImport); } }
private CodeBlock singleAdapterFor(ProtoType type) { CodeBlock.Builder result = CodeBlock.builder(); if (type.isScalar()) { result.add("$T.$L", ADAPTER, type.simpleName().toUpperCase(Locale.US)); } else if (type.isMap()) { throw new IllegalArgumentException("Cannot create single adapter for map type " + type); } else { AdapterConstant adapterConstant = profile.getAdapter(type); if (adapterConstant != null) { result.add("$T.$L", adapterConstant.className, adapterConstant.memberName); } else { result.add("$T.ADAPTER", typeName(type)); } } return result.build(); }
result.addStatement("builder.$N = null", fieldName); } else if (!field.type().isScalar() && !isEnum(field.type())) { if (field.isRepeated()) { CodeBlock adapter = singleAdapterFor(field); } else if (field.type().isMap()) { if (!field.type().valueType().isScalar() && !isEnum(field.type().valueType())) { CodeBlock adapter = singleAdapterFor(field.type().valueType()); result.addStatement("$T.redactElements(builder.$N, $L)", Internal.class, fieldName,
private CodeBlock defaultValue(Field field) { Object defaultValue = field.getDefault(); if (defaultValue == null && isEnum(field.type())) { defaultValue = enumDefault(field.type()).name(); } if (field.type().isScalar() || defaultValue != null) { return fieldInitializer(field.type(), defaultValue); } throw new IllegalStateException("Field " + field + " cannot have default value"); }
if (protoType.isScalar()) { continue; // Skip scalar types.
private ProtoType resolveType(String name, boolean messageOnly) { ProtoType type = ProtoType.get(name); if (type.isScalar()) { if (messageOnly) { addError("expected a message but was %s", name); } return type; } if (type.isMap()) { if (messageOnly) { addError("expected a message but was %s", name); } ProtoType keyType = resolveType(type.keyType().toString(), false); ProtoType valueType = resolveType(type.valueType().toString(), false); return new ProtoType(keyType, valueType, name); } Type resolved = resolve(name, protoTypeNames); if (resolved == null) { addError("unable to resolve %s", name); return ProtoType.BYTES; // Just return any placeholder. } if (messageOnly && !(resolved instanceof MessageType)) { addError("expected a message but was %s", name); return ProtoType.BYTES; // Just return any placeholder. } return resolved.type(); }
TypeName fieldJavaType = fieldType(field); if ((field.type().isScalar() || isEnum(field.type())) && !field.isRepeated() && !field.isPacked()) {
/** Returns the type to import for {@code type}. */ private ProtoType importedType(ProtoType type) { // Map key type is always scalar. if (type.isMap()) { type = type.valueType(); } return type.isScalar() ? null : type; }
/** Creates a map type. */ ProtoType(ProtoType keyType, ProtoType valueType, String string) { checkNotNull(keyType, "keyType == null"); checkNotNull(valueType, "valueType == null"); checkNotNull(string, "string == null"); checkArgument( keyType.isScalar() && !keyType.equals(BYTES) && !keyType.equals(DOUBLE) && !keyType.equals( FLOAT), "map key must be non-byte, non-floating point scalar: %s", keyType); this.isScalar = false; this.string = string; this.isMap = true; this.keyType = keyType; // TODO restrict what's allowed here this.valueType = valueType; }
private String adapterString(ProtoType type) { return type.isScalar() ? ProtoAdapter.class.getName() + '#' + type.toString().toUpperCase(Locale.US) : reflectionName((ClassName) typeName(type)) + "#ADAPTER"; }
private String getTypeString(ProtoType type) { checkNotNull(type); if (type.isScalar()) { return type.toString(); } else if (type.isMap()) { return type.valueType().toString(); } else if (isEnum(type)) { return "enum"; } else { return "message"; } }
private CodeBlock singleAdapterFor(ProtoType type) { CodeBlock.Builder result = CodeBlock.builder(); if (type.isScalar()) { result.add("$T.$L", ADAPTER, type.simpleName().toUpperCase(Locale.US)); } else if (type.isMap()) { throw new IllegalArgumentException("Cannot create single adapter for map type " + type); } else { result.add("$T.ADAPTER", typeName(type)); } return result.build(); }
void validateImport(Location location, ProtoType type) { // Map key type is always scalar. No need to validate it. if (type.isMap()) type = type.valueType(); if (type.isScalar()) return; String path = location.path(); String requiredImport = get(type).location().path(); if (!path.equals(requiredImport) && !imports.containsEntry(path, requiredImport)) { addError("%s needs to import %s", path, requiredImport); } }
void validateImport(Location location, ProtoType type) { // Map key type is always scalar. No need to validate it. if (type.isMap()) type = type.valueType(); if (type.isScalar()) return; String path = location.path(); String requiredImport = get(type).location().path(); if (!path.equals(requiredImport) && !imports.containsEntry(path, requiredImport)) { addError("%s needs to import %s", path, requiredImport); } }
private CodeBlock defaultValue(Field field) { Object defaultValue = field.getDefault(); if (defaultValue == null && isEnum(field.type())) { defaultValue = enumDefault(field.type()).name(); } if (field.type().isScalar() || defaultValue != null) { return fieldInitializer(field.type(), defaultValue); } throw new IllegalStateException("Field " + field + " cannot have default value"); }
public Schema getSchemaByType(ProtoType protoType) { if (protoType.isScalar()) { return SchemaUtil.getSchema(protoType, this); } else if (protoType.isMap()) { return new Schema().additionalProperties(getSchemaByType(protoType.valueType())).type("object"); } else { return new Schema().$ref(getRefer(protoType)); } } }