-
-
Notifications
You must be signed in to change notification settings - Fork 4
Add <auto-generated> header to output file #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
eabde87
Add generated header helper with deterministic render and unit tests
christianhelle 3cfed81
Wire generated header into CLI and library outputs
christianhelle 5c6dc25
Regenerate sample outputs with generated header
christianhelle 0a36359
Add <auto-generated> lines to header
christianhelle 996389b
Add renderNowFromBuildInfo helper to generated_header
christianhelle c5a3753
Use renderNowFromBuildInfo in CLI generator
christianhelle 0b0a87e
Use renderNowFromBuildInfo and simplify models_code cleanup in lib.zig
christianhelle 40e9f47
Use test_utils allocator in generated header tests
christianhelle dd23850
Document auto-generated header in README
christianhelle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| const std = @import("std"); | ||
| const version_info = @import("build_info"); | ||
|
|
||
| pub fn render(allocator: std.mem.Allocator, version: []const u8, timestamp: []const u8) ![]const u8 { | ||
| return try std.fmt.allocPrint(allocator, | ||
| \\// <auto-generated> | ||
| \\// this code was generated by openapi2zig {s} on {s} | ||
| \\// changes to this file may cause incorrect behavior and will be lost if the code is regenerated | ||
| \\// </auto-generated> | ||
| \\ | ||
| , .{ version, timestamp }); | ||
| } | ||
|
|
||
| pub fn renderNow(allocator: std.mem.Allocator, io: std.Io, version: []const u8) ![]const u8 { | ||
| const timestamp = try formatUtcTimestamp(allocator, io); | ||
| defer allocator.free(timestamp); | ||
| return try render(allocator, version, timestamp); | ||
| } | ||
|
|
||
| pub fn renderNowFromBuildInfo(allocator: std.mem.Allocator, io: std.Io) ![]const u8 { | ||
| const version = try std.fmt.allocPrint(allocator, "{s} ({s})", .{ version_info.VERSION, version_info.GIT_COMMIT }); | ||
| defer allocator.free(version); | ||
| return try renderNow(allocator, io, version); | ||
| } | ||
|
|
||
| fn formatUtcTimestamp(allocator: std.mem.Allocator, io: std.Io) ![]const u8 { | ||
| const now = std.Io.Clock.real.now(io); | ||
| const epoch_seconds = std.time.epoch.EpochSeconds{ .secs = @as(u64, @intCast(now.toSeconds())) }; | ||
| const epoch_day = epoch_seconds.getEpochDay(); | ||
| const day_seconds = epoch_seconds.getDaySeconds(); | ||
| const year_day = epoch_day.calculateYearDay(); | ||
| const month_day = year_day.calculateMonthDay(); | ||
|
|
||
| return try std.fmt.allocPrint(allocator, "{d}-{d:0>2}-{d:0>2} {d:0>2}:{d:0>2}:{d:0>2} UTC", .{ | ||
| year_day.year, | ||
| month_day.month.numeric(), | ||
| month_day.day_index + 1, | ||
| day_seconds.getHoursIntoDay(), | ||
| day_seconds.getMinutesIntoHour(), | ||
| day_seconds.getSecondsIntoMinute(), | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| const std = @import("std"); | ||
| const generated_header = @import("../generators/generated_header.zig"); | ||
| const openapi2zig = @import("../lib.zig"); | ||
| const test_utils = @import("test_utils.zig"); | ||
|
|
||
| test "render emits expected header" { | ||
| var gpa = test_utils.createTestAllocator(); | ||
| const allocator = gpa.allocator(); | ||
| defer std.debug.assert(gpa.deinit() == .ok); | ||
|
|
||
| const header = try generated_header.render(allocator, "0.6.0 (a1b2c3d)", "2026-07-12 21:49:56 UTC"); | ||
| defer allocator.free(header); | ||
|
|
||
| try std.testing.expectEqualStrings( | ||
| \\// <auto-generated> | ||
| \\// this code was generated by openapi2zig 0.6.0 (a1b2c3d) on 2026-07-12 21:49:56 UTC | ||
| \\// changes to this file may cause incorrect behavior and will be lost if the code is regenerated | ||
| \\// </auto-generated> | ||
| \\ | ||
| , header); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| test "generateCode output includes generated header" { | ||
| var gpa = test_utils.createTestAllocator(); | ||
| const allocator = gpa.allocator(); | ||
| defer std.debug.assert(gpa.deinit() == .ok); | ||
|
|
||
| const json = | ||
| \\{ | ||
| \\ "openapi": "3.0.0", | ||
| \\ "info": { "title": "fixture", "version": "1.0.0" }, | ||
| \\ "paths": {} | ||
| \\} | ||
| ; | ||
|
|
||
| var unified = try openapi2zig.parseToUnified(allocator, json); | ||
| defer unified.deinit(allocator); | ||
|
|
||
| const code = try openapi2zig.generateCode(allocator, std.testing.io, unified, .{ | ||
| .input_path = "fixture.json", | ||
| .models_only = true, | ||
| }); | ||
| defer allocator.free(code); | ||
|
|
||
| try std.testing.expect(std.mem.indexOf(u8, code, "this code was generated by openapi2zig") != null); | ||
| try std.testing.expect(std.mem.indexOf(u8, code, "changes to this file may cause incorrect behavior") != null); | ||
| try std.testing.expect(std.mem.indexOf(u8, code, "will be lost if the code is regenerated") != null); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.