consolidate

This commit is contained in:
2026-02-15 13:56:08 -05:00
parent 42db451349
commit cf352dde23
8 changed files with 1096 additions and 338 deletions

View File

@@ -51,13 +51,13 @@ key_destroy :: proc(key: ^Key) {
key_from_item :: proc(item: Item, key_schema: []Key_Schema_Element) -> (Key, bool) {
pk_value: Attribute_Value
sk_value: Maybe(Attribute_Value)
for schema_elem in key_schema {
attr, ok := item[schema_elem.attribute_name]
if !ok {
return {}, false
}
// Validate that key is a scalar type (S, N, or B)
#partial switch _ in attr {
case String, Number, Binary:
@@ -65,10 +65,10 @@ key_from_item :: proc(item: Item, key_schema: []Key_Schema_Element) -> (Key, boo
case:
return {}, false
}
// Deep copy the attribute value
copied := attr_value_deep_copy(attr)
switch schema_elem.key_type {
case .HASH:
pk_value = copied
@@ -76,17 +76,17 @@ key_from_item :: proc(item: Item, key_schema: []Key_Schema_Element) -> (Key, boo
sk_value = copied
}
}
return Key{pk = pk_value, sk = sk_value}, true
}
// Convert key to item
key_to_item :: proc(key: Key, key_schema: []Key_Schema_Element) -> Item {
item := make(Item)
for schema_elem in key_schema {
attr_value: Attribute_Value
switch schema_elem.key_type {
case .HASH:
attr_value = key.pk
@@ -97,10 +97,10 @@ key_to_item :: proc(key: Key, key_schema: []Key_Schema_Element) -> Item {
continue
}
}
item[schema_elem.attribute_name] = attr_value_deep_copy(attr_value)
}
return item
}
@@ -112,8 +112,8 @@ Key_Values :: struct {
key_get_values :: proc(key: ^Key) -> (Key_Values, bool) {
pk_bytes: []byte
switch v in key.pk {
#partial switch v in key.pk {
case String:
pk_bytes = transmute([]byte)string(v)
case Number:
@@ -121,12 +121,13 @@ key_get_values :: proc(key: ^Key) -> (Key_Values, bool) {
case Binary:
pk_bytes = transmute([]byte)string(v)
case:
// Keys should only be scalar types (S, N, or B)
return {}, false
}
sk_bytes: Maybe([]byte)
if sk, ok := key.sk.?; ok {
switch v in sk {
#partial switch v in sk {
case String:
sk_bytes = transmute([]byte)string(v)
case Number:
@@ -134,10 +135,11 @@ key_get_values :: proc(key: ^Key) -> (Key_Values, bool) {
case Binary:
sk_bytes = transmute([]byte)string(v)
case:
// Keys should only be scalar types
return {}, false
}
}
return Key_Values{pk = pk_bytes, sk = sk_bytes}, true
}
@@ -289,9 +291,9 @@ operation_from_target :: proc(target: string) -> Operation {
if !strings.has_prefix(target, prefix) {
return .Unknown
}
op_name := target[len(prefix):]
switch op_name {
case "CreateTable": return .CreateTable
case "DeleteTable": return .DeleteTable
@@ -309,7 +311,7 @@ operation_from_target :: proc(target: string) -> Operation {
case "TransactGetItems": return .TransactGetItems
case "TransactWriteItems": return .TransactWriteItems
}
return .Unknown
}
@@ -327,7 +329,7 @@ DynamoDB_Error_Type :: enum {
error_to_response :: proc(err_type: DynamoDB_Error_Type, message: string) -> string {
type_str: string
switch err_type {
case .ValidationException:
type_str = "com.amazonaws.dynamodb.v20120810#ValidationException"
@@ -346,7 +348,7 @@ error_to_response :: proc(err_type: DynamoDB_Error_Type, message: string) -> str
case .SerializationException:
type_str = "com.amazonaws.dynamodb.v20120810#SerializationException"
}
return fmt.aprintf(`{{"__type":"%s","message":"%s"}}`, type_str, message)
}
@@ -410,30 +412,35 @@ attr_value_destroy :: proc(attr: ^Attribute_Value) {
for s in v {
delete(s)
}
delete([]string(v))
slice := v
delete(slice)
case Number_Set:
for n in v {
delete(n)
}
delete([]string(v))
slice := v
delete(slice)
case Binary_Set:
for b in v {
delete(b)
}
delete([]string(v))
slice := v
delete(slice)
case List:
for item in v {
item_copy := item
attr_value_destroy(&item_copy)
}
delete([]Attribute_Value(v))
list := v
delete(list)
case Map:
for key, val in v {
delete(key)
val_copy := val
attr_value_destroy(&val_copy)
}
delete(map[string]Attribute_Value(v))
m := v
delete(m)
case Bool, Null:
// Nothing to free
}