2021-05-30 02:33:08 +00:00
|
|
|
const std = @import("std");
|
2021-06-09 23:22:44 +00:00
|
|
|
const expectEqualStrings = std.testing.expectEqualStrings;
|
2021-05-30 02:33:08 +00:00
|
|
|
|
2021-06-10 21:09:22 +00:00
|
|
|
pub fn Services(service_imports: anytype) type {
|
2021-06-09 23:22:44 +00:00
|
|
|
// This service list can be imported from a master file of all services
|
|
|
|
// provided by codegen
|
|
|
|
const service_list = @import("codegen/service_manifest.zig").services;
|
|
|
|
|
|
|
|
// From here, the fields of our structure can be generated at comptime...
|
2021-06-10 21:09:22 +00:00
|
|
|
var fields: [serviceCount(service_list, service_imports)]std.builtin.TypeInfo.StructField = undefined;
|
|
|
|
var inx = 0;
|
2021-06-09 23:22:44 +00:00
|
|
|
for (fields) |*item, i| {
|
2021-06-10 21:09:22 +00:00
|
|
|
if (service_imports.len == 0) {
|
|
|
|
const import = @field(@import("codegen/models/" ++ service_list[i].file_name), service_list[i].export_name);
|
|
|
|
item.* = .{
|
|
|
|
.name = service_list[i].name,
|
|
|
|
.field_type = @TypeOf(import),
|
|
|
|
.default_value = import,
|
|
|
|
.is_comptime = false,
|
|
|
|
.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");
|
2021-06-09 23:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// finally, generate the type
|
2021-05-30 02:33:08 +00:00
|
|
|
return @Type(.{
|
|
|
|
.Struct = .{
|
|
|
|
.layout = .Auto,
|
2021-06-09 23:22:44 +00:00
|
|
|
.fields = &fields,
|
2021-05-30 02:33:08 +00:00
|
|
|
.decls = &[_]std.builtin.TypeInfo.Declaration{},
|
|
|
|
.is_tuple = false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2021-06-10 21:09:22 +00:00
|
|
|
|
|
|
|
fn serviceCount(service_list: anytype, desired_services: anytype) usize {
|
|
|
|
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(.{}){};
|
2021-05-30 02:33:08 +00:00
|
|
|
|
2021-06-09 23:22:44 +00:00
|
|
|
test "services includes sts" {
|
2021-06-10 21:09:22 +00:00
|
|
|
try expectEqualStrings("2011-06-15", services.sts.version);
|
2021-05-30 02:33:08 +00:00
|
|
|
}
|
2021-06-09 23:22:44 +00:00
|
|
|
test "sts includes get_caller_identity" {
|
2021-06-10 21:09:22 +00:00
|
|
|
try expectEqualStrings("GetCallerIdentity", services.sts.get_caller_identity.action_name);
|
2021-06-09 23:22:44 +00:00
|
|
|
}
|
|
|
|
test "can get service and action name from request" {
|
|
|
|
// get request object. This call doesn't have parameters
|
|
|
|
const req = services.sts.get_caller_identity.Request{};
|
|
|
|
// const metadata = @TypeOf(req).metaInfo();
|
|
|
|
const metadata = req.metaInfo();
|
2021-06-10 21:09:22 +00:00
|
|
|
try expectEqualStrings("2011-06-15", metadata.service.version);
|
2021-06-09 23:22:44 +00:00
|
|
|
// expectEqualStrings("GetCallerIdentity", metadata.action.action_name);
|
2021-05-30 02:33:08 +00:00
|
|
|
}
|
2021-06-10 21:09:22 +00:00
|
|
|
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);
|
|
|
|
}
|