From fa306a52e2c84980912955dd00b91abe36027723 Mon Sep 17 00:00:00 2001 From: Kyle Szklenski Date: Thu, 27 Jun 2024 06:44:24 -0400 Subject: [PATCH] Update so fields have correct types --- addons/godot-firebase/Utilities.gd | 37 ++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/addons/godot-firebase/Utilities.gd b/addons/godot-firebase/Utilities.gd index efae477..d9ae3b5 100644 --- a/addons/godot-firebase/Utilities.gd +++ b/addons/godot-firebase/Utilities.gd @@ -52,10 +52,37 @@ static func dict2fields(dict : Dictionary) -> Dictionary: return {'fields' : fields} -static func from_firebase_type(value : Variant) -> Variant: + +class FirebaseTypeConverter extends RefCounted: + var converters = { + "nullValue": _to_null, + "booleanValue": _to_bool, + "integerValue": _to_int, + "doubleValue": _to_float + } + + func convert_value(type, value): + if converters.has(type): + return converters[type].call(value) + + return value + + func _to_null(value): + return null + + func _to_bool(value): + return bool(value) + + func _to_int(value): + return int(value) + + func _to_float(value): + return float(value) + +static func from_firebase_type(value): if value == null: return null - + if value.has("mapValue"): value = fields2dict(value.values()[0]) elif value.has("arrayValue"): @@ -63,10 +90,12 @@ static func from_firebase_type(value : Variant) -> Variant: elif value.has("timestampValue"): value = Time.get_datetime_dict_from_datetime_string(value.values()[0], false) else: - value = value.values()[0] - + var converter = FirebaseTypeConverter.new() + value = converter.convert_value(value.keys()[0], value.values()[0]) + return value + static func to_firebase_type(value : Variant) -> Dictionary: var var_type : String = ""