Java Code Examples for com.google.protobuf.ListValue#newBuilder()

The following examples show how to use com.google.protobuf.ListValue#newBuilder() . 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: ArrayCodec.java    From cloud-spanner-r2dbc with Apache License 2.0 5 votes vote down vote up
@Override
Value doEncode(A[] value) {
  Builder builder = ListValue.newBuilder();
  for (A val : value) {
    builder.addValues(this.codecs.encode(val));
  }
  return Value.newBuilder().setListValue(builder.build()).build();
}
 
Example 2
Source File: MessageMarshallerTest.java    From curiostack with MIT License 5 votes vote down vote up
@Test
public void struct() throws Exception {
  // Build a struct with all possible values.
  TestStruct.Builder builder = TestStruct.newBuilder();
  Struct.Builder structBuilder = builder.getStructValueBuilder();
  structBuilder.putFields("null_value", Value.newBuilder().setNullValueValue(0).build());
  structBuilder.putFields("number_value", Value.newBuilder().setNumberValue(1.25).build());
  structBuilder.putFields("string_value", Value.newBuilder().setStringValue("hello").build());
  Struct.Builder subStructBuilder = Struct.newBuilder();
  subStructBuilder.putFields("number_value", Value.newBuilder().setNumberValue(1234).build());
  structBuilder.putFields(
      "struct_value", Value.newBuilder().setStructValue(subStructBuilder.build()).build());
  ListValue.Builder listBuilder = ListValue.newBuilder();
  listBuilder.addValues(Value.newBuilder().setNumberValue(1.125).build());
  listBuilder.addValues(Value.newBuilder().setNullValueValue(0).build());
  structBuilder.putFields(
      "list_value", Value.newBuilder().setListValue(listBuilder.build()).build());
  TestStruct message = builder.build();
  assertMatchesUpstream(message);

  builder = TestStruct.newBuilder();
  builder.setValue(Value.newBuilder().setNullValueValue(0).build());
  message = builder.build();
  assertMatchesUpstream(message);

  builder = TestStruct.newBuilder();
  listBuilder = builder.getListValueBuilder();
  listBuilder.addValues(Value.newBuilder().setNumberValue(31831.125).build());
  listBuilder.addValues(Value.newBuilder().setNullValueValue(0).build());
  message = builder.build();
  assertMatchesUpstream(message);
}
 
Example 3
Source File: VcfToVariant.java    From genomewarp with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static Map<String, ListValue> getInfo(VariantContext vc, VCFHeader header) {
  Map<String, ListValue> toReturn = new HashMap<>();

  for (Map.Entry<String, Object> entry : vc.getAttributes().entrySet()) {
    String currKey = entry.getKey();
    VCFInfoHeaderLine metaData = header.getInfoHeaderLine(currKey);

    // All info fields must have a corresponding header field.
    if (metaData == null) {
      logger.log(Level.WARNING, String.format("Could not find matching VCF header field, "
          + "skipping info field %s", currKey));
      continue;
    }

    Object currObject = entry.getValue();
    ListValue.Builder listValueBuilder = ListValue.newBuilder();

    VCFHeaderLineType type = metaData.getType();
    if (!(currObject instanceof List)) {
      toReturn.put(currKey,
          listValueBuilder.addValues(createTypedValue(type, currObject)).build());
      continue;
    }

    List<Object> currObjectList = (List<Object>) currObject;
    for (Object currObj : currObjectList) {
      listValueBuilder.addValues(createTypedValue(type, currObj));
    }
    toReturn.put(currKey, listValueBuilder.build());
  }

  return toReturn;
}
 
Example 4
Source File: GenomeWarpTestUtils.java    From genomewarp with Apache License 2.0 5 votes vote down vote up
private static ListValue listFromObject(Object obj) {
  ListValue.Builder lvBuilder = ListValue.newBuilder();
  if (!(obj instanceof List)) {
    lvBuilder.addValues(valueFromObject(obj));
  } else {
    List<Object> objList = (List<Object>) obj;

    for (int i = 0; i < objList.size(); i++) {
      lvBuilder.addValues(valueFromObject(objList.get(i)));
    }
  }

  return lvBuilder.build();
}
 
