public final String toSchema() { Object value = value(); switch (kind()) { case STRING: return formatName() + " = \"" + value + '"'; case BOOLEAN: case NUMBER: case ENUM: return formatName() + " = " + value; case OPTION: { StringBuilder builder = new StringBuilder(); OptionElement.create(optionValue.name(), optionValue.kind(), optionValue.value()); builder.append(formatName()).append('.').append(optionValue.toSchema()); return builder.toString(); builder.append(formatName()).append(" = {\n"); formatOptionMap(builder, valueMap); builder.append('}'); return builder.toString(); builder.append(formatName()).append(" = ");
static String formatOptionMapValue(Object value) { checkNotNull(value, "value == null"); if (value instanceof String) { return "\"" + value + '"'; } if (value instanceof Map) { StringBuilder builder = new StringBuilder().append("{\n"); //noinspection unchecked Map<String, ?> map = (Map<String, ?>) value; formatOptionMap(builder, map); return builder.append('}').toString(); } if (value instanceof List) { StringBuilder builder = new StringBuilder().append("[\n"); List<?> list = (List<?>) value; for (int i = 0, count = list.size(); i < count; i++) { String endl = (i < count - 1) ? "," : ""; appendIndented(builder, formatOptionMapValue(list.get(i)) + endl); } return builder.append("]").toString(); } return value.toString(); }
private String formatName() { return isParenthesized() ? '(' + name() + ')' : name(); } }
/** * Defaults aren't options. This finds an option named "default", removes, and returns it. Returns * null if no default option is present. */ private @Nullable String stripDefault(List<OptionElement> options) { String result = null; for (Iterator<OptionElement> i = options.iterator(); i.hasNext();) { OptionElement option = i.next(); if (option.name().equals("default")) { i.remove(); result = String.valueOf(option.value()); // Defaults aren't options! } } return result; }
public static OptionElement create(String name, Kind kind, Object value) { return create(name, kind, value, false); }
@Test public void listToSchema() { OptionElement option = OptionElement.create("foo", LIST, ImmutableList.of(OptionElement.create("ping", STRING, "pong", true), OptionElement.create("kit", STRING, "kat")), true); String expected = "" + "(foo) = [\n" + " (ping) = \"pong\",\n" + " kit = \"kat\"\n" + "]"; assertThat(option.toSchema()).isEqualTo(expected); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof OptionElement) { OptionElement that = (OptionElement) o; return (this.name.equals(that.name())) && (this.kind.equals(that.kind())) && (this.value.equals(that.value())) && (this.isParenthesized == that.isParenthesized()); } return false; }
public final String toSchemaDeclaration() { return "option " + toSchema() + ";\n"; }
public final String toSchema() { StringBuilder builder = new StringBuilder(); appendDocumentation(builder, documentation()); builder.append("rpc ") .append(name()) .append(" ("); if (requestStreaming()) { builder.append("stream "); } builder.append(requestType()) .append(") returns ("); if (responseStreaming()) { builder.append("stream "); } builder.append(responseType()) .append(')'); if (!options().isEmpty()) { builder.append(" {\n"); for (OptionElement option : options()) { appendIndented(builder, option.toSchemaDeclaration()); } builder.append("}"); } return builder.append(";\n").toString(); }
static void formatOptionMap(StringBuilder builder, Map<String, ?> valueMap) { List<? extends Map.Entry<String, ?>> entries = new ArrayList<>(valueMap.entrySet()); for (int i = 0, count = entries.size(); i < count; i++) { Map.Entry<String, ?> entry = entries.get(i); String endl = (i < count - 1) ? "," : ""; appendIndented(builder, entry.getKey() + ": " + formatOptionMapValue(entry.getValue()) + endl); } }
@Test public void field() { FieldElement field = FieldElement.builder(location) .label(OPTIONAL) .type("CType") .name("ctype") .tag(1) .options(ImmutableList.of( OptionElement.create("default", Kind.ENUM, "TEST"), OptionElement.create("deprecated", Kind.BOOLEAN, "true"))) .build(); assertThat(field.options()).containsOnly( // OptionElement.create("default", Kind.ENUM, "TEST"), // OptionElement.create("deprecated", Kind.BOOLEAN, "true")); }
public static void appendOptions(StringBuilder builder, List<OptionElement> options) { builder.append("[\n"); for (int i = 0, count = options.size(); i < count; i++) { String endl = (i < count - 1) ? "," : ""; appendIndented(builder, options.get(i).toSchema() + endl); } builder.append(']'); }
@Override public final String toSchema() { StringBuilder builder = new StringBuilder(); appendDocumentation(builder, documentation()); builder.append("enum ") .append(name()) .append(" {"); if (!options().isEmpty()) { builder.append('\n'); for (OptionElement option : options()) { appendIndented(builder, option.toSchemaDeclaration()); } } if (!constants().isEmpty()) { builder.append('\n'); for (EnumConstantElement constant : constants()) { appendIndented(builder, constant.toSchema()); } } return builder.append("}\n").toString(); }
static void formatOptionMap(StringBuilder builder, Map<String, ?> valueMap) { List<? extends Map.Entry<String, ?>> entries = new ArrayList<>(valueMap.entrySet()); for (int i = 0, count = entries.size(); i < count; i++) { Map.Entry<String, ?> entry = entries.get(i); String endl = (i < count - 1) ? "," : ""; appendIndented(builder, entry.getKey() + ": " + formatOptionMapValue(entry.getValue()) + endl); } }
public final String toSchema() { Object value = value(); switch (kind()) { case STRING: return formatName() + " = \"" + value + '"'; case BOOLEAN: case NUMBER: case ENUM: return formatName() + " = " + value; case OPTION: { StringBuilder builder = new StringBuilder(); OptionElement.create(optionValue.name(), optionValue.kind(), optionValue.value()); builder.append(formatName()).append('.').append(optionValue.toSchema()); return builder.toString(); builder.append(formatName()).append(" = {\n"); formatOptionMap(builder, valueMap); builder.append('}'); return builder.toString(); builder.append(formatName()).append(" = [\n"); formatOptionList(builder, optionList); builder.append(']'); return builder.toString();
.name("ExoticOptions") .options(ImmutableList.of( OptionElement.create("squareup.one", Kind.MAP, option_one_map, true), OptionElement.create("squareup.two.a", Kind.MAP, option_two_a_map, true), OptionElement.create("squareup.two.b", Kind.MAP, option_two_b_map, true), OptionElement.create("squareup.three", Kind.MAP, option_three_map, true), OptionElement.create("squareup.four", Kind.MAP, option_four_map, true))) .build())) .build();
Field field = messageType.field(option.name()); if (field != null) { path = new String[] {option.name()}; } else { path = resolveFieldPath(option.name(), extensionsForType.keySet()); String packageName = linker.packageName(); if (path == null && packageName != null) { path = resolveFieldPath(packageName + "." + option.name(), extensionsForType.keySet()); canonicalizeValue(linker, field, option.value())); return result;