-
Notifications
You must be signed in to change notification settings - Fork 34
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
How to write odin info as json #48
Conversation
There are a couple things I would change here:
End result: /*
Demonstrate how you can create a JSON file.
*/
package main
import "base:builtin"
import "base:runtime"
import "core:encoding/json"
import "core:fmt"
import "core:os"
main :: proc() {
fmt.println("Some of Odin's builtin constants")
path := len(os.args) > 1 ? os.args[1] : "odin_info.json"
info: struct {
ODIN_OS: runtime.Odin_OS_Type,
ODIN_ARCH: runtime.Odin_Arch_Type,
ODIN_ENDIAN: runtime.Odin_Endian_Type,
ODIN_VENDOR: string,
ODIN_VERSION: string,
ODIN_ROOT: string,
ODIN_DEBUG: bool,
} = {ODIN_OS, ODIN_ARCH, ODIN_ENDIAN, ODIN_VENDOR, ODIN_VERSION, ODIN_ROOT, ODIN_DEBUG}
fmt.println("Odin:")
fmt.printfln("%#v", info)
fmt.println()
fmt.println("JSON:")
json_data, err := json.marshal(info, {
pretty = true,
use_enum_names = true,
})
if err != nil {
fmt.eprintfln("Unable to marshal JSON: %v", err)
os.exit(1)
}
fmt.printfln("%s", json_data)
fmt.printfln("Writing: %s", path)
werr := os.write_entire_file_or_err(path, json_data)
if werr == nil {
fmt.println("Done")
} else {
fmt.eprintfln("Unable to write file: %v", werr)
os.exit(1)
}
} |
Tx for the feedback, looks cleaner :) |
Yeah it is a good idea for an example! Indeed the or_err variants came a few weeks ago, not sure if you had written this already. And yep you could write directly to the file through the writers, but you wouldn't be able to print it like you did here so I think this is fine as is. Writing directly to the file might also cause slow downs because you are doing a bunch of small writes/syscalls instead of writing to a memory buffer and then doing one write/syscall to the file. It is usually a trade-off between speed and memory usage and only really something to worry about on huge files. There is also |
Looks good. Can you remove the unused |
.github/workflows/check.yml
?