Compare commits

..

No commits in common. "17f115d9c60ce598a314b18ae89828eef2955915" and "0a5a08a7ede2bb20d9c6207e4b9a6f1ce96ccf5c" have entirely different histories.

2 changed files with 11 additions and 15 deletions

View File

@ -30,9 +30,9 @@ pub fn build(b: *std.Build) void {
b.installArtifact(lib); b.installArtifact(lib);
const module = b.addModule("smithy", .{ const module = b.addModule("smithy", .{
.root_source_file = .{ .path = "src/smithy.zig" }, .source_file = .{ .path = "src/smithy.zig" },
}); });
lib.root_module.addImport("smithy", module); lib.addModule("smithy", module);
// Creates a step for unit testing. This only builds the test executable // Creates a step for unit testing. This only builds the test executable
// but does not run it. // but does not run it.

View File

@ -7,16 +7,14 @@ pub const Smithy = struct {
metadata: ModelMetadata, metadata: ModelMetadata,
shapes: []ShapeInfo, shapes: []ShapeInfo,
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
json_source: std.json.Parsed(std.json.Value),
const Self = @This(); const Self = @This();
pub fn init(allocator: std.mem.Allocator, version: []const u8, metadata: ModelMetadata, shapeinfo: []ShapeInfo, json_source: std.json.Parsed(std.json.Value)) Smithy { pub fn init(allocator: std.mem.Allocator, version: []const u8, metadata: ModelMetadata, shapeinfo: []ShapeInfo) Smithy {
return .{ return .{
.version = version, .version = version,
.metadata = metadata, .metadata = metadata,
.shapes = shapeinfo, .shapes = shapeinfo,
.allocator = allocator, .allocator = allocator,
.json_source = json_source,
}; };
} }
pub fn deinit(self: Self) void { pub fn deinit(self: Self) void {
@ -78,7 +76,6 @@ pub const Smithy = struct {
} }
} }
self.allocator.free(self.shapes); self.allocator.free(self.shapes);
self.json_source.deinit();
} }
}; };
pub const ShapeInfo = struct { pub const ShapeInfo = struct {
@ -102,13 +99,13 @@ pub const TraitType = enum {
aws_auth_sigv4, aws_auth_sigv4,
aws_protocol, aws_protocol,
ec2_query_name, ec2_query_name,
json_name,
xml_name,
http, http,
http_header, http_header,
http_label, http_label,
http_query, http_query,
http_payload, http_payload,
json_name,
xml_name,
required, required,
documentation, documentation,
pattern, pattern,
@ -256,10 +253,10 @@ pub const AwsProtocol = enum {
}; };
pub fn parse(allocator: std.mem.Allocator, json_model: []const u8) !Smithy { pub fn parse(allocator: std.mem.Allocator, json_model: []const u8) !Smithy {
// construct a parser. We're not copying strings here // construct a parser. We're not copying strings here, but that may
// Instead, we keep the original json string around // be a poor decision
// This might be bad if we only need a small fraction of the original json source
var vt = try std.json.parseFromSlice(std.json.Value, allocator, json_model, .{}); var vt = try std.json.parseFromSlice(std.json.Value, allocator, json_model, .{});
defer vt.deinit();
return Smithy.init( return Smithy.init(
allocator, allocator,
vt.value.object.get("smithy").?.string, vt.value.object.get("smithy").?.string,
@ -268,7 +265,6 @@ pub fn parse(allocator: std.mem.Allocator, json_model: []const u8) !Smithy {
.suppressions = &.{}, .suppressions = &.{},
}, },
try shapes(allocator, vt.value.object.get("shapes").?.object), try shapes(allocator, vt.value.object.get("shapes").?.object),
vt,
); );
} }
@ -576,7 +572,7 @@ fn getShape(allocator: std.mem.Allocator, shape: std.json.Value) SmithyParseErro
} }
fn parseMembers(allocator: std.mem.Allocator, shape: ?std.json.Value) SmithyParseError![]TypeMember { fn parseMembers(allocator: std.mem.Allocator, shape: ?std.json.Value) SmithyParseError![]TypeMember {
const rc: []TypeMember = &.{}; var rc: []TypeMember = &.{};
if (shape == null) if (shape == null)
return rc; return rc;
@ -610,7 +606,7 @@ fn parseTraitsOnly(allocator: std.mem.Allocator, shape: std.json.Value) SmithyPa
} }
fn parseTraits(allocator: std.mem.Allocator, shape: ?std.json.Value) SmithyParseError![]Trait { fn parseTraits(allocator: std.mem.Allocator, shape: ?std.json.Value) SmithyParseError![]Trait {
const rc: []Trait = &.{}; var rc: []Trait = &.{};
if (shape == null) if (shape == null)
return rc; return rc;
@ -868,7 +864,7 @@ fn read_file_to_string(allocator: std.mem.Allocator, file_name: []const u8, max_
return file.readToEndAlloc(allocator, max_bytes); return file.readToEndAlloc(allocator, max_bytes);
} }
const test_data: []const u8 = @embedFile("test.json"); const test_data: []const u8 = @embedFile("test.json");
const intrinsic_type_count: usize = 15; // 15 intrinsic types are added to every model (see shapes() function) const intrinsic_type_count: usize = 5; // 5 intrinsic types are added to every model
test "parse string" { test "parse string" {
const test_string = const test_string =