From 1e544d75c1ee3d92bbde7d812a9cfc0afaf10e0a Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Wed, 15 May 2024 13:16:24 -0700 Subject: [PATCH] upgrade to zig 0.12.0 --- build.zig | 31 +++++++++++++++++++------------ build.zig.zon | 27 +++++++++++++++------------ src/AuthenticatedRequest.zig | 2 +- src/batchgetitem.zig | 10 +++++----- src/batchwriteitem.zig | 12 ++++++------ src/createtable.zig | 6 +++--- src/ddb.zig | 18 +++++++++--------- src/encryption.zig | 8 ++++---- src/main.zig | 4 ++-- 9 files changed, 64 insertions(+), 54 deletions(-) diff --git a/build.zig b/build.zig index ff165be..02b7723 100644 --- a/build.zig +++ b/build.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const universal_lambda = @import("universal_lambda_build"); +const universal_lambda_build = @import("universal-lambda-zig"); // This seems to fail for some reason. zig-sqlite does a lot of messing with // the target. So instead, we will handle this in the CI/CD system at the @@ -95,9 +95,15 @@ pub fn build(b: *std.Build) !void { const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); - try universal_lambda.configureBuild(b, exe); + const universal_lambda_zig_dep = b.dependency("universal-lambda-zig", .{ + .target = target, + .optimize = optimize, + }); + // All modules should be added before this is called + try universal_lambda_build.configureBuild(b, exe, universal_lambda_zig_dep); + _ = universal_lambda_build.addImports(b, exe, universal_lambda_zig_dep); - const exe_aws_dep = b.dependency("aws", .{ + const exe_aws_dep = b.dependency("aws-zig", .{ .target = target, .optimize = optimize, }); @@ -108,16 +114,17 @@ pub fn build(b: *std.Build) !void { .use_bundled = true, }); const exe_sqlite_module = exe_sqlite_dep.module("sqlite"); - exe.addModule("aws-signing", exe_aws_signing_module); - exe.addModule("sqlite", exe_sqlite_module); + exe.root_module.addImport("aws-signing", exe_aws_signing_module); + exe.root_module.addImport("sqlite", exe_sqlite_module); exe.addIncludePath(.{ .path = "c" }); exe.linkLibrary(exe_sqlite_dep.artifact("sqlite")); // Similar to creating the run step earlier, this exposes a `test` step to // the `zig build --help` menu, providing a way for the user to request // running the unit tests. const test_step = b.step("test", "Run unit tests"); - for (test_targets) |t| { - const aws_dep = b.dependency("aws", .{ + for (test_targets) |ct| { + const t = b.resolveTargetQuery(ct); + const aws_dep = b.dependency("aws-zig", .{ .target = t, .optimize = optimize, }); @@ -135,15 +142,15 @@ pub fn build(b: *std.Build) !void { .target = t, .optimize = optimize, }); - _ = try universal_lambda.addModules(b, unit_tests); + _ = universal_lambda_build.addImports(b, unit_tests, universal_lambda_zig_dep); const run_unit_tests = b.addRunArtifact(unit_tests); // run_unit_tests.skip_foreign_checks = true; test_step.dependOn(&run_unit_tests.step); - unit_tests.addModule("aws-signing", aws_signing_module); - unit_tests.addModule("sqlite", sqlite_module); + unit_tests.root_module.addImport("aws-signing", aws_signing_module); + unit_tests.root_module.addImport("sqlite", sqlite_module); unit_tests.addIncludePath(.{ .path = "c" }); unit_tests.linkLibrary(sqlite_dep.artifact("sqlite")); } @@ -152,7 +159,7 @@ pub fn build(b: *std.Build) !void { creds_step.makeFn = generateCredentials; } -fn generateCredentials(s: *std.build.Step, prog_node: *std.Progress.Node) error{ MakeFailed, MakeSkipped }!void { +fn generateCredentials(s: *std.Build.Step, prog_node: *std.Progress.Node) error{ MakeFailed, MakeSkipped }!void { _ = s; // Account id: // Documentation describes account id as a 12 digit number: @@ -258,7 +265,7 @@ pub fn base36encode(comptime T: type, allocator: std.mem.Allocator, data: T) ![] al.appendAssumeCapacity(alphabet[@as(usize, @intCast(remaining % alphabet.len))]); } // This is not exact, but 6 bits - var rc = try al.toOwnedSlice(); + const rc = try al.toOwnedSlice(); std.mem.reverse(u8, rc); return rc; } diff --git a/build.zig.zon b/build.zig.zon index 2e2b927..9326761 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -3,21 +3,24 @@ .version = "0.0.1", .dependencies = .{ - .aws = .{ - .url = "https://git.lerch.org/api/packages/lobo/generic/aws-sdk-with-models/31324c7e83392ce7aeca13629013870181d1458e/31324c7e83392ce7aeca13629013870181d1458e-with-models.tar.gz", - .hash = "1220ae1932ce061440dece024f6d605fb4d00ef3bf03518e2aae0502980108368ee3", - }, .sqlite = .{ - .url = "https://github.com/vrischmann/zig-sqlite/archive/19535aab5760eeaf2979a9dadfca3bb21d1594b9.tar.gz", - .hash = "12208c654deea149cee27eaa45d0e6515c3d8f97d775a4156cbcce0ff424b5d26ea3", + .url = "https://github.com/vrischmann/zig-sqlite/archive/fd17eb9a4ea35f515cc7c2e4755a3a5e284a0bd3.tar.gz", + .hash = "1220e0300613f2b949a8328c5b1d9ba84777f2258c1535039afcb536e9e1228d2881", }, - .universal_lambda_build = .{ - .url = "https://git.lerch.org/lobo/universal-lambda-zig/archive/5f1b1a52beea841e130ea4d878437f9488da0eb7.tar.gz", - .hash = "12202e3f5cc4db196d9bef727e10b407413d6dd95a6e94d66f11c4c14dc5ee060b58", + .@"universal-lambda-zig" = .{ + .url = "https://git.lerch.org/lobo/universal-lambda-zig/archive/f3d80b4afe8c13031b6cff051e93deaeadb1d268.tar.gz", + .hash = "122093d59b28dcd0201eaa1587ca78e11c06fb563b2cb9b554aaa5f7bab0ae34432e", }, - .flexilib = .{ - .url = "https://git.lerch.org/lobo/flexilib/archive/3d3dab9c792651477932e2b61c9f4794ac694dcb.tar.gz", - .hash = "1220fd7a614fe3c9f6006b630bba528e2ec9dca9c66f5ff10f7e471ad2bdd41b6c89", + .@"aws-zig" = .{ + .url = "https://git.lerch.org/api/packages/lobo/generic/aws-sdk-with-models/007f2f588aa30fd2c62d6a037fb87bace8a83710/007f2f588aa30fd2c62d6a037fb87bace8a83710-with-models.tar.gz", + .hash = "1220f128554146e2f58f223a51f1839f49012294a8163a445e8320889539edcda9b3", }, }, + .paths = .{ + "build.zig", + "build.zig.zon", + "src", + "LICENSE", + "README.md", + }, } diff --git a/src/AuthenticatedRequest.zig b/src/AuthenticatedRequest.zig index 5051b6c..6b40632 100644 --- a/src/AuthenticatedRequest.zig +++ b/src/AuthenticatedRequest.zig @@ -2,7 +2,7 @@ const std = @import("std"); allocator: std.mem.Allocator, event_data: []const u8, -headers: std.http.Headers, +headers: []const std.http.Header, status: std.http.Status, reason: ?[]const u8, account_id: u40, diff --git a/src/batchgetitem.zig b/src/batchgetitem.zig index 2c43b6d..e268391 100644 --- a/src/batchgetitem.zig +++ b/src/batchgetitem.zig @@ -295,7 +295,7 @@ const Params = struct { writer, "ExpressionAttributeNames must be an object", ); - var count = v.object.count(); + const count = v.object.count(); var names = v.object.iterator(); var hashmap = std.StringHashMap([]const u8).init(aa); try hashmap.ensureTotalCapacity(@as(u32, @intCast(count))); @@ -434,7 +434,7 @@ test "basic request parsing" { }; var al = std.ArrayList(u8).init(allocator); defer al.deinit(); - var writer = al.writer(); + const writer = al.writer(); var parms = try Params.parseRequest(allocator, &request, writer); defer parms.deinit(); try std.testing.expect(parms.return_consumed_capacity == .total); @@ -463,7 +463,7 @@ test "read item" { defer Account.test_retain_db = false; const allocator = std.testing.allocator; const account_id = 1234; - var db = try Account.dbForAccount(allocator, account_id); + const db = try Account.dbForAccount(allocator, account_id); defer allocator.destroy(db); defer Account.testDbDeinit(); @@ -562,7 +562,7 @@ test "read item" { }; var al = std.ArrayList(u8).init(allocator); defer al.deinit(); - var writer = al.writer(); + const writer = al.writer(); _ = try @import("batchwriteitem.zig").handler(&request, writer); } @@ -596,7 +596,7 @@ test "read item" { }; var al = std.ArrayList(u8).init(allocator); defer al.deinit(); - var writer = al.writer(); + const writer = al.writer(); const output = try handler(&request, writer); defer allocator.free(output); // TODO: Fix this diff --git a/src/batchwriteitem.zig b/src/batchwriteitem.zig index aba0ec9..35610e8 100644 --- a/src/batchwriteitem.zig +++ b/src/batchwriteitem.zig @@ -361,7 +361,7 @@ test "basic request parsing failure" { }; var al = std.ArrayList(u8).init(allocator); defer al.deinit(); - var writer = al.writer(); + const writer = al.writer(); var parms = try Params.parseRequest(allocator, &request, writer); defer parms.deinit(); try std.testing.expectError(error.InvalidPadding, parms.validate()); @@ -398,7 +398,7 @@ test "basic request parsing" { }; var al = std.ArrayList(u8).init(allocator); defer al.deinit(); - var writer = al.writer(); + const writer = al.writer(); var parms = try Params.parseRequest(allocator, &request, writer); defer parms.deinit(); try std.testing.expect(parms.return_consumed_capacity == .none); @@ -477,7 +477,7 @@ test "all types request parsing" { }; var al = std.ArrayList(u8).init(allocator); defer al.deinit(); - var writer = al.writer(); + const writer = al.writer(); var parms = try Params.parseRequest(allocator, &request, writer); defer parms.deinit(); try parms.validate(); @@ -497,7 +497,7 @@ test "all types request parsing" { try std.testing.expectEqualStrings("Binary", put[2].name); try std.testing.expect(put[2].value == .binary); try std.testing.expect(put[2].value.binary.len > 0); - var buf = try allocator.alloc(u8, "this text is base64-encoded".len); + const buf = try allocator.alloc(u8, "this text is base64-encoded".len); defer allocator.free(buf); try std.base64.standard.Decoder.decode(buf, put[2].value.binary); try std.testing.expectEqualStrings("this text is base64-encoded", buf); @@ -509,7 +509,7 @@ test "write item" { defer Account.testDbDeinit(); const allocator = std.testing.allocator; const account_id = 1234; - var db = try Account.dbForAccount(allocator, account_id); + const db = try Account.dbForAccount(allocator, account_id); defer allocator.destroy(db); defer Account.testDbDeinit(); const account = try Account.accountForId(allocator, account_id); // This will get us the encryption key needed @@ -591,6 +591,6 @@ test "write item" { }; var al = std.ArrayList(u8).init(allocator); defer al.deinit(); - var writer = al.writer(); + const writer = al.writer(); _ = try handler(&request, writer); } diff --git a/src/createtable.zig b/src/createtable.zig index 6642008..662f260 100644 --- a/src/createtable.zig +++ b/src/createtable.zig @@ -561,7 +561,7 @@ test "can create a table" { .account_id = 1234, .status = .ok, .reason = null, - .headers = std.http.Headers.init(allocator), + .headers = &.{}, .output_format = .text, }; const output = try handler(&request, std.io.null_writer); @@ -592,7 +592,7 @@ test "will fail an unrecognized request parameter" { .account_id = 1234, .status = .ok, .reason = null, - .headers = std.http.Headers.init(allocator), + .headers = &.{}, .output_format = .text, }; var al = std.ArrayList(u8).init(allocator); @@ -631,7 +631,7 @@ fn failOnShortTableNames(format: AuthenticatedRequest.OutputFormat) !void { .account_id = 1234, .status = .ok, .reason = null, - .headers = std.http.Headers.init(allocator), + .headers = &.{}, .output_format = format, }; var al = std.ArrayList(u8).init(allocator); diff --git a/src/ddb.zig b/src/ddb.zig index 60b6a9b..542f3dc 100644 --- a/src/ddb.zig +++ b/src/ddb.zig @@ -77,7 +77,7 @@ pub const Attribute = struct { // { // "string" : {...attribute value...} // } - var attribute_count = value.count(); + const attribute_count = value.count(); if (attribute_count == 0) try returnException( request, @@ -149,11 +149,11 @@ pub const AttributeValue = union(AttributeTypeName) { if (std.mem.eql(u8, token.string, "binary_set") or std.mem.eql(u8, token.string, "BS")) rc = Self{ .binary_set = try std.json.innerParse([][]const u8, allocator, source, options) }; if (std.mem.eql(u8, token.string, "list") or std.mem.eql(u8, token.string, "L")) { - var json = try std.json.Value.jsonParse(allocator, source, options); + const json = try std.json.Value.jsonParse(allocator, source, options); rc = Self{ .list = json.array }; } if (std.mem.eql(u8, token.string, "map") or std.mem.eql(u8, token.string, "M")) { - var json = try std.json.Value.jsonParse(allocator, source, options); + const json = try std.json.Value.jsonParse(allocator, source, options); rc = Self{ .map = json.object }; } if (rc == null) return error.InvalidEnumTag; @@ -182,11 +182,11 @@ pub const AttributeValue = union(AttributeTypeName) { if (source.object.get("binary_set") orelse source.object.get("BS")) |attr| rc = Self{ .binary_set = try std.json.innerParseFromValue([][]const u8, allocator, attr, options) }; if (source.object.get("list") orelse source.object.get("L")) |attr| { - var json = try std.json.Value.jsonParseFromValue(allocator, attr, options); + const json = try std.json.Value.jsonParseFromValue(allocator, attr, options); rc = Self{ .list = json.array }; } if (source.object.get("map") orelse source.object.get("M")) |attr| { - var json = try std.json.Value.jsonParseFromValue(allocator, attr, options); + const json = try std.json.Value.jsonParseFromValue(allocator, attr, options); rc = Self{ .map = json.object }; } if (rc == null) return error.InvalidEnumTag; @@ -262,7 +262,7 @@ pub const AttributeValue = union(AttributeTypeName) { return error.InvalidPadding; } if (leftover_idx == null) return; - var leftover = source[leftover_idx.?..]; + const leftover = source[leftover_idx.?..]; if (decoder.pad_char) |pad_char| { const padding_len = acc_len / 2; var padding_chars: usize = 0; @@ -677,7 +677,7 @@ fn insertIntoDm( } fn testCreateTable(allocator: std.mem.Allocator, account_id: u40) !*sqlite.Db { - var db = try Account.dbForAccount(allocator, account_id); + const db = try Account.dbForAccount(allocator, account_id); const account = try Account.accountForId(allocator, account_id); // This will get us the encryption key needed defer account.deinit(); var hash = AttributeDefinition{ .name = "Artist", .type = .S }; @@ -716,7 +716,7 @@ test "can list tables in an account" { Account.test_retain_db = true; const allocator = std.testing.allocator; const account_id = 1234; - var db = try testCreateTable(allocator, account_id); + const db = try testCreateTable(allocator, account_id); defer allocator.destroy(db); defer Account.testDbDeinit(); var table_list = try tablesForAccount(allocator, account_id); @@ -730,7 +730,7 @@ test "can put an item in a table in an account" { Account.test_retain_db = true; const allocator = std.testing.allocator; const account_id = 1234; - var db = try testCreateTable(allocator, account_id); + const db = try testCreateTable(allocator, account_id); defer allocator.destroy(db); defer Account.testDbDeinit(); var table_list = try tablesForAccount(allocator, account_id); diff --git a/src/encryption.zig b/src/encryption.zig index 3dc1e2f..90ec226 100644 --- a/src/encryption.zig +++ b/src/encryption.zig @@ -97,7 +97,7 @@ pub fn encryptAndEncode(allocator: std.mem.Allocator, key: [key_length]u8, plain const ciphertext = try encrypt(allocator, key, plaintext); defer allocator.free(ciphertext); const Encoder = std.base64.standard.Encoder; - var encoded_ciphertext = try allocator.alloc(u8, Encoder.calcSize(ciphertext.len)); + const encoded_ciphertext = try allocator.alloc(u8, Encoder.calcSize(ciphertext.len)); errdefer allocator.free(encoded_ciphertext); return Encoder.encode(encoded_ciphertext, ciphertext); } @@ -108,14 +108,14 @@ pub fn encryptAndEncodeWithNonce(allocator: std.mem.Allocator, key: [key_length] const ciphertext = try encryptWithNonce(allocator, key, nonce, plaintext); defer allocator.free(ciphertext); const Encoder = std.base64.standard.Encoder; - var encoded_ciphertext = try allocator.alloc(u8, Encoder.calcSize(ciphertext.len)); + const encoded_ciphertext = try allocator.alloc(u8, Encoder.calcSize(ciphertext.len)); errdefer allocator.free(encoded_ciphertext); return Encoder.encode(encoded_ciphertext, ciphertext); } /// Decrypts data. Use deriveKey function to get a key from password/salt pub fn decrypt(allocator: std.mem.Allocator, key: [key_length]u8, ciphertext: []const u8) ![]const u8 { - var plaintext = try allocator.alloc( + const plaintext = try allocator.alloc( u8, ciphertext.len - nonce_length - std.crypto.aead.salsa_poly.XSalsa20Poly1305.tag_length, ); @@ -140,7 +140,7 @@ pub fn decrypt(allocator: std.mem.Allocator, key: [key_length]u8, ciphertext: [] pub fn decodeAndDecrypt(allocator: std.mem.Allocator, key: [key_length]u8, encoded_ciphertext: []const u8) ![]const u8 { const Decoder = std.base64.standard.Decoder; const ciphertext_len = try Decoder.calcSizeForSlice(encoded_ciphertext); - var ciphertext = try allocator.alloc(u8, ciphertext_len); + const ciphertext = try allocator.alloc(u8, ciphertext_len); defer allocator.free(ciphertext); try std.base64.standard.Decoder.decode(ciphertext, encoded_ciphertext); return try decrypt(allocator, key, ciphertext); diff --git a/src/main.zig b/src/main.zig index 0d4dd82..f22a7f0 100644 --- a/src/main.zig +++ b/src/main.zig @@ -99,7 +99,7 @@ fn executeOperation( }; } fn authenticateUser(allocator: std.mem.Allocator, context: universal_lambda_interface.Context, target: []const u8, headers: std.http.Headers, body_reader: anytype) !void { - var request = signing.UnverifiedRequest{ + const request = signing.UnverifiedRequest{ .method = std.http.Method.POST, .target = target, .headers = headers, @@ -167,7 +167,7 @@ fn fillRootCreds(allocator: std.mem.Allocator) !void { var line_num: usize = 1; while (reader.streamUntilDelimiter(line_writer, '\n', null)) : (line_num += 1) { defer line.clearRetainingCapacity(); - var relevant_line = line.items[0 .. std.mem.indexOfScalar(u8, line.items, '#') orelse line.items.len]; + const relevant_line = line.items[0 .. std.mem.indexOfScalar(u8, line.items, '#') orelse line.items.len]; const relevant_line_trimmed = std.mem.trim(u8, relevant_line, " \t"); var value_iterator = std.mem.splitScalar(u8, relevant_line_trimmed, ','); if (std.mem.trim(u8, value_iterator.peek().?, " \t").len == 0) continue;