com.google.protobuf.Api Java Examples

The following examples show how to use com.google.protobuf.Api. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DescriptorGenerator.java    From api-compiler with Apache License 2.0 6 votes vote down vote up
private FileDescriptorProto generateFile(String name, FileContents contents) {
  FileDescriptorProto.Builder fileBuilder = FileDescriptorProto.newBuilder();
  fileBuilder.setName(name);
  if (!Strings.isNullOrEmpty(contents.packageName)) {
    fileBuilder.setPackage(contents.packageName);
  }
  for (Api api : contents.apis) {
    fileBuilder.addService(generateApi(api));
  }
  for (Type type : contents.types.values()) {
    fileBuilder.addMessageType(generateType(type, contents));
  }
  for (Enum e : contents.enums) {
    fileBuilder.addEnumType(generateEnum(e));
  }
  if (imports.containsKey(name)) {
    for (String imported : imports.get(name)) {
      fileBuilder.addDependency(imported);
    }
  }
  return fileBuilder.build();
}
 
Example #2
Source File: Model.java    From api-compiler with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a model from a normalized service config, rather than from descriptor and .yaml files.
 */
public static Model create(Service normalizedConfig) {
  FileDescriptorSet regeneratedDescriptor = DescriptorGenerator.generate(normalizedConfig);
  Model model = create(regeneratedDescriptor);

  // Configured with a stripped Service
  Service.Builder builder = normalizedConfig.toBuilder();
  ImmutableList.Builder<Api> strippedApis = ImmutableList.builder();
  for (Api api : normalizedConfig.getApisList()) {
    strippedApis.add(
        Api.newBuilder().setName(api.getName()).setVersion(api.getVersion()).build());
  }
  // NOTE: Documentation may still contain text from the original protos.
  builder.clearEnums();
  builder.clearTypes();
  builder.clearApis();
  builder.addAllApis(strippedApis.build());
  ConfigSource strippedConfig = ConfigSource.newBuilder(builder.build()).build();

  model.setConfigSources(ImmutableList.of(strippedConfig));

  return model;
}
 
Example #3
Source File: ProtoApiFromOpenApi.java    From api-compiler with Apache License 2.0 6 votes vote down vote up
public ProtoApiFromOpenApi(
    DiagCollector diagCollector,
    TypeBuilder typeBuilder,
    String filename,
    String apiName,
    HttpRuleGenerator httpRuleGenerator,
    AuthRuleGenerator authRuleGenerator,
    MetricRuleGenerator metricRuleGenerator,
    AuthBuilder authBuilder) {
  this.typeBuilder = typeBuilder;
  this.diagCollector = diagCollector;
  coreApiBuilder = Api.newBuilder().setName(apiName);
  coreApiBuilder.getSourceContextBuilder().setFileName(filename);
  this.httpRuleGenerator = httpRuleGenerator;
  this.authRuleGenerator = authRuleGenerator;
  this.authBuilder = authBuilder;
  this.metricRuleGenerator = metricRuleGenerator;
}
 
Example #4
Source File: DescriptorGenerator.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
private ServiceDescriptorProto generateApi(Api api) {
  ServiceDescriptorProto.Builder builder = ServiceDescriptorProto.newBuilder();
  builder.setName(getSimpleName(api.getName()));
  for (Method method : api.getMethodsList()) {
    builder.addMethod(generateMethod(method));
  }
  if (!api.getOptionsList().isEmpty()) {
    builder.setOptions(generateServiceOptions(api));
  }
  return builder.build();
}
 
Example #5
Source File: DescriptorNormalizer.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
@VisitsBefore
void normalize(Interface iface) {
  Api.Builder coreApiBuilder = Api.newBuilder().setName(iface.getFullName());
  coreApiBuilder.setSourceContext(
      SourceContext.newBuilder().setFileName(iface.getFile().getLocation().getDisplayString()));
  coreApiBuilder.setSyntax(iface.getSyntax());

  for (Method method : iface.getReachableMethods()) {
    com.google.protobuf.Method.Builder coreMethodBuilder =
        com.google.protobuf.Method.newBuilder()
            .setName(method.getSimpleName())
            .setRequestTypeUrl(generateTypeUrl(method.getInputType()))
            .setResponseTypeUrl(generateTypeUrl(method.getOutputType()));

    coreMethodBuilder.setRequestStreaming(method.getRequestStreaming());
    coreMethodBuilder.setResponseStreaming(method.getResponseStreaming());
    coreMethodBuilder.addAllOptions(
        DescriptorNormalization.getMethodOptions(
            method.getOptionFields(),
            false,
            includeDefaults));
    coreApiBuilder.addMethods(coreMethodBuilder);
  }

  coreApiBuilder.addAllOptions(
      DescriptorNormalization.getOptions(iface.getProto(), includeDefaults));
  coreApiBuilder.setVersion(iface.getAttribute(VersionAttribute.KEY).majorVersion());
  apis.add(coreApiBuilder.build());
}
 
