@@ -305,12 +305,15 @@ class StructField(DataType):
305
305
306
306
"""
307
307
308
- def __init__ (self , name , dataType , nullable ):
308
+ def __init__ (self , name , dataType , nullable , metadata = {} ):
309
309
"""Creates a StructField
310
310
:param name: the name of this field.
311
311
:param dataType: the data type of this field.
312
312
:param nullable: indicates whether values of this field
313
313
can be null.
314
+ :param metadata: metadata of this field, which is a map from string
315
+ to simple type that can be serialized to JSON
316
+ automatically
314
317
315
318
>>> (StructField("f1", StringType, True)
316
319
... == StructField("f1", StringType, True))
@@ -322,6 +325,7 @@ def __init__(self, name, dataType, nullable):
322
325
self .name = name
323
326
self .dataType = dataType
324
327
self .nullable = nullable
328
+ self .metadata = metadata
325
329
326
330
def __repr__ (self ):
327
331
return "StructField(%s,%s,%s)" % (self .name , self .dataType ,
@@ -330,13 +334,15 @@ def __repr__(self):
330
334
def jsonValue (self ):
331
335
return {"name" : self .name ,
332
336
"type" : self .dataType .jsonValue (),
333
- "nullable" : self .nullable }
337
+ "nullable" : self .nullable ,
338
+ "metadata" : self .metadata }
334
339
335
340
@classmethod
336
341
def fromJson (cls , json ):
337
342
return StructField (json ["name" ],
338
343
_parse_datatype_json_value (json ["type" ]),
339
- json ["nullable" ])
344
+ json ["nullable" ],
345
+ json ["metadata" ])
340
346
341
347
342
348
class StructType (DataType ):
@@ -415,7 +421,8 @@ def _parse_datatype_json_string(json_string):
415
421
... StructField("simpleArray", simple_arraytype, True),
416
422
... StructField("simpleMap", simple_maptype, True),
417
423
... StructField("simpleStruct", simple_structtype, True),
418
- ... StructField("boolean", BooleanType(), False)])
424
+ ... StructField("boolean", BooleanType(), False),
425
+ ... StructField("withMeta", DoubleType(), False, {"name": "age"})])
419
426
>>> check_datatype(complex_structtype)
420
427
True
421
428
>>> # Complex ArrayType.
0 commit comments