move service model to its own file

This will need further refactoring to incorporate genned stuff
This commit is contained in:
Emil Lerch 2021-05-29 19:33:08 -07:00
parent 1a97775287
commit b1397d896e
Signed by: lobo
GPG Key ID: A7B62D657EF764F8
4 changed files with 129 additions and 107 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.cache .cache
zig-cache zig-cache
codegen/models/*.zig codegen/models/*.zig
codegen/codegen

View File

@ -11,7 +11,7 @@ pub fn build(b: *Builder) void {
// Standard release options allow the person running `zig build` to select // Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions(); 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 // TODO: Generate src/models.zig

View File

@ -2,117 +2,13 @@ const std = @import("std");
const awshttp = @import("awshttp.zig"); const awshttp = @import("awshttp.zig");
const json = @import("json.zig"); const json = @import("json.zig");
const servicemodel = @import("servicemodel.zig");
const log = std.log.scoped(.aws); const log = std.log.scoped(.aws);
pub const Options = awshttp.Options; pub const Options = awshttp.Options;
// Code "generation" prototype pub const services = servicemodel.services;
// 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 Aws = struct { pub const Aws = struct {
allocator: *std.mem.Allocator, allocator: *std.mem.Allocator,

125
src/servicemodel.zig Normal file
View File

@ -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