Example #6
Source File: VersionConfigAspect.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
private void merge(Interface iface) {
  Api api = iface.getConfig();
  if (api == null) {
      return;
  }
  // Get user-defined api version, which is optional.
  String apiVersion = api.getVersion();
  String packageName = iface.getFile().getFullName();
  if (Strings.isNullOrEmpty(apiVersion)) {
    // If version is not provided by user, extract major version from package name.
    apiVersion = ApiVersionUtil.extractDefaultMajorVersionFromPackageName(packageName);
  }
  iface.setConfig(api.toBuilder().setVersion(apiVersion).build());
  iface.putAttribute(VersionAttribute.KEY, VersionAttribute.create(apiVersion));
}
 
Example #7
Source File: MixinConfigAspect.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
/**
 * Analyze any mixins and attach attributes related to interfaces and methods which are used by
 * the main merging phase.
 */
@Override
public void startMerging() {
  for (Api api : getModel().getServiceConfig().getApisList()) {
    // Resolve the including interface.
    Interface including =
        resolveInterface(
            api.getName(), MessageLocationContext.create(api, Api.NAME_FIELD_NUMBER));

    // Process each mixin declaration.
    for (Mixin mixin : api.getMixinsList()) {
      Interface included =
          resolveInterface(
              mixin.getName(), MessageLocationContext.create(mixin, Mixin.NAME_FIELD_NUMBER));
      if (including == null || included == null) {
        // Errors have been reported.
        continue;
      }

      // Attach the mixin attribute.
      MixinAttribute mixinAttr = MixinAttribute.create(included, mixin);
      including.addAttribute(MixinAttribute.KEY, mixinAttr);

      // Process each method in the included interface.
      for (Method method : included.getMethods()) {
        startMergeMethod(including, method);
      }
    }
  }
}
 
Example #8
Source File: Convert.java    From metastore with Apache License 2.0 4 votes vote down vote up
private static Descriptors.FileDescriptor convertToFileDescriptorMap(
    String name,
    String parent,
    Map<String, DescriptorProtos.FileDescriptorProto> inMap,
    Map<String, Descriptors.FileDescriptor> outMap,
    ExtensionRegistry extensionRegistry) {
  if (outMap.containsKey(name)) {
    return outMap.get(name);
  }
  Descriptors.FileDescriptor fd;
  switch (name) {
    case "google/protobuf/descriptor.proto":
      fd = DescriptorProtos.FieldOptions.getDescriptor().getFile();
      break;
    case "google/protobuf/wrappers.proto":
      fd = Int32Value.getDescriptor().getFile();
      break;
    case "google/protobuf/timestamp.proto":
      fd = Timestamp.getDescriptor().getFile();
      break;
    case "google/protobuf/duration.proto":
      fd = Duration.getDescriptor().getFile();
      break;
    case "google/protobuf/any.proto":
      fd = Any.getDescriptor().getFile();
      break;
    case "google/protobuf/api.proto":
      fd = Api.getDescriptor().getFile();
      break;
    case "google/protobuf/empty.proto":
      fd = Empty.getDescriptor().getFile();
      break;
    case "google/protobuf/field_mask.proto":
      fd = FieldMask.getDescriptor().getFile();
      break;
    case "google/protobuf/source_context.proto":
      fd = SourceContext.getDescriptor().getFile();
      break;
    case "google/protobuf/struct.proto":
      fd = Struct.getDescriptor().getFile();
      break;
    case "google/protobuf/type.proto":
      fd = Type.getDescriptor().getFile();
      break;
    default:
      DescriptorProtos.FileDescriptorProto fileDescriptorProto = inMap.get(name);
      if (fileDescriptorProto == null) {
        if (parent == null) {
          throw new IllegalArgumentException(
              String.format("Couldn't find file \"%1s\" in file descriptor set", name));
        }
        throw new IllegalArgumentException(
            String.format("Couldn't find file \"%1s\", imported by \"%2s\"", name, parent));
      }
      List<Descriptors.FileDescriptor> dependencies = new ArrayList<>();
      if (fileDescriptorProto.getDependencyCount() > 0) {
        fileDescriptorProto
            .getDependencyList()
            .forEach(
                dependencyName ->
                    dependencies.add(
                        convertToFileDescriptorMap(
                            dependencyName, name, inMap, outMap, extensionRegistry)));
      }
      try {
        fd =
            Descriptors.FileDescriptor.buildFrom(
                fileDescriptorProto, dependencies.toArray(new Descriptors.FileDescriptor[0]));

      } catch (Descriptors.DescriptorValidationException e) {
        throw new RuntimeException(e);
      }
  }
  outMap.put(name, fd);
  return fd;
}
 
Example #9
Source File: DescriptorGenerator.java    From api-compiler with Apache License 2.0 4 votes vote down vote up
private ServiceOptions generateServiceOptions(Api api) {
  ServiceOptions.Builder serviceOptionsBuilder = ServiceOptions.newBuilder();
  setOptions(serviceOptionsBuilder, api.getOptionsList(), SERVICE_OPTION_NAME_PREFIX);
  return serviceOptionsBuilder.build();
}
 
Example #10
Source File: Interface.java    From api-compiler with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the api config.
 */
@Requires(Merged.class)
public Api getConfig() {
  return config;
}
 
Example #11
Source File: Interface.java    From api-compiler with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the api config.
 */
public void setConfig(Api config) {
  this.config = config;
}