clean up AttributeTypeName/Descriptor

This commit is contained in:
Emil Lerch 2024-02-01 09:50:04 -08:00
parent 9020037c20
commit cefefd3cc8
Signed by: lobo
GPG Key ID: A7B62D657EF764F8
2 changed files with 6 additions and 20 deletions

View File

@ -6,23 +6,7 @@ const encryption = @import("encryption.zig");
const ddb = @import("ddb.zig"); const ddb = @import("ddb.zig");
const returnException = @import("main.zig").returnException; const returnException = @import("main.zig").returnException;
// Copied from ddb_type and made inferred. Yuck :( const AttributeValue = union(ddb.AttributeTypeName) {
pub const AttributeTypeName = enum {
string,
number,
binary,
boolean,
null,
map,
list,
string_set,
number_set,
binary_set,
};
// Cannot use AttributeTypeName enum as it is not inferred
// const AttributeValue = union(ddb.AttributeTypeName) {
const AttributeValue = union(AttributeTypeName) {
string: []const u8, string: []const u8,
number: []const u8, // Floating point stored as string number: []const u8, // Floating point stored as string
binary: []const u8, // Base64-encoded binary data object binary: []const u8, // Base64-encoded binary data object
@ -402,13 +386,11 @@ const Params = struct {
writer, writer,
"Request in RequestItems found attribute with invalid type", "Request in RequestItems found attribute with invalid type",
); );
// Convert our enum to something that looks better when reading code
const attribute_type_enum_converted = @as(ddb.AttributeTypeName, @enumFromInt(@intFromEnum(attribute_type_enum.?)));
// Now we need to get *THIS* enum over to our union, which uses the same values // Now we need to get *THIS* enum over to our union, which uses the same values
// We'll just use a switch here, because each of these cases must // We'll just use a switch here, because each of these cases must
// be handled slightly differently // be handled slightly differently
var final_attribute_value: AttributeValue = undefined; var final_attribute_value: AttributeValue = undefined;
switch (attribute_type_enum_converted) { switch (attribute_type_enum.?.toAttributeTypeName()) {
.string => { .string => {
try expectType(attribute_value, .string, request, writer); try expectType(attribute_value, .string, request, writer);
final_attribute_value = .{ .string = attribute_value.string }; final_attribute_value = .{ .string = attribute_value.string };

View File

@ -20,6 +20,10 @@ pub const AttributeTypeDescriptor = enum(u4) {
SS = 7, SS = 7,
NS = 8, NS = 8,
BS = 9, BS = 9,
pub fn toAttributeTypeName(self: AttributeTypeDescriptor) AttributeTypeName {
return @as(AttributeTypeName, @enumFromInt(@intFromEnum(self)));
}
}; };
pub const AttributeTypeName = enum(u4) { pub const AttributeTypeName = enum(u4) {