Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitce883f4

Browse files
author
Stephen Cobbe
committed
Added Map type.
1 parent95f0db6 commitce883f4

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

‎generator/java.stoneg.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
is_bytes_type,
3232
is_composite_type,
3333
is_list_type,
34+
is_map_type,
3435
is_nullable_type,
3536
is_numeric_type,
3637
is_primitive_type,
@@ -294,12 +295,14 @@ def add_subtype(data_type):
294295
returnresult
295296

296297

297-
defget_underlying_type(data_type,allow_lists=True):
298+
defget_underlying_type(data_type,allow_data_structures=True):
298299
assertisinstance(data_type,DataType),repr(data_type)
299300

300301
whileTrue:
301-
ifallow_listsandis_list_type(data_type):
302+
ifallow_data_structuresandis_list_type(data_type):
302303
data_type=data_type.data_type
304+
elifallow_data_structuresandis_map_type(data_type):
305+
data_type=data_type.value_data_type
303306
elifis_nullable_type(data_type):
304307
data_type=data_type.data_type
305308
else:
@@ -744,6 +747,7 @@ def add_imports_for_route(self, route):
744747
'com.dropbox.core.v2.DbxDownloadStyleBuilder',
745748
'java.util.Collections',
746749
'java.util.List',
750+
'java.util.Map',
747751
)
748752

749753
defadd_imports_for_route_builder(self,route):
@@ -834,6 +838,8 @@ def _add_imports_for_data_type(self, data_type):
834838

835839
ifis_list_type(data_type)oris_nullable_type(data_type):
836840
self._add_imports_for_data_type(data_type.data_type)
841+
elifis_map_type(data_type):
842+
self._add_imports_for_data_type(data_type.value_data_type)
837843
elifis_timestamp_type(data_type):
838844
self.add_imports('com.dropbox.core.util.LangUtil')
839845

@@ -1088,6 +1094,9 @@ def java_serializer(self, data_type):
10881094
# TODO: also support passing collapsed to list serializer
10891095
returnself.fmt('%s.list(%s)',
10901096
serializers_class,self.java_serializer(data_type.data_type))
1097+
elifis_map_type(data_type):
1098+
returnself.fmt('%s.map(%s)',
1099+
serializers_class,self.java_serializer(data_type.value_data_type))
10911100
else:
10921101
returnself.fmt('%s.%s()',serializers_class,camelcase(data_type.name))
10931102

@@ -1350,7 +1359,7 @@ def add_req(precondition, failure_reason):
13501359
ifvalisnotNone:
13511360
add_req(precondition%val,failure_reason%val)
13521361

