individual service support - evaluation exceeded 1000 branches

This commit is contained in:
Emil Lerch 2021-06-10 14:09:22 -07:00
parent dff03429d9
commit f9e6f92026
Signed by: lobo
GPG Key ID: A7B62D657EF764F8

View File

@ -1,15 +1,16 @@
const std = @import("std"); const std = @import("std");
const expectEqualStrings = std.testing.expectEqualStrings; const expectEqualStrings = std.testing.expectEqualStrings;
// TODO: Make generic pub fn Services(service_imports: anytype) type {
fn Services() type {
// This service list can be imported from a master file of all services // This service list can be imported from a master file of all services
// provided by codegen // provided by codegen
const service_list = @import("codegen/service_manifest.zig").services; const service_list = @import("codegen/service_manifest.zig").services;
// From here, the fields of our structure can be generated at comptime... // From here, the fields of our structure can be generated at comptime...
var fields: [service_list.len]std.builtin.TypeInfo.StructField = undefined; var fields: [serviceCount(service_list, service_imports)]std.builtin.TypeInfo.StructField = undefined;
var inx = 0;
for (fields) |*item, i| { for (fields) |*item, i| {
if (service_imports.len == 0) {
const import = @field(@import("codegen/models/" ++ service_list[i].file_name), service_list[i].export_name); const import = @field(@import("codegen/models/" ++ service_list[i].file_name), service_list[i].export_name);
item.* = .{ item.* = .{
.name = service_list[i].name, .name = service_list[i].name,
@ -18,6 +19,33 @@ fn Services() type {
.is_comptime = false, .is_comptime = false,
.alignment = 0, .alignment = 0,
}; };
continue;
}
var found = false;
// we will loop through the big list and check each service
// against the list of desired imports
while (inx < service_list.len) {
for (service_imports) |si| {
if (std.mem.eql(u8, @tagName(si), service_list[inx].name)) {
const import = @field(@import("codegen/models/" ++ service_list[inx].file_name), service_list[inx].export_name);
item.* = .{
.name = service_list[inx].name,
.field_type = @TypeOf(import),
.default_value = import,
.is_comptime = false,
.alignment = 0,
};
found = true;
break;
}
}
inx = inx + 1; // service found or not in list - move to next service
if (found) break;
}
if (!found)
@compileError("imported service(s) not found");
} }
// finally, generate the type // finally, generate the type
@ -30,21 +58,32 @@ fn Services() type {
}, },
}); });
} }
// TODO: One constant to rule them all is a bit much. We can keep this
// in the back pocket, but let's move to a factory style getService("blah"); fn serviceCount(service_list: anytype, desired_services: anytype) usize {
pub const services = Services(){}; if (desired_services.len == 0) return service_list.len;
return desired_services.len;
}
/// Using this constant may blow up build times. Recommed using Services()
/// function directly, e.g. const services = Services(.{.sts, .ec2, .s3, .ddb}){};
pub const services = Services(.{}){};
test "services includes sts" { test "services includes sts" {
expectEqualStrings("2011-06-15", services.sts.version); try expectEqualStrings("2011-06-15", services.sts.version);
} }
test "sts includes get_caller_identity" { test "sts includes get_caller_identity" {
expectEqualStrings("GetCallerIdentity", services.sts.get_caller_identity.action_name); try expectEqualStrings("GetCallerIdentity", services.sts.get_caller_identity.action_name);
} }
test "can get service and action name from request" { test "can get service and action name from request" {
// get request object. This call doesn't have parameters // get request object. This call doesn't have parameters
const req = services.sts.get_caller_identity.Request{}; const req = services.sts.get_caller_identity.Request{};
// const metadata = @TypeOf(req).metaInfo(); // const metadata = @TypeOf(req).metaInfo();
const metadata = req.metaInfo(); const metadata = req.metaInfo();
expectEqualStrings("2011-06-15", metadata.service.version); try expectEqualStrings("2011-06-15", metadata.service.version);
// expectEqualStrings("GetCallerIdentity", metadata.action.action_name); // expectEqualStrings("GetCallerIdentity", metadata.action.action_name);
} }
test "can filter services" {
const filtered_services = Services(.{ .sts, .waf_v2 }){};
// const filtered_services = Services(.{.sts}){};
try expectEqualStrings("2011-06-15", filtered_services.sts.version);
}