本文整理汇总了Java中com.google.protobuf.Value类的典型用法代码示例。如果您正苦于以下问题:Java Value类的具体用法?Java Value怎么用?Java Value使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Value类属于com.google.protobuf包,在下文中一共展示了Value类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doMerge
import com.google.protobuf.Value; //导入依赖的package包/类
@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());
}
}
示例2: doWrite
import com.google.protobuf.Value; //导入依赖的package包/类
@Override
public void doWrite(Value message, JsonGenerator gen) throws IOException {
switch (message.getKindCase()) {
case NULL_VALUE:
SerializeSupport.printNull(0, gen);
break;
case NUMBER_VALUE:
SerializeSupport.printDouble(message.getNumberValue(), gen);
break;
case STRING_VALUE:
SerializeSupport.printString(message.getStringValue(), gen);
break;
case BOOL_VALUE:
SerializeSupport.printBool(message.getBoolValue(), gen);
break;
case STRUCT_VALUE:
StructMarshaller.INSTANCE.writeValue(message.getStructValue(), gen);
break;
case LIST_VALUE:
ListValueMarshaller.INSTANCE.writeValue(message.getListValue(), gen);
break;
case KIND_NOT_SET:
SerializeSupport.printNull(0, gen);
break;
}
}
示例3: anyInMaps
import com.google.protobuf.Value; //导入依赖的package包/类
@Test
public void anyInMaps() throws Exception {
TestAny.Builder testAny = TestAny.newBuilder();
testAny.putAnyMap("int32_wrapper", Any.pack(Int32Value.newBuilder().setValue(123).build()));
testAny.putAnyMap("int64_wrapper", Any.pack(Int64Value.newBuilder().setValue(456).build()));
testAny.putAnyMap("timestamp", Any.pack(Timestamps.parse("1969-12-31T23:59:59Z")));
testAny.putAnyMap("duration", Any.pack(Durations.parse("12345.1s")));
testAny.putAnyMap("field_mask", Any.pack(FieldMaskUtil.fromString("foo.bar,baz")));
Value numberValue = Value.newBuilder().setNumberValue(1.125).build();
Struct.Builder struct = Struct.newBuilder();
struct.putFields("number", numberValue);
testAny.putAnyMap("struct", Any.pack(struct.build()));
Value nullValue = Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
testAny.putAnyMap(
"list_value",
Any.pack(ListValue.newBuilder().addValues(numberValue).addValues(nullValue).build()));
testAny.putAnyMap("number_value", Any.pack(numberValue));
testAny.putAnyMap("any_value_number", Any.pack(Any.pack(numberValue)));
testAny.putAnyMap("any_value_default", Any.pack(Any.getDefaultInstance()));
testAny.putAnyMap("default", Any.getDefaultInstance());
assertMatchesUpstream(testAny.build(), TestAllTypes.getDefaultInstance());
}
示例4: newTestLogEntry
import com.google.protobuf.Value; //导入依赖的package包/类
private static LogEntry.Builder newTestLogEntry(String name, int responseCode) {
Value.Builder vb = Value.newBuilder();
Map<String, Value> values = Maps.newHashMap();
values.put("api_key", vb.setStringValue(TEST_API_KEY).build());
values.put("http_method", vb.setStringValue("GET").build());
values.put("timestamp",
vb.setNumberValue(TEST_CLOCK.currentTimeMillis()).build());
values.put("http_response_code", vb.setNumberValue(responseCode).build());
values.put("response_size", vb.setNumberValue(TEST_SIZE).build());
values.put("request_size", vb.setNumberValue(TEST_SIZE).build());
if (responseCode >= 400) {
values.put("error_cause", vb.setStringValue("internal").build());
}
return LogEntry
.newBuilder()
.setStructPayload(Struct.newBuilder().putAllFields(values))
.setName(name)
.setSeverity(responseCode >= 400 ? LogSeverity.ERROR : LogSeverity.INFO)
.setTimestamp(REALLY_EARLY);
}
示例5: createTypedValue
import com.google.protobuf.Value; //导入依赖的package包/类
private static Value createTypedValue(VCFHeaderLineType type, Object value) {
if (type == VCFHeaderLineType.Flag) {
return Value.newBuilder().setBoolValue((Boolean) value).build();
}
// Booleans are given as Boolean objects. Strangely, Floats and Integers
// are given as String objects by HTSJDK.
if (!(value instanceof String)) {
throw new IllegalStateException("Received non-Boolean, non-List type in non-String format. "
+ "This is most likely due to a change in htsjdk's library.");
}
String stringValue = (String) value;
boolean isNumeric = stringValue.matches("[-+]?\\d+(\\.\\d+)?");
if (type == VCFHeaderLineType.Integer && isNumeric) {
return Value.newBuilder().setNumberValue(Integer.parseInt(stringValue)).build();
}
if (type == VCFHeaderLineType.Float && isNumeric) {
return Value.newBuilder().setNumberValue(Double.parseDouble(stringValue)).build();
}
return Value.newBuilder().setStringValue(stringValue).build();
}
示例6: parseInlineGenotypeFields
import com.google.protobuf.Value; //导入依赖的package包/类
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;
}
示例7: testGetAlleleFreq
import com.google.protobuf.Value; //导入依赖的package包/类
@Test
public void testGetAlleleFreq() throws Exception {
DoFnTester<Variant, KV<Position, AlleleFreq>> getAlleleFreq = DoFnTester.of(
new GetAlleleFreq());
Position pos = Position.newBuilder()
.setReferenceName("1")
.setPosition(123L)
.build();
Variant.Builder vBuild = Variant.newBuilder()
.setReferenceName("1")
.setStart(123L)
.setReferenceBases("C")
.addAlternateBases("T");
vBuild.getMutableInfo().put("AF", ListValue.newBuilder()
.addValues(Value.newBuilder().setStringValue("0.25").build()).build());
AlleleFreq af = new AlleleFreq();
af.setAltBases(Lists.newArrayList("T"));
af.setRefBases("C");
af.setRefFreq(0.25);
Assert.assertThat(getAlleleFreq.processBundle(vBuild.build()),
CoreMatchers.hasItems(KV.of(pos, af)));
}
示例8: onNext
import com.google.protobuf.Value; //导入依赖的package包/类
@Override
public void onNext(T value) {
List<Value> path =
dataFetchingEnvironment
.getFieldTypeInfo()
.getPath()
.toList()
.stream()
.map(
p ->
p instanceof Number
? Value.newBuilder()
.setNumberValue(Double.parseDouble(p.toString()))
.build()
: Value.newBuilder().setStringValue(p.toString()).build())
.collect(ImmutableList.toImmutableList());
ListValue pathListVale =
ListValue.newBuilder()
.addAllValues(path)
.addValues(Value.newBuilder().setNumberValue(pathIndex.incrementAndGet()))
.build();
R graphQlResponse = getData(value, pathListVale);
rejoinerStreamingContext.responseStreamObserver().onNext(graphQlResponse);
try {
System.out.println(
"Streaming response as Json: " + JsonFormat.printer().print(graphQlResponse));
} catch (InvalidProtocolBufferException e) {
throw new RuntimeException(e);
}
}
示例9: struct
import com.google.protobuf.Value; //导入依赖的package包/类
@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);
}
示例10: tensorToNDArray
import com.google.protobuf.Value; //导入依赖的package包/类
public static DefaultData tensorToNDArray(DefaultData data){
if (data.getDataOneofCase() == DataOneofCase.TENSOR)
{
List<Double> valuesList = data.getTensor().getValuesList();
List<Integer> shapeList = data.getTensor().getShapeList();
DefaultData.Builder dataBuilder = DefaultData.newBuilder();
int index=0;
for (Iterator<String> i = data.getNamesList().iterator(); i.hasNext();){
dataBuilder.setNames(index, i.next());
index++;
}
ListValue.Builder b1 = ListValue.newBuilder();
for (int i = 0; i < shapeList.get(0); ++i) {
ListValue.Builder b2 = ListValue.newBuilder();
for (int j = 0; j < shapeList.get(1); j++){
b2.addValues(Value.newBuilder().setNumberValue(valuesList.get(i*shapeList.get(1))+j));
}
b1.addValues(Value.newBuilder().setListValue(b2.build()));
}
dataBuilder.setNdarray(b1.build());
return dataBuilder.build();
}
else if (data.getDataOneofCase() == DataOneofCase.NDARRAY)
{
return data;
}
return null;
}
示例11: updateData
import com.google.protobuf.Value; //导入依赖的package包/类
public static DefaultData updateData(DefaultData oldData, INDArray newData){
DefaultData.Builder dataBuilder = DefaultData.newBuilder();
dataBuilder.addAllNames(oldData.getNamesList());
// int index=0;
// for (Iterator<String> i = oldData.getFeaturesList().iterator(); i.hasNext();){
// dataBuilder.setFeatures(index, i.next());
// index++;
// }
if (oldData.getDataOneofCase() == DataOneofCase.TENSOR){
Tensor.Builder tBuilder = Tensor.newBuilder();
List<Integer> shapeList = Arrays.stream(newData.shape()).boxed().collect(Collectors.toList());
tBuilder.addAllShape(shapeList);
for (int i=0; i<shapeList.get(0); ++i){
for (int j=0; j<shapeList.get(1); ++j){
tBuilder.addValues(newData.getDouble(i,j));
}
}
dataBuilder.setTensor(tBuilder);
return dataBuilder.build();
}
else if (oldData.getDataOneofCase() == DataOneofCase.NDARRAY){
ListValue.Builder b1 = ListValue.newBuilder();
for (int i = 0; i < newData.shape()[0]; ++i) {
ListValue.Builder b2 = ListValue.newBuilder();
for (int j = 0; j < newData.shape()[1]; j++){
b2.addValues(Value.newBuilder().setNumberValue(newData.getDouble(i,j)));
}
b1.addValues(Value.newBuilder().setListValue(b2.build()));
}
dataBuilder.setNdarray(b1.build());
return dataBuilder.build();
}
return null;
}
示例12: defaultRequest
import com.google.protobuf.Value; //导入依赖的package包/类
@Test
public void defaultRequest() throws InvalidProtocolBufferException
{
String[] features = {"a","b"};
Double[] values = {1.0,2.0,1.5,2.2};
DefaultData.Builder defB = DefaultData.newBuilder();
defB.addAllNames( Arrays.asList(features) );
defB.setTensor(Tensor.newBuilder().addShape(1).addShape(values.length).addAllValues(Arrays.asList(values)).build());
SeldonMessage.Builder b = SeldonMessage.newBuilder();
Value v;
Value v1 = Value.newBuilder().setNumberValue(1.0).build();
b.setData(defB.build()).setMeta(Meta.newBuilder().putTags("key", v1).build());
SeldonMessage request = b.build();
String json = ProtoBufUtils.toJson(request);
System.out.println(json);
SeldonMessage.Builder b2 = SeldonMessage.newBuilder();
ProtoBufUtils.updateMessageBuilderFromJson(b2, json);
SeldonMessage request2 = b2.build();
String json2 = ProtoBufUtils.toJson(request2);
System.out.println(json2);
Assert.assertEquals(json, json2);
}
示例13: testSimpleNDArrayCase
import com.google.protobuf.Value; //导入依赖的package包/类
@Test
public void testSimpleNDArrayCase() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException {
List<SeldonMessage> predictorReturns = new ArrayList<>();
String[] names = {"c","d"};
Double[] values1 = {1.0,1.0};
predictorReturns.add(SeldonMessage.newBuilder().setStatus(Status.newBuilder().setStatus(Status.StatusFlag.SUCCESS).build())
.setData(DefaultData.newBuilder().addAllNames(Arrays.asList(names))
.setNdarray(ListValue.newBuilder().addValues(Value.newBuilder().setListValue(ListValue.newBuilder()
.addValues(Value.newBuilder().setNumberValue(values1[0]))
.addValues(Value.newBuilder().setNumberValue(values1[1])).build())).build()).build()).build());
Double[] values2 = {1.0,0.5};
predictorReturns.add(SeldonMessage.newBuilder().setStatus(Status.newBuilder().setStatus(Status.StatusFlag.SUCCESS).build())
.setData(DefaultData.newBuilder().addAllNames(Arrays.asList(names))
.setNdarray(ListValue.newBuilder().addValues(Value.newBuilder().setListValue(ListValue.newBuilder()
.addValues(Value.newBuilder().setNumberValue(values2[0]))
.addValues(Value.newBuilder().setNumberValue(values2[1])).build())).build()).build()).build());
Double[] values3 = {2.2,0.9};
predictorReturns.add(SeldonMessage.newBuilder().setStatus(Status.newBuilder().setStatus(Status.StatusFlag.SUCCESS).build())
.setData(DefaultData.newBuilder().addAllNames(Arrays.asList(names))
.setNdarray(ListValue.newBuilder().addValues(Value.newBuilder().setListValue(ListValue.newBuilder()
.addValues(Value.newBuilder().setNumberValue(values3[0]))
.addValues(Value.newBuilder().setNumberValue(values3[1])).build())).build()).build()).build());
AverageCombinerUnit averageCombinerUnit = new AverageCombinerUnit();
SeldonMessage average = averageCombinerUnit.aggregate(predictorReturns, null);
Assert.assertThat(average.getData().getNamesList().get(0),is(names[0]));
Double[][] expected_values = {{(1.0+1.0+2.2)/3,(1.0+0.5+0.9)/3}};
Assert.assertEquals(expected_values[0][0],average.getData().getNdarray().getValues(0).getListValue().getValues(0).getNumberValue(),1e-7);
}
示例14: createAdditionalServiceTypes
import com.google.protobuf.Value; //导入依赖的package包/类
/**
* Creates additional types (Value, Struct and ListValue) to be added to the Service config.
* TODO (guptasu): Fix this hack. Find a better way to add the predefined types.
* TODO (guptasu): Add them only when required and not in all cases.
*/
static Iterable<Type> createAdditionalServiceTypes() {
Map<String, DescriptorProto> additionalMessages = Maps.newHashMap();
additionalMessages.put(Struct.getDescriptor().getFullName(),
Struct.getDescriptor().toProto());
additionalMessages.put(Value.getDescriptor().getFullName(),
Value.getDescriptor().toProto());
additionalMessages.put(ListValue.getDescriptor().getFullName(),
ListValue.getDescriptor().toProto());
additionalMessages.put(Empty.getDescriptor().getFullName(),
Empty.getDescriptor().toProto());
additionalMessages.put(Int32Value.getDescriptor().getFullName(),
Int32Value.getDescriptor().toProto());
additionalMessages.put(DoubleValue.getDescriptor().getFullName(),
DoubleValue.getDescriptor().toProto());
additionalMessages.put(BoolValue.getDescriptor().getFullName(),
BoolValue.getDescriptor().toProto());
additionalMessages.put(StringValue.getDescriptor().getFullName(),
StringValue.getDescriptor().toProto());
for (Descriptor descriptor : Struct.getDescriptor().getNestedTypes()) {
additionalMessages.put(descriptor.getFullName(), descriptor.toProto());
}
// TODO (guptasu): Remove this hard coding. Without this, creation of Model from Service throws.
// Needs investigation.
String fileName = "struct.proto";
List<Type> additionalTypes = Lists.newArrayList();
for (String typeName : additionalMessages.keySet()) {
additionalTypes.add(TypesBuilderFromDescriptor.createType(typeName,
additionalMessages.get(typeName), fileName));
}
return additionalTypes;
}
示例15: getKind
import com.google.protobuf.Value; //导入依赖的package包/类
private static String getKind(Value.KindCase kind) {
switch (kind) {
case NULL_VALUE: return "NULL_VALUE";
case NUMBER_VALUE: return "NUMBER_VALUE";
case STRING_VALUE: return "STRING_VALUE";
case BOOL_VALUE: return "BOOL_VALUE";
case STRUCT_VALUE: return "STRUCT_VALUE";
case LIST_VALUE: return "LIST_VALUE";
default: return "KIND_NOT_SET";
}
}