1353-
ifis_list_type(data_type):
1362+
ifis_list_type(data_type)oris_map_type(data_type):
13541363
add_req('not contain a {@code null} item','contains a {@code null} item')
13551364
elifis_string_type(data_type)anddata_type.patternisnotNone:
13561365
pattern=sanitize_pattern(data_type.pattern)
@@ -1714,7 +1723,7 @@ def get_spec_filenames(self, element):
17141723
@staticmethod
17151724
defrequires_validation(data_type):
17161725
assertisinstance(data_type,DataType),repr(data_type)
1717-
ifis_list_type(data_type):
1726+
ifis_list_type(data_type)oris_map_type(data_type):
17181727
returnTrue
17191728
elifis_numeric_type(data_type):
17201729
returnany(risnotNoneforrin (
@@ -1947,6 +1956,9 @@ def java_class(self, stone_elem, boxed=False, generics=True):
19471956
generic_classes= []
19481957
ifgenericsandis_list_type(data_type):
19491958
generic_classes= [self.java_class(data_type.data_type,boxed=True,generics=True)]
1959+
elifgenericsandis_map_type(data_type):
1960+
generic_classes= [self.java_class(data_type.key_data_type,boxed=True),self.java_class(
1961+
data_type.value_data_type,boxed=True,generics=True)]
19501962

19511963
type_map=_TYPE_MAP_BOXEDifboxedelse_TYPE_MAP_UNBOXED
19521964
returnJavaClass(type_map[data_type.name],generics=generic_classes)
@@ -4077,6 +4089,14 @@ def generate_data_type_validation(self, data_type, value_name, description=None,
40774089
w.out('throw new IllegalArgumentException("An item in list%s is null");',description)
40784090
self.generate_data_type_validation(data_type.data_type,xn,'an item in list%s'%description,level=level+1)
40794091

4092+
elifis_map_type(data_type):
4093+
xn='x'iflevel==0else'x%d'%level
4094+
map_item_type=j.java_class(data_type.value_data_type,boxed=True,generics=True)
4095+
withw.block('for (%s %s : %s.values())',map_item_type,xn,value_name):
4096+
withw.block('if (%s == null)',xn):
4097+
w.out('throw new IllegalArgumentException("An item in map%s is null");',description)
4098+
self.generate_data_type_validation(data_type.value_data_type,xn,'an item in map%s'%description,level=level+1)
4099+
40804100
elifis_numeric_type(data_type):
40814101
ifdata_type.min_valueisnotNone:
40824102
java_value=w.java_value(data_type,data_type.min_value)
@@ -4328,6 +4348,7 @@ def generate_struct_equals(self, data_type):
43284348
'Timestamp':'java.util.Date',
43294349
'Void':'void',
43304350
'List':'java.util.List',
4351+
'Map':'java.util.Map',
43314352
}
43324353

43334354

@@ -4344,6 +4365,7 @@ def generate_struct_equals(self, data_type):
43444365
'Timestamp':'java.util.Date',
43454366
'Void':'Void',
43464367
'List':'java.util.List',
4368+
'Map':'java.util.Map',
43474369
}
43484370

43494371
_CATCH_ALL_DOC="""

‎src/main/java/com/dropbox/core/stone/StoneSerializers.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
importjava.text.ParseException;
55
importjava.util.ArrayList;
66
importjava.util.Date;
7+
importjava.util.HashMap;
78
importjava.util.List;
9+
importjava.util.Map;
810

911
importcom.fasterxml.jackson.core.JsonGenerationException;
1012
importcom.fasterxml.jackson.core.JsonGenerator;
@@ -70,6 +72,9 @@ public static <T> StoneSerializer<List<T>> list(StoneSerializer<T> underlying) {
7072
returnnewListSerializer<T>(underlying);
7173
}
7274

75+
publicstatic <T>StoneSerializer<Map<String,T>>map(StoneSerializer<T>underlying) {
76+
returnnewMapSerializer<T>(underlying);
77+
}
7378

7479
privatestaticfinalclassLongSerializerextendsStoneSerializer<Long> {
7580
publicstaticfinalLongSerializerINSTANCE =newLongSerializer();
@@ -319,4 +324,33 @@ public List<T> deserialize(JsonParser p) throws IOException, JsonParseException
319324
returnlist;
320325
}
321326
}
327+
328+
privatestaticfinalclassMapSerializer<T>extendsStoneSerializer<Map<String,T>> {
329+
privatefinalStoneSerializer<T>underlying;
330+
331+
publicMapSerializer(StoneSerializer<T>underlying) {
332+
this.underlying =underlying;
333+
}
334+
335+
@Override
336+
publicvoidserialize(Map<String,T>value,JsonGeneratorg)throwsIOException,JsonGenerationException {
337+
g.writeString(value.toString());
338+
}
339+
340+
@Override
341+
publicMap<String,T>deserialize(JsonParserp)throwsIOException,JsonParseException {
342+
Map<String,T>map =newHashMap<String,T>();
343+
344+
expectStartObject(p);
345+
while (p.getCurrentToken() ==JsonToken.FIELD_NAME) {
346+
Stringkey =p.getCurrentName();
347+
p.nextToken();
348+
Tval =underlying.deserialize(p);
349+
map.put(key,val);
350+
}
351+
expectEndObject(p);
352+
353+
returnmap;
354+
}
355+
}
322356
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp