@@ -37,23 +37,54 @@ static func dict2fields(dict : Dictionary) -> Dictionary:
3737 TYPE_REAL : var_type = "doubleValue"
3838 TYPE_STRING : var_type = "stringValue"
3939 TYPE_DICTIONARY :
40- var_type = "mapValue"
41- field_value = dict2fields (field_value )
40+ if is_field_timestamp (field_value ):
41+ var_type = "timestampValue"
42+ field_value = dict2timestamp (field_value )
43+ else :
44+ var_type = "mapValue"
45+ field_value = dict2fields (field_value )
4246 TYPE_ARRAY :
4347 var_type = "arrayValue"
4448 field_value = {"values" : array2fields (field_value )}
4549 fields [field ] = { var_type : field_value }
4650 return {'fields' : fields }
4751
52+ # Pass the .fields inside a Firestore Document to print out the Dictionary { 'key' : 'value' }
53+ static func fields2dict (doc : Dictionary ) -> Dictionary :
54+ var dict : Dictionary = {}
55+ if doc .has ("fields" ):
56+ for field in (doc .fields ).keys ():
57+ if (doc .fields )[field ].has ("mapValue" ):
58+ dict [field ] = fields2dict ((doc .fields )[field ].mapValue )
59+ elif (doc .fields )[field ].has ("timestampValue" ):
60+ dict [field ] = timestamp2dict ((doc .fields )[field ].timestampValue )
61+ elif (doc .fields )[field ].has ("arrayValue" ):
62+ dict [field ] = fields2array ((doc .fields )[field ].arrayValue )
63+ elif (doc .fields )[field ].has ("integerValue" ):
64+ dict [field ] = (doc .fields )[field ].values ()[0 ] as int
65+ elif (doc .fields )[field ].has ("doubleValue" ):
66+ dict [field ] = (doc .fields )[field ].values ()[0 ] as float
67+ elif (doc .fields )[field ].has ("booleanValue" ):
68+ dict [field ] = (doc .fields )[field ].values ()[0 ] as bool
69+ elif (doc .fields )[field ].has ("nullValue" ):
70+ dict [field ] = null
71+ else :
72+ dict [field ] = (doc .fields )[field ].values ()[0 ]
73+ return dict
74+
4875# Pass an Array to parse it to a Firebase arrayValue
4976static func array2fields (array : Array ) -> Array :
5077 var fields : Array = []
5178 var var_type : String = ""
5279 for field in array :
53- if typeof (field ) == TYPE_DICTIONARY :
54- fields .append ({'mapValue' : dict2fields (field ) })
55- continue
5680 match typeof (field ):
81+ TYPE_DICTIONARY :
82+ if is_field_timestamp (field ):
83+ var_type = "timestampValue"
84+ field = dict2timestamp (field )
85+ else :
86+ var_type = "mapValue"
87+ field = dict2fields (field )
5788 TYPE_NIL : var_type = "nullValue"
5889 TYPE_BOOL : var_type = "booleanValue"
5990 TYPE_INT : var_type = "integerValue"
@@ -81,33 +112,33 @@ static func fields2array(array : Dictionary) -> Array:
81112 item = field .values ()[0 ] as float
82113 "booleanValue" :
83114 item = field .values ()[0 ] as bool
115+ "timestampValue" :
116+ item = timestamp2dict (field .timestampValue )
84117 "nullValue" :
85118 item = null
86119 _ :
87120 item = field .values ()[0 ]
88121 fields .append (item )
89122 return fields
90123
91- # Pass the .fields inside a Firestore Document to print out the Dictionary { 'key' : 'value' }
92- static func fields2dict (doc : Dictionary ) -> Dictionary :
93- var dict : Dictionary = {}
94- if doc .has ("fields" ):
95- for field in (doc .fields ).keys ():
96- if (doc .fields )[field ].has ("mapValue" ):
97- dict [field ] = fields2dict ((doc .fields )[field ].mapValue )
98- elif (doc .fields )[field ].has ("arrayValue" ):
99- dict [field ] = fields2array ((doc .fields )[field ].arrayValue )
100- elif (doc .fields )[field ].has ("integerValue" ):
101- dict [field ] = (doc .fields )[field ].values ()[0 ] as int
102- elif (doc .fields )[field ].has ("doubleValue" ):
103- dict [field ] = (doc .fields )[field ].values ()[0 ] as float
104- elif (doc .fields )[field ].has ("booleanValue" ):
105- dict [field ] = (doc .fields )[field ].values ()[0 ] as bool
106- elif (doc .fields )[field ].has ("nullValue" ):
107- dict [field ] = null
108- else :
109- dict [field ] = (doc .fields )[field ].values ()[0 ]
110- return dict
124+ # Converts a gdscript Dictionary (most likely obtained with OS.get_datetime()) to a Firebase Timestamp
125+ static func dict2timestamp (dict : Dictionary ) -> String :
126+ var dict_values : Array = dict .values ()
127+ dict_values .remove (3 )
128+ dict_values .remove (3 )
129+ return "%04d -%02d -%02d T%02d :%02d :%02d .00Z" % dict_values
130+
131+ # Converts a Firebase Timestamp back to a gdscript Dictionary
132+ static func timestamp2dict (timestamp : String ) -> Dictionary :
133+ var datetime : Dictionary = {year = 0 , month = 0 , day = 0 , hour = 0 , minute = 0 , second = 0 }
134+ var dict : PoolStringArray = timestamp .split ("T" )[0 ].split ("-" )
135+ dict .append_array (timestamp .split ("T" )[1 ].split (":" ))
136+ for value in dict .size () :
137+ datetime [datetime .keys ()[value ]] = int (dict [value ])
138+ return datetime
139+
140+ static func is_field_timestamp (field : Dictionary ) -> bool :
141+ return field .has_all (['year' ,'month' ,'day' ,'hour' ,'minute' ,'second' ])
111142
112143# Call print(document) to return directly this document formatted
113144func _to_string () -> String :
0 commit comments