Example 5
Source File: Bootstrapper.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
/**
 * Converts Java representation of the given JSON value to protobuf's {@link
 * com.google.protobuf.Value} representation.
 *
 * <p>The given {@code rawObject} must be a valid JSON value in Java representation, which is
 * either a {@code Map<String, ?>}, {@code List<?>}, {@code String}, {@code Double},
 * {@code Boolean}, or {@code null}.
 */
private static Value convertToValue(Object rawObject) {
  Value.Builder valueBuilder = Value.newBuilder();
  if (rawObject == null) {
    valueBuilder.setNullValue(NullValue.NULL_VALUE);
  } else if (rawObject instanceof Double) {
    valueBuilder.setNumberValue((Double) rawObject);
  } else if (rawObject instanceof String) {
    valueBuilder.setStringValue((String) rawObject);
  } else if (rawObject instanceof Boolean) {
    valueBuilder.setBoolValue((Boolean) rawObject);
  } else if (rawObject instanceof Map) {
    Struct.Builder structBuilder = Struct.newBuilder();
    @SuppressWarnings("unchecked")
    Map<String, ?> map = (Map<String, ?>) rawObject;
    for (Map.Entry<String, ?> entry : map.entrySet()) {
      structBuilder.putFields(entry.getKey(), convertToValue(entry.getValue()));
    }
    valueBuilder.setStructValue(structBuilder);
  } else if (rawObject instanceof List) {
    ListValue.Builder listBuilder = ListValue.newBuilder();
    List<?> list = (List<?>) rawObject;
    for (Object obj : list) {
      listBuilder.addValues(convertToValue(obj));
    }
    valueBuilder.setListValue(listBuilder);
  }
  return valueBuilder.build();
}
 
Example 6
Source File: VcfToVariant.java    From genomewarp with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
static List<VariantCall> getCalls(VariantContext vc, VCFHeader header) {
  List<VariantCall> toReturn = new ArrayList<>();

  for (String currSample : header.getGenotypeSamples()) {
    if (!vc.hasGenotype(currSample)) {
      continue;
    }

    Genotype currGenotype = vc.getGenotype(currSample);
    VariantCall.Builder vcBuilder = VariantCall.newBuilder();
    vcBuilder.setCallSetName(currSample);

    // Get GT info.
    final Map<Allele, Integer> alleleStrings = buildAlleleMap(vc);
    vcBuilder.addGenotype(alleleStrings.get(currGenotype.getAllele(0)));
    for (int i = 1; i < currGenotype.getPloidy(); i++) {
      vcBuilder.addGenotype(alleleStrings.get(currGenotype.getAllele(i)));
    }

    // Set phasing (not applicable to haploid).
    if (currGenotype.isPhased() && currGenotype.getPloidy() > 1) {
      vcBuilder.setPhaseset("*");
    }

    // Get rest of the genotype info.
    Map<String, ListValue> genotypeInfo = new HashMap<>();

    // Set filters
    if (currGenotype.isFiltered()) {
      genotypeInfo.put(VCFConstants.GENOTYPE_FILTER_KEY, ListValue.newBuilder()
          .addValues(Value.newBuilder().setStringValue(currGenotype.getFilters()).build())
          .build());
    }

    for (final String field : vc.calcVCFGenotypeKeys(header)) {
      // We've already handled genotype
      if (field.equals(VCFConstants.GENOTYPE_KEY)) {
        continue;
      }

      ListValue.Builder listValueBuilder = ListValue.newBuilder();
      if (field.equals(VCFConstants.GENOTYPE_FILTER_KEY)) {
        // This field has already been dealt with
        continue;
      } else {
        final IntGenotypeFieldAccessors.Accessor accessor =
            GENOTYPE_FIELD_ACCESSORS.getAccessor(field);

        if (accessor != null) {
          // The field is a default inline field.
          if (!parseInlineGenotypeFields(field, vcBuilder, listValueBuilder, accessor,
              currGenotype)) {
            continue;
          }
        } else {
          // Other field, we'll get type/other info from header.
          if (!parseOtherGenotypeFields(field, vc, listValueBuilder, currGenotype,
              header)) {
            continue;
          }
        }
      }

      genotypeInfo.put(field, listValueBuilder.build());
    }

    vcBuilder.putAllInfo(genotypeInfo);
    toReturn.add(vcBuilder.build());
  }

  return toReturn;
}
 
Example 7
Source File: DatasetServiceImpl.java    From modeldb with Apache License 2.0 4 votes vote down vote up
@Override
public void getLastExperimentByDatasetId(
    LastExperimentByDatasetId request,
    StreamObserver<LastExperimentByDatasetId.Response> responseObserver) {
  QPSCountResource.inc();
  try (RequestLatencyResource latencyResource =
      new RequestLatencyResource(ModelDBAuthInterceptor.METHOD_NAME.get())) {

    if (request.getDatasetId().isEmpty()) {
      ModelDBUtils.logAndThrowError(
          ModelDBMessages.DATASET_ID_NOT_FOUND_IN_REQUEST,
          Code.INVALID_ARGUMENT_VALUE,
          Any.pack(LastExperimentByDatasetId.Response.getDefaultInstance()));
    }

    // Validate if current user has access to the entity or not
    roleService.validateEntityUserWithUserInfo(
        ModelDBServiceResourceTypes.DATASET, request.getDatasetId(), ModelDBServiceActions.READ);

    FindDatasetVersions findDatasetVersions =
        FindDatasetVersions.newBuilder()
            .setDatasetId(request.getDatasetId())
            .setIdsOnly(true)
            .build();
    // Get the user info from the Context
    UserInfo userInfo = authService.getCurrentLoginUserInfo();
    DatasetVersionDTO datasetVersionDTO =
        datasetVersionDAO.findDatasetVersions(datasetDAO, findDatasetVersions, userInfo);
    List<String> datasetVersionIds = new ArrayList<>();
    ListValue.Builder listValueBuilder = ListValue.newBuilder();
    if (datasetVersionDTO != null
        && datasetVersionDTO.getDatasetVersions() != null
        && !datasetVersionDTO.getDatasetVersions().isEmpty()) {
      for (DatasetVersion datasetVersion : datasetVersionDTO.getDatasetVersions()) {
        datasetVersionIds.add(datasetVersion.getId());
        listValueBuilder.addValues(
            Value.newBuilder().setStringValue(datasetVersion.getId()).build());
      }
    }

    Experiment lastUpdatedExperiment = null;
    if (!datasetVersionIds.isEmpty()) {
      KeyValueQuery keyValueQuery =
          KeyValueQuery.newBuilder()
              .setKey(ModelDBConstants.DATASETS + "." + ModelDBConstants.LINKED_ARTIFACT_ID)
              .setValue(Value.newBuilder().setListValue(listValueBuilder.build()).build())
              .setOperator(OperatorEnum.Operator.IN)
              .build();
      FindExperimentRuns findExperimentRuns =
          FindExperimentRuns.newBuilder().addPredicates(keyValueQuery).build();
      ExperimentRunPaginationDTO experimentRunPaginationDTO =
          experimentRunDAO.findExperimentRuns(projectDAO, userInfo, findExperimentRuns);
      if (experimentRunPaginationDTO != null
          && experimentRunPaginationDTO.getExperimentRuns() != null
          && !experimentRunPaginationDTO.getExperimentRuns().isEmpty()) {
        List<ExperimentRun> experimentRuns = experimentRunPaginationDTO.getExperimentRuns();
        List<String> experimentIds = new ArrayList<>();
        for (ExperimentRun experimentRun : experimentRuns) {
          experimentIds.add(experimentRun.getExperimentId());
        }
        FindExperiments findExperiments =
            FindExperiments.newBuilder()
                .addAllExperimentIds(experimentIds)
                .setPageLimit(1)
                .setPageNumber(1)
                .setSortKey(ModelDBConstants.DATE_UPDATED)
                .setAscending(false)
                .build();
        ExperimentPaginationDTO experimentPaginationDTO =
            experimentDAO.findExperiments(projectDAO, userInfo, findExperiments);
        if (experimentPaginationDTO.getExperiments() != null
            && !experimentPaginationDTO.getExperiments().isEmpty()) {
          lastUpdatedExperiment = experimentPaginationDTO.getExperiments().get(0);
        }
      }
    }

    if (lastUpdatedExperiment != null) {
      responseObserver.onNext(
          LastExperimentByDatasetId.Response.newBuilder()
              .setExperiment(lastUpdatedExperiment)
              .build());
    } else {
      responseObserver.onNext(LastExperimentByDatasetId.Response.newBuilder().build());
    }
    responseObserver.onCompleted();

  } catch (Exception e) {
    ModelDBUtils.observeError(
        responseObserver, e, LastExperimentByDatasetId.Response.getDefaultInstance());
  }
}
 
Example 8
Source File: DatasetServiceImpl.java    From modeldb with Apache License 2.0 4 votes vote down vote up
@Override
public void getExperimentRunByDataset(
    GetExperimentRunByDataset request,
    StreamObserver<GetExperimentRunByDataset.Response> responseObserver) {
  QPSCountResource.inc();
  try (RequestLatencyResource latencyResource =
      new RequestLatencyResource(ModelDBAuthInterceptor.METHOD_NAME.get())) {

    if (request.getDatasetId().isEmpty()) {
      ModelDBUtils.logAndThrowError(
          ModelDBMessages.DATASET_ID_NOT_FOUND_IN_REQUEST,
          Code.INVALID_ARGUMENT_VALUE,
          Any.pack(GetExperimentRunByDataset.Response.getDefaultInstance()));
    }

    // Validate if current user has access to the entity or not
    roleService.validateEntityUserWithUserInfo(
        ModelDBServiceResourceTypes.DATASET, request.getDatasetId(), ModelDBServiceActions.READ);

    FindDatasetVersions findDatasetVersions =
        FindDatasetVersions.newBuilder()
            .setDatasetId(request.getDatasetId())
            .setIdsOnly(true)
            .build();
    // Get the user info from the Context
    UserInfo userInfo = authService.getCurrentLoginUserInfo();
    DatasetVersionDTO datasetVersionDTO =
        datasetVersionDAO.findDatasetVersions(datasetDAO, findDatasetVersions, userInfo);
    List<String> datasetVersionIds = new ArrayList<>();
    ListValue.Builder listValueBuilder = ListValue.newBuilder();
    if (datasetVersionDTO != null
        && datasetVersionDTO.getDatasetVersions() != null
        && !datasetVersionDTO.getDatasetVersions().isEmpty()) {
      for (DatasetVersion datasetVersion : datasetVersionDTO.getDatasetVersions()) {
        datasetVersionIds.add(datasetVersion.getId());
        listValueBuilder.addValues(
            Value.newBuilder().setStringValue(datasetVersion.getId()).build());
      }
    }

    List<ExperimentRun> experimentRuns = new ArrayList<>();
    if (!datasetVersionIds.isEmpty()) {
      KeyValueQuery keyValueQuery =
          KeyValueQuery.newBuilder()
              .setKey(ModelDBConstants.DATASETS + "." + ModelDBConstants.LINKED_ARTIFACT_ID)
              .setValue(Value.newBuilder().setListValue(listValueBuilder.build()).build())
              .setOperator(OperatorEnum.Operator.IN)
              .build();
      FindExperimentRuns findExperimentRuns =
          FindExperimentRuns.newBuilder().addPredicates(keyValueQuery).build();
      ExperimentRunPaginationDTO experimentRunPaginationDTO =
          experimentRunDAO.findExperimentRuns(projectDAO, userInfo, findExperimentRuns);
      if (experimentRunPaginationDTO != null
          && experimentRunPaginationDTO.getExperimentRuns() != null
          && !experimentRunPaginationDTO.getExperimentRuns().isEmpty()) {
        experimentRuns.addAll(experimentRunPaginationDTO.getExperimentRuns());
      }
    }

    responseObserver.onNext(
        GetExperimentRunByDataset.Response.newBuilder()
            .addAllExperimentRuns(experimentRuns)
            .build());
    responseObserver.onCompleted();

  } catch (Exception e) {
    ModelDBUtils.observeError(
        responseObserver, e, GetExperimentRunByDataset.Response.getDefaultInstance());
  }
}