Skip to content

Commit c518f25

Browse files
Add Values.of(Map<String, Value> values).
Also update Javadoc to include {@link} syntax and fix typo. PiperOrigin-RevId: 806318745
1 parent d53c7aa commit c518f25

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

java/util/src/main/java/com/google/protobuf/util/Values.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.google.protobuf.NullValue;
1212
import com.google.protobuf.Struct;
1313
import com.google.protobuf.Value;
14+
import java.util.Map;
1415

1516
/** Utilities to help create {@code google.protobuf.Value} messages. */
1617
public final class Values {
@@ -25,38 +26,46 @@ public static Value ofNull() {
2526
return NULL_VALUE;
2627
}
2728

28-
/** Returns a Value object with number set to value. */
29+
/** Returns a {@link Value} object with number set to value. */
2930
public static Value of(boolean value) {
3031
return value ? TRUE_VALUE : FALSE_VALUE;
3132
}
3233

33-
/** Returns a Value object with number set to value. */
34+
/** Returns a {@link Value} object with number set to value. */
3435
public static Value of(double value) {
3536
return Value.newBuilder().setNumberValue(value).build();
3637
}
3738

38-
/** Returns a Value object with string set to value. */
39+
/** Returns a {@link Value} object with string set to value. */
3940
public static Value of(String value) {
4041
return value.isEmpty() ? EMPTY_STR_VALUE : Value.newBuilder().setStringValue(value).build();
4142
}
4243

43-
/** Returns a Value object with struct set to value. */
44+
/** Returns a {@link Value} object with struct set to value. */
4445
public static Value of(Struct value) {
4546
return Value.newBuilder().setStructValue(value).build();
4647
}
4748

48-
/** Returns a Value with ListValue set to value. */
49+
/** Returns a {@link Value} with a {@link ListValue} set to value. */
4950
public static Value of(ListValue value) {
5051
return Value.newBuilder().setListValue(value).build();
5152
}
5253

5354
/**
54-
* Returns a Value with ListValue set to the appending the result of calling {@link #of} on each
55-
* element in the iterable.
55+
* Returns a {@link Value} with a {@link ListValue} whose values are set to the result of calling
56+
* {@link #of} on each element in the iterable.
5657
*/
5758
public static Value of(Iterable<Value> values) {
5859
return Value.newBuilder().setListValue(ListValue.newBuilder().addAllValues(values)).build();
5960
}
6061

62+
/**
63+
* Returns a {@link Value} with a {@link Struct} whose fields are set to the result of calling
64+
* {@link #of} on each value in the map, preserving keys.
65+
*/
66+
public static Value of(Map<String, Value> values) {
67+
return Value.newBuilder().setStructValue(Struct.newBuilder().putAllFields(values)).build();
68+
}
69+
6170
private Values() {}
6271
}

java/util/src/test/java/com/google/protobuf/util/ValuesTest.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,43 @@
1414
import com.google.protobuf.Struct;
1515
import com.google.protobuf.Value;
1616
import java.util.ArrayList;
17+
import java.util.HashMap;
1718
import java.util.List;
19+
import java.util.Map;
1820
import org.junit.Test;
1921
import org.junit.runner.RunWith;
2022
import org.junit.runners.JUnit4;
2123

2224
@RunWith(JUnit4.class)
2325
public final class ValuesTest {
2426
@Test
25-
public void testOfNull_IsNullValue() throws Exception {
27+
public void testOfNull_isNullValue() throws Exception {
2628
assertThat(Values.ofNull())
2729
.isEqualTo(Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build());
2830
}
2931

3032
@Test
31-
public void testOfBoolean_ConstructsValue() {
33+
public void testOfBoolean_constructsValue() {
3234
assertThat(Values.of(true)).isEqualTo(Value.newBuilder().setBoolValue(true).build());
3335
assertThat(Values.of(false)).isEqualTo(Value.newBuilder().setBoolValue(false).build());
3436
}
3537

3638
@Test
37-
public void testOfNumeric_ConstructsValue() {
39+
public void testOfNumeric_constructsValue() {
3840
assertThat(Values.of(100)).isEqualTo(Value.newBuilder().setNumberValue(100).build());
3941
assertThat(Values.of(1000L)).isEqualTo(Value.newBuilder().setNumberValue(1000).build());
4042
assertThat(Values.of(1000.23f)).isEqualTo(Value.newBuilder().setNumberValue(1000.23f).build());
4143
assertThat(Values.of(10000.23)).isEqualTo(Value.newBuilder().setNumberValue(10000.23).build());
4244
}
4345

4446
@Test
45-
public void testOfString_ConstructsValue() {
47+
public void testOfString_constructsValue() {
4648
assertThat(Values.of("")).isEqualTo(Value.newBuilder().setStringValue("").build());
4749
assertThat(Values.of("foo")).isEqualTo(Value.newBuilder().setStringValue("foo").build());
4850
}
4951

5052
@Test
51-
public void testOfStruct_ConstructsValue() {
53+
public void testOfStruct_constructsValue() {
5254
Struct.Builder builder = Struct.newBuilder();
5355
builder.putFields("a", Values.of("a"));
5456
builder.putFields("b", Values.of("b"));
@@ -58,7 +60,7 @@ public void testOfStruct_ConstructsValue() {
5860
}
5961

6062
@Test
61-
public void testOfListValue_ConstructsInstance() {
63+
public void testOfListValue_constructsInstance() {
6264
ListValue.Builder builder = ListValue.newBuilder();
6365
builder.addValues(Values.of(1));
6466
builder.addValues(Values.of(2));
@@ -68,7 +70,7 @@ public void testOfListValue_ConstructsInstance() {
6870
}
6971

7072
@Test
71-
public void testOfIterable_ReturnsTheValue() {
73+
public void testOfIterable_returnsTheValue() {
7274
ListValue.Builder builder = ListValue.newBuilder();
7375
builder.addValues(Values.of(1));
7476
builder.addValues(Values.of(2));
@@ -86,4 +88,24 @@ public void testOfIterable_ReturnsTheValue() {
8688
assertThat(Values.of(new ArrayList<Value>()))
8789
.isEqualTo(Value.newBuilder().setListValue(ListValue.getDefaultInstance()).build());
8890
}
91+
92+
@Test
93+
public void testOfMap_returnsTheValue() {
94+
Struct.Builder builder = Struct.newBuilder();
95+
builder.putFields("a", Values.of(1));
96+
builder.putFields("b", Values.of(2));
97+
builder.putFields("c", Values.of(true));
98+
builder.putFields("d", Value.newBuilder().setStructValue(builder).build());
99+
100+
Map<String, Value> map = new HashMap<>();
101+
map.put("a", Values.of(1));
102+
map.put("b", Values.of(2));
103+
map.put("c", Values.of(true));
104+
Map<String, Value> copyMap = new HashMap<>(map);
105+
map.put("d", Values.of(copyMap));
106+
107+
assertThat(Values.of(map)).isEqualTo(Value.newBuilder().setStructValue(builder).build());
108+
assertThat(Values.of(new HashMap<String, Value>()))
109+
.isEqualTo(Value.newBuilder().setStructValue(Struct.getDefaultInstance()).build());
110+
}
89111
}

0 commit comments

Comments
 (0)