Java Code Examples for com.google.protobuf.ListValue#Builder
The following examples show how to use
com.google.protobuf.ListValue#Builder .
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: WellKnownTypeMarshaller.java From curiostack with MIT License | 6 votes |
@Override public void doMerge(JsonParser parser, int currentDepth, Message.Builder messageBuilder) throws IOException { Value.Builder builder = (Value.Builder) messageBuilder; JsonToken token = parser.currentToken(); if (token.isBoolean()) { builder.setBoolValue(ParseSupport.parseBool(parser)); } else if (token.isNumeric()) { builder.setNumberValue(ParseSupport.parseDouble(parser)); } else if (token == JsonToken.VALUE_NULL) { builder.setNullValue(NullValue.NULL_VALUE); } else if (token.isScalarValue()) { builder.setStringValue(ParseSupport.parseString(parser)); } else if (token == JsonToken.START_OBJECT) { Struct.Builder structBuilder = builder.getStructValueBuilder(); StructMarshaller.INSTANCE.mergeValue(parser, currentDepth + 1, structBuilder); } else if (token == JsonToken.START_ARRAY) { ListValue.Builder listValueBuilder = builder.getListValueBuilder(); ListValueMarshaller.INSTANCE.mergeValue(parser, currentDepth + 1, listValueBuilder); } else { throw new IllegalStateException("Unexpected json data: " + parser.getText()); } }
Example 2
Source File: VcfToVariant.java From genomewarp with Apache License 2.0 | 6 votes |
private static boolean parseInlineGenotypeFields(String field, VariantCall.Builder vcBuilder, ListValue.Builder lvBuilder, IntGenotypeFieldAccessors.Accessor accessor, Genotype g) { final int[] intValues = accessor.getValues(g); if (intValues == null || intValues.length == 0) { return false; } if (field.equals(VCFConstants.GENOTYPE_PL_KEY)) { // HTSJDK folds GL's into PL's. We only use PL's to store genotype likelihood. for (int i = 0; i < intValues.length; i++) { // We add 0.0 to remove the possiblity of getting -0.0. vcBuilder.addGenotypeLikelihood(-(double) intValues[i] / 10.0 + 0.0); } return false; } for (int i = 0; i < intValues.length; i++) { lvBuilder.addValues(Value.newBuilder().setNumberValue(intValues[i])); } return true; }
Example 3
Source File: WellKnownTypeMarshaller.java From curiostack with MIT License | 5 votes |
@Override public void doMerge(JsonParser parser, int currentDepth, Message.Builder messageBuilder) throws IOException { JsonToken token = parser.currentToken(); if (token != JsonToken.START_ARRAY) { throw new InvalidProtocolBufferException("Expect an array but found: " + parser.getText()); } ListValue.Builder builder = (ListValue.Builder) messageBuilder; while (parser.nextValue() != JsonToken.END_ARRAY) { Value.Builder valueBuilder = builder.addValuesBuilder(); ValueMarshaller.INSTANCE.mergeValue(parser, currentDepth + 1, valueBuilder); } }
Example 4
Source File: MessageMarshallerTest.java From curiostack with MIT License | 5 votes |
@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 5
Source File: VcfToVariant.java From genomewarp with Apache License 2.0 | 5 votes |
@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 6
Source File: GenomeWarpTestUtils.java From genomewarp with Apache License 2.0 | 5 votes |
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 7
Source File: Bootstrapper.java From grpc-java with Apache License 2.0 | 5 votes |
/** * 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 8
Source File: ListValueDeserializer.java From jackson-datatype-protobuf with Apache License 2.0 | 5 votes |
@Override protected void populate( ListValue.Builder builder, JsonParser parser, DeserializationContext context ) throws IOException { List<Object> values = readArray(builder, VALUES_FIELD, null, parser, context); for (Object value : values) { builder.addRepeatedField(VALUES_FIELD, value); } }
Example 9
Source File: VcfToVariant.java From genomewarp with Apache License 2.0 | 4 votes |
private static boolean parseOtherGenotypeFields(String field, VariantContext vc, ListValue.Builder lvBuilder, Genotype g, VCFHeader header) { if (!g.hasAnyAttribute(field)) { return false; } final VCFFormatHeaderLine metaData = header.getFormatHeaderLine(field); if (metaData == null) { logger.log(Level.WARNING, String.format("Could not find matching VCF header field for " + "genotype field %s", field)); return false; } VCFHeaderLineType type = metaData.getType(); Object value = g.getExtendedAttribute(field); final int fieldCount = metaData.getCount(vc); if (fieldCount == 1) { lvBuilder.addValues(createTypedValue(type, value)); return true; } if (!(value instanceof String)) { throw new IllegalStateException("received non-Flag genotype field as non-String type"); } String[] valueArray = ((String) value).split(","); if (valueArray.length == 1) { throw new IllegalStateException(String.format("header indicating a count greater than 1 " + "with non-List type found for field %s", field)); } boolean allFalse = true; for (int i = 0; i < valueArray.length; i++) { VCFHeaderLineType thisType = VCFHeaderLineType.String; if (!valueArray[i].equals(VCFConstants.MISSING_VALUE_v4)) { thisType = type; allFalse = false; } lvBuilder.addValues(createTypedValue(thisType, valueArray[i])); } // We only add the lvBuilder if there is at least one non-missing value return !allFalse; }
Example 10
Source File: VcfToVariant.java From genomewarp with Apache License 2.0 | 4 votes |
@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 11
Source File: DatasetServiceImpl.java From modeldb with Apache License 2.0 | 4 votes |
@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 12
Source File: DatasetServiceImpl.java From modeldb with Apache License 2.0 | 4 votes |
@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()); } }