/** * Fill a builder with attribute values from the provided {@code Module} instance. * Regular attribute values will be replaced with those from the given instance. * Absent optional values will not replace present values. * Collection elements and entries will be added, not replaced. * @param instance The instance from which to copy values * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final Builder from(Module instance) { Preconditions.checkNotNull(instance, "instance"); name(instance.getName()); addAllProtos(instance.getProtos()); output(instance.getOutput()); putAllOptions(instance.getOptions()); usageIndex(instance.usageIndex()); return this; }
@Override public void compile(Module module) { String moduleOutput = module.getOutput(); processModule(module, moduleOutput); for (Proto proto : module.getProtos()) { processProto(moduleOutput, proto); for (Service service : proto.getServices()) { processService(moduleOutput, service); } processUserTypes(module, proto); } }
@Override public void compile(Module module) { module.getProtos() .forEach(proto -> rec(module, proto)); }
protected void write(Module module, String file, Object data) { Preconditions.checkNotNull(file); Preconditions.checkNotNull(data); LOGGER.info("Generate {}", file); String targetFile = module.getOutput() + "/" + file; try (OutputStream os = outputStreamFactory.createStream(targetFile)) { objectMapper.writeValue(os, data); } catch (Exception e) { throw new GeneratorException("Could not write " + targetFile, e); } }
@Override public ProtoCompiler get() { // deffer compiler instantiation until wrapper is called with // configured template and extension provider class return module -> { try { Map<String, Object> options = module.getOptions(); @SuppressWarnings("unchecked") Collection<String> templates = checkNotNull((Collection<String>) options.get(TEMPLATES_OPTION), TEMPLATES_OPTION + " is not set"); String extProviderClass = checkNotNull((String) options.get(EXTENSIONS_OPTION), EXTENSIONS_OPTION + " is not set"); ExtensionProvider extensionProvider = instantiate(extProviderClass, ExtensionProvider.class); ProtoCompiler compiler = factory.create(templates, extensionProvider); compiler.compile(module); } catch (Exception e) { throw new GeneratorException("Could not compile module: %s, module=%s", e, e.getMessage(), module); } }; }
private void process(Module module, Enum anEnum) { ImmutableEnumDescriptor descriptor = ImmutableEnumDescriptor.builder() .type(NodeType.ENUM) .name(anEnum.getName()) .canonicalName(anEnum.getCanonicalName()) .description(markdownProcessor.toHtml(anEnum.getComments())) .options(anEnum.getOptions().toMap()) .usages(module.usageIndex().getUsages(anEnum).stream() .map(type -> ImmutableUsageItem.builder() .ref(type.getCanonicalName()) .type(UsageType.from(type)) .build()) .collect(Collectors.toList())) .addAllConstants(anEnum.getConstants().stream() .map(enumConstant -> ImmutableEnumConstant.builder() .name(enumConstant.getName()) .value(enumConstant.getValue()) .description(markdownProcessor.toHtml(enumConstant.getComments())) .options(enumConstant.getOptions().toMap()) .build()) .collect(Collectors.toList())) .build(); String output = "data/type/" + anEnum.getCanonicalName() + ".json"; write(module, output, descriptor); } }
@Override public void compile(Module module) { module.getProtos() .forEach(proto -> rec(module, proto)); }
@Override public void compile(Module module) { String moduleOutput = module.getOutput(); processModule(module, moduleOutput); for (Proto proto : module.getProtos()) { processProto(moduleOutput, proto); for (Service service : proto.getServices()) { processService(moduleOutput, service); } processUserTypes(module, proto); } }
protected void write(Module module, String file, Object data) { Preconditions.checkNotNull(file); Preconditions.checkNotNull(data); LOGGER.info("Generate {}", file); String targetFile = module.getOutput() + "/" + file; try (OutputStream os = outputStreamFactory.createStream(targetFile)) { objectMapper.writeValue(os, data); } catch (Exception e) { throw new GeneratorException("Could not write " + targetFile, e); } }
@Override public ProtoCompiler get() { // deffer compiler instantiation until wrapper is called with // configured template and extension provider class return module -> { try { Map<String, Object> options = module.getOptions(); @SuppressWarnings("unchecked") Collection<String> templates = checkNotNull((Collection<String>) options.get(TEMPLATES_OPTION), TEMPLATES_OPTION + " is not set"); String extProviderClass = checkNotNull((String) options.get(EXTENSIONS_OPTION), EXTENSIONS_OPTION + " is not set"); ExtensionProvider extensionProvider = instantiate(extProviderClass, ExtensionProvider.class); ProtoCompiler compiler = factory.create(templates, extensionProvider); compiler.compile(module); } catch (Exception e) { throw new GeneratorException("Could not compile module: %s, module=%s", e, e.getMessage(), module); } }; }
private void process(Module module, Enum anEnum) { ImmutableEnumDescriptor descriptor = ImmutableEnumDescriptor.builder() .type(NodeType.ENUM) .name(anEnum.getName()) .canonicalName(anEnum.getCanonicalName()) .description(markdownProcessor.toHtml(anEnum.getComments())) .options(anEnum.getOptions().toMap()) .usages(module.usageIndex().getUsages(anEnum).stream() .map(type -> ImmutableUsageItem.builder() .ref(type.getCanonicalName()) .type(UsageType.from(type)) .build()) .collect(Collectors.toList())) .addAllConstants(anEnum.getConstants().stream() .map(enumConstant -> ImmutableEnumConstant.builder() .name(enumConstant.getName()) .value(enumConstant.getValue()) .description(markdownProcessor.toHtml(enumConstant.getComments())) .options(enumConstant.getOptions().toMap()) .build()) .collect(Collectors.toList())) .build(); String output = "data/type/" + anEnum.getCanonicalName() + ".json"; write(module, output, descriptor); } }
@Override public void compile(Module module) { module.getProtos() .forEach(proto -> process(module, proto)); }
@Override public void compile(Module module) { @SuppressWarnings("unchecked") List<StaticPage> pages = (List<StaticPage>) module.getOptions().get(PAGES); if (pages != null) { List<PageIndexItem> root = pages.stream() .map(page -> ImmutablePageIndexItem.builder() .name(page.getName()) .ref(FilenameUtils.getBaseName(page.getFile().getName())) .build()) .collect(Collectors.toList()); write(module, "data/pages.json", root); } } }
.description(markdownProcessor.toHtml(message.getComments())) .options(message.getOptions().toMap()) .usages(module.usageIndex().getUsages(message).stream() .map(type -> ImmutableUsageItem.builder() .ref(type.getCanonicalName())
@Override public void compile(Module module) { module.getProtos() .forEach(proto -> rec(module, proto)); }
@Override public void compile(Module module) { @SuppressWarnings("unchecked") List<StaticPage> pages = (List<StaticPage>) module.getOptions().get(PAGES); if (pages != null) { List<PageIndexItem> root = pages.stream() .map(page -> ImmutablePageIndexItem.builder() .name(page.getName()) .ref(FilenameUtils.getBaseName(page.getFile().getName())) .build()) .collect(Collectors.toList()); write(module, "data/pages.json", root); } } }
.description(markdownProcessor.toHtml(message.getComments())) .options(message.getOptions().toMap()) .usages(module.usageIndex().getUsages(message).stream() .map(type -> ImmutableUsageItem.builder() .ref(type.getCanonicalName())
@Override public void compile(Module module) { module.getProtos() .forEach(proto -> rec(module, proto)); }