fix leaks
This commit is contained in:
@@ -13,8 +13,6 @@
|
||||
package dynamodb
|
||||
|
||||
import "core:encoding/json"
|
||||
import "core:fmt"
|
||||
import "core:strconv"
|
||||
import "core:strings"
|
||||
|
||||
// ============================================================================
|
||||
@@ -710,7 +708,7 @@ execute_update_plan :: proc(item: ^Item, plan: ^Update_Plan) -> bool {
|
||||
if existing, found := item[action.path]; found {
|
||||
// If existing is a number, add numerically
|
||||
#partial switch v in existing {
|
||||
case Number:
|
||||
case DDB_Number:
|
||||
result, add_ok := numeric_add(existing, action.value)
|
||||
if !add_ok {
|
||||
return false
|
||||
@@ -732,13 +730,13 @@ execute_update_plan :: proc(item: ^Item, plan: ^Update_Plan) -> bool {
|
||||
return false
|
||||
}
|
||||
|
||||
case Number_Set:
|
||||
if new_ns, is_ns := action.value.(Number_Set); is_ns {
|
||||
merged := set_union_strings(([]string)(v), ([]string)(new_ns))
|
||||
case DDB_Number_Set:
|
||||
if new_ns, is_ns := action.value.(DDB_Number_Set); is_ns {
|
||||
merged := set_union_ddb_numbers(([]DDB_Number)(v), ([]DDB_Number)(new_ns))
|
||||
old_copy := existing
|
||||
attr_value_destroy(&old_copy)
|
||||
delete_key(item, action.path)
|
||||
item[strings.clone(action.path)] = Number_Set(merged)
|
||||
item[strings.clone(action.path)] = DDB_Number_Set(merged)
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
@@ -769,14 +767,14 @@ execute_update_plan :: proc(item: ^Item, plan: ^Update_Plan) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
case Number_Set:
|
||||
if del_ns, is_ns := action.value.(Number_Set); is_ns {
|
||||
result := set_difference_strings(([]string)(v), ([]string)(del_ns))
|
||||
case DDB_Number_Set:
|
||||
if del_ns, is_ns := action.value.(DDB_Number_Set); is_ns {
|
||||
result := set_difference_ddb_numbers(([]DDB_Number)(v), ([]DDB_Number)(del_ns))
|
||||
old_copy := existing
|
||||
attr_value_destroy(&old_copy)
|
||||
delete_key(item, action.path)
|
||||
if len(result) > 0 {
|
||||
item[strings.clone(action.path)] = Number_Set(result)
|
||||
item[strings.clone(action.path)] = DDB_Number_Set(result)
|
||||
} else {
|
||||
delete(result)
|
||||
}
|
||||
@@ -810,30 +808,17 @@ numeric_add :: proc(a: Attribute_Value, b: Attribute_Value) -> (Attribute_Value,
|
||||
}
|
||||
|
||||
numeric_subtract :: proc(a: Attribute_Value, b: Attribute_Value) -> (Attribute_Value, bool) {
|
||||
a_num, a_ok := a.(Number)
|
||||
b_num, b_ok := b.(Number)
|
||||
a_num, a_ok := a.(DDB_Number)
|
||||
b_num, b_ok := b.(DDB_Number)
|
||||
if !a_ok || !b_ok {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
a_val, a_parse := strconv.parse_f64(string(a_num))
|
||||
b_val, b_parse := strconv.parse_f64(string(b_num))
|
||||
if !a_parse || !b_parse {
|
||||
result, result_ok := subtract_ddb_numbers(a_num, b_num)
|
||||
if !result_ok {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
result := a_val - b_val
|
||||
result_str := format_number(result)
|
||||
return Number(result_str), true
|
||||
}
|
||||
|
||||
format_number :: proc(val: f64) -> string {
|
||||
// If it's an integer, format without decimal point
|
||||
int_val := i64(val)
|
||||
if f64(int_val) == val {
|
||||
return fmt.aprintf("%d", int_val)
|
||||
}
|
||||
return fmt.aprintf("%g", val)
|
||||
return result, true
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
@@ -873,6 +858,52 @@ set_difference_strings :: proc(a: []string, b: []string) -> []string {
|
||||
return result[:]
|
||||
}
|
||||
|
||||
// Union of two DDB_Number slices (dedup by numeric equality)
|
||||
set_union_ddb_numbers :: proc(a: []DDB_Number, b: []DDB_Number) -> []DDB_Number {
|
||||
result := make([dynamic]DDB_Number)
|
||||
|
||||
// Add all from a
|
||||
for num in a {
|
||||
append(&result, clone_ddb_number(num))
|
||||
}
|
||||
|
||||
// Add from b if not already present
|
||||
for num in b {
|
||||
found := false
|
||||
for existing in result {
|
||||
if compare_ddb_numbers(existing, num) == 0 {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
append(&result, clone_ddb_number(num))
|
||||
}
|
||||
}
|
||||
|
||||
return result[:]
|
||||
}
|
||||
|
||||
// Difference: elements in a that are NOT in b
|
||||
set_difference_ddb_numbers :: proc(a: []DDB_Number, b: []DDB_Number) -> []DDB_Number {
|
||||
result := make([dynamic]DDB_Number)
|
||||
|
||||
for num in a {
|
||||
in_b := false
|
||||
for del in b {
|
||||
if compare_ddb_numbers(num, del) == 0 {
|
||||
in_b = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !in_b {
|
||||
append(&result, clone_ddb_number(num))
|
||||
}
|
||||
}
|
||||
|
||||
return result[:]
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Request Parsing Helper
|
||||
// ============================================================================
|
||||
|
||||
Reference in New Issue
Block a user