Skip to content

Commit 9cf5d6c

Browse files
committed
fix: actually fix string conversions, allow 'salvage' of number types
1 parent 21208e9 commit 9cf5d6c

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

godot/addons/stag_toolkit/plugin/utils.gd

+37-8
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,40 @@ static func factorial(n: int) -> float:
5757
## Fetches the given key out of the dictionary, or null if not found.[br]
5858
## If the fetched value does not match the specified Variant type ([code]valuetype[/code]),
5959
## the value is forcibly converted to that type (applying default when necessary),
60-
## unless an [code]override[/code] of the same type is specified, at which point the override is used.
61-
static func default(dictionary: Dictionary, key: Variant, valuetype: Variant.Type, override: Variant = null) -> Variant:
62-
var value: Variant = dictionary.get(key, "") # Fetch value out of dictionary
63-
if typeof(value) != valuetype:
64-
if typeof(override) == valuetype:
65-
return override
66-
return type_convert(value, valuetype)
67-
return value
60+
## unless an [code]override[/code] of the same type is specified, at which point the override is used.[br]
61+
## If [code]salvage[/code] is true, similiar types (such as integers and floats)
62+
## are converted instead of using the provided override.
63+
## The override is still applied in cases where types are not similiar (such as string and float).
64+
static func default(
65+
dictionary: Dictionary, key: Variant, valuetype: Variant.Type,
66+
override: Variant = null, salvage: bool = true
67+
) -> Variant:
68+
# Fetch value out of dictionary, or use null if nothing was returned
69+
var value: Variant = dictionary.get(key, null)
70+
71+
# If our value was valid, use it!
72+
if typeof(value) == valuetype:
73+
return value
74+
75+
if salvage:
76+
# Attempt to salvage numbers that are of similiar type
77+
match valuetype:
78+
TYPE_INT, TYPE_FLOAT:
79+
match typeof(value):
80+
TYPE_INT, TYPE_FLOAT:
81+
return type_convert(value, valuetype)
82+
83+
# If our override matches our expected type, use it in place of our invalid value.
84+
if typeof(override) == valuetype:
85+
return override
86+
87+
# Explicit type checks:
88+
# Using type_convert from a null to a string, returns "<null>"
89+
# Very nice :)
90+
match valuetype:
91+
TYPE_STRING:
92+
return ""
93+
TYPE_STRING_NAME:
94+
return &""
95+
_: # Otherwise, attempt to convert
96+
return type_convert(value, valuetype)

godot/test/scenarios/test_utils.gd

+15-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ func _ready():
1919
"test": "1.0",
2020
"coolness": true,
2121
Vector2.ZERO: 5,
22+
"dumbness": null,
23+
"float": 5.0,
2224
}
2325

2426
StagTest.assert_equal(1.0, StagUtils.default(d, "test", TYPE_FLOAT), "invalid types should be converted when possible")
@@ -27,5 +29,16 @@ func _ready():
2729
StagTest.assert_equal(5, StagUtils.default(d, Vector2.ZERO, TYPE_INT), "non-string keys can be used")
2830
StagTest.assert_equal(false, StagUtils.default(d, "deer", TYPE_BOOL), "non-existent keys use the corresponding type fallback")
2931
StagTest.assert_equal(true, StagUtils.default(d, "deer", TYPE_BOOL, true), "non-existent keys use overrides when provided")
30-
StagTest.assert_equal("", StagUtils.default(d, "deer", TYPE_STRING), "non-existent keys keep empty strings")
31-
StagTest.assert_true(typeof(&"") == typeof(StagUtils.default(d, "deer", TYPE_STRING_NAME)), "should retain proper type when loading default")
32+
StagTest.assert_equal("", StagUtils.default(d, "deer", TYPE_STRING), "non-existent keys return empty strings")
33+
StagTest.assert_true(typeof(&"") == typeof(StagUtils.default(d, "deer", TYPE_STRING_NAME)),
34+
"should retain proper type when loading default")
35+
36+
StagTest.assert_equal("", StagUtils.default(d, "dumbness", TYPE_STRING),
37+
"fetched null values are returned as empty strings")
38+
StagTest.assert_equal(&"", StagUtils.default(d, "dumbness", TYPE_STRING_NAME),
39+
"fetched null values are returned as empty string names")
40+
41+
StagTest.assert_equal(5, StagUtils.default(d, "float", TYPE_INT, 1),
42+
"numbers within reason are converted")
43+
StagTest.assert_equal(1, StagUtils.default(d, "float", TYPE_INT, 1, false),
44+
"numbers within reason are not converted without salvage")

0 commit comments

Comments
 (0)