diff --git a/.gitignore b/.gitignore index 2703284..910d90e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .cache zig-cache codegen/models/*.zig +codegen/codegen diff --git a/build.zig b/build.zig index ffaa897..03a8877 100644 --- a/build.zig +++ b/build.zig @@ -11,7 +11,7 @@ pub fn build(b: *Builder) void { // Standard release options allow the person running `zig build` to select // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. const mode = b.standardReleaseOptions(); - const exe = b.addExecutable("start-hand-test", "src/main.zig"); + const exe = b.addExecutable("demo", "src/main.zig"); // TODO: Generate src/models.zig diff --git a/src/aws.zig b/src/aws.zig index e1050f4..5e8e364 100644 --- a/src/aws.zig +++ b/src/aws.zig @@ -2,117 +2,13 @@ const std = @import("std"); const awshttp = @import("awshttp.zig"); const json = @import("json.zig"); +const servicemodel = @import("servicemodel.zig"); const log = std.log.scoped(.aws); pub const Options = awshttp.Options; -// Code "generation" prototype -// TODO: Make generic -pub fn Services() type { - const types = [_]type{ - Service("sts"), - }; - return @Type(.{ - .Struct = .{ - .layout = .Auto, - .fields = &[_]std.builtin.TypeInfo.StructField{ - .{ - .name = "sts", - .field_type = types[0], - .default_value = new(types[0]), - .is_comptime = false, - .alignment = 0, - }, - }, - .decls = &[_]std.builtin.TypeInfo.Declaration{}, - .is_tuple = false, - }, - }); -} - -fn ServiceActionResponse(comptime service: []const u8, comptime action: []const u8) type { - if (std.mem.eql(u8, service, "sts") and std.mem.eql(u8, action, "get_caller_identity")) { - return struct { - arn: []const u8, - user_id: []const u8, - account: []const u8, - }; - } - unreachable; -} - -fn ServiceAction(comptime service: []const u8, comptime action: []const u8) type { - if (std.mem.eql(u8, service, "sts") and std.mem.eql(u8, action, "get_caller_identity")) { - return @Type(.{ - .Struct = .{ - .layout = .Auto, - .fields = &[_]std.builtin.TypeInfo.StructField{ - .{ - .name = "Request", - .field_type = type, - .default_value = struct {}, - .is_comptime = false, - .alignment = 0, - }, - .{ - .name = "action_name", - .field_type = @TypeOf("GetCallerIdentity"), - .default_value = "GetCallerIdentity", - .is_comptime = false, - .alignment = 0, - }, - // TODO: maybe best is to separate requests from responses in whole other struct? - .{ - .name = "Response", - .field_type = type, - .default_value = ServiceActionResponse("sts", "get_caller_identity"), - .is_comptime = false, - .alignment = 0, - }, - }, - .decls = &[_]std.builtin.TypeInfo.Declaration{}, - .is_tuple = false, - }, - }); - } - unreachable; -} - -pub const services = Services(){}; - -fn new(comptime T: type) T { - return T{}; -} -fn Service(comptime service: []const u8) type { - if (std.mem.eql(u8, "sts", service)) { - return @Type(.{ - .Struct = .{ - .layout = .Auto, - .fields = &[_]std.builtin.TypeInfo.StructField{ - .{ - .name = "version", - .field_type = @TypeOf("2011-06-15"), - .default_value = "2011-06-15", - .is_comptime = false, - .alignment = 0, - }, - .{ - .name = "get_caller_identity", - .field_type = ServiceAction("sts", "get_caller_identity"), - .default_value = new(ServiceAction("sts", "get_caller_identity")), - .is_comptime = false, - .alignment = 0, - }, - }, - .decls = &[_]std.builtin.TypeInfo.Declaration{}, - .is_tuple = false, - }, - }); - } - unreachable; -} -// End code "generation" prototype +pub const services = servicemodel.services; pub const Aws = struct { allocator: *std.mem.Allocator, diff --git a/src/servicemodel.zig b/src/servicemodel.zig new file mode 100644 index 0000000..9fad5cc --- /dev/null +++ b/src/servicemodel.zig @@ -0,0 +1,125 @@ +const std = @import("std"); +const models = @import("models.zig"); +const json = @import("json.zig"); + +const Model = []struct { + smithy: []const u8, +metadata: struct { + suppressions: []struct { + id: []const u8, + namespace: []const u8, + }, + }, + shapes: struct +}; +const model = { + var stream = json.TokenStream.init(models); + const res = json.parse(Config, &stream, .{}); + // Assert no error can occur since we are + // parsing this JSON at comptime! + break :x res catch unreachable; +}; +// TODO: Make generic +fn Services() type { + const types = [_]type{ + Service("sts"), + }; + return @Type(.{ + .Struct = .{ + .layout = .Auto, + .fields = &[_]std.builtin.TypeInfo.StructField{ + .{ + .name = "sts", + .field_type = types[0], + .default_value = new(types[0]), + .is_comptime = false, + .alignment = 0, + }, + }, + .decls = &[_]std.builtin.TypeInfo.Declaration{}, + .is_tuple = false, + }, + }); +} + +fn ServiceActionResponse(comptime service: []const u8, comptime action: []const u8) type { + if (std.mem.eql(u8, service, "sts") and std.mem.eql(u8, action, "get_caller_identity")) { + return struct { + arn: []const u8, + user_id: []const u8, + account: []const u8, + }; + } + unreachable; +} + +fn ServiceAction(comptime service: []const u8, comptime action: []const u8) type { + if (std.mem.eql(u8, service, "sts") and std.mem.eql(u8, action, "get_caller_identity")) { + return @Type(.{ + .Struct = .{ + .layout = .Auto, + .fields = &[_]std.builtin.TypeInfo.StructField{ + .{ + .name = "Request", + .field_type = type, + .default_value = struct {}, + .is_comptime = false, + .alignment = 0, + }, + .{ + .name = "action_name", + .field_type = @TypeOf("GetCallerIdentity"), + .default_value = "GetCallerIdentity", + .is_comptime = false, + .alignment = 0, + }, + .{ + .name = "Response", + .field_type = type, + .default_value = ServiceActionResponse("sts", "get_caller_identity"), + .is_comptime = false, + .alignment = 0, + }, + }, + .decls = &[_]std.builtin.TypeInfo.Declaration{}, + .is_tuple = false, + }, + }); + } + unreachable; +} + +pub const services = Services(){}; + +fn new(comptime T: type) T { + return T{}; +} +fn Service(comptime service: []const u8) type { + if (std.mem.eql(u8, "sts", service)) { + return @Type(.{ + .Struct = .{ + .layout = .Auto, + .fields = &[_]std.builtin.TypeInfo.StructField{ + .{ + .name = "version", + .field_type = @TypeOf("2011-06-15"), + .default_value = "2011-06-15", + .is_comptime = false, + .alignment = 0, + }, + .{ + .name = "get_caller_identity", + .field_type = ServiceAction("sts", "get_caller_identity"), + .default_value = new(ServiceAction("sts", "get_caller_identity")), + .is_comptime = false, + .alignment = 0, + }, + }, + .decls = &[_]std.builtin.TypeInfo.Declaration{}, + .is_tuple = false, + }, + }); + } + unreachable; +} +// End code "generation" prototype