From d164cdddb5643244efddabfbe592b6daf3b1d1de Mon Sep 17 00:00:00 2001 From: Thomas la Cour Date: Thu, 8 Aug 2024 19:08:58 +0200 Subject: [PATCH 1/4] odin info json example --- json/odin_info/odin_info.odin | 50 +++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 json/odin_info/odin_info.odin diff --git a/json/odin_info/odin_info.odin b/json/odin_info/odin_info.odin new file mode 100644 index 0000000..8d209ad --- /dev/null +++ b/json/odin_info/odin_info.odin @@ -0,0 +1,50 @@ +/* +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" +import "core:strings" + +main :: proc() { + fmt.println("Odin builtin constants") + + path: string = len(os.args) > 1 ? os.args[1] : "odin_info.json" + + builder := strings.builder_make() + defer strings.builder_destroy(&builder) + + 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("Json") + mo: json.Marshal_Options = { + pretty = true, + use_enum_names = true, + } + err := json.marshal_to_builder(&builder, info, &mo) + assert(err == json.Marshal_Data_Error.None) + if len(builder.buf) != 0 { + json_data := builder.buf[:] + fmt.printfln("%s", json_data) + fmt.printfln("Writing: %s", path) + ok := os.write_entire_file(path, json_data) + if !ok {fmt.eprintln("Unable to write file")} + } + + fmt.println("Done.") +} From b99233467d837de83d2970e118924884a831c4c2 Mon Sep 17 00:00:00 2001 From: Thomas la Cour Date: Thu, 8 Aug 2024 19:18:37 +0200 Subject: [PATCH 2/4] as instructed wp :) --- .github/workflows/check.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 4bb25ab..246c78d 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -43,6 +43,7 @@ jobs: odin check glfw/window $FLAGS odin check json/load_json $FLAGS + odin check json/odin_info $FLAGS odin check learn_metal/00-window $FLAGS odin check learn_metal/01-primitive $FLAGS From 9666b4160e44846d0525ce520747206029bd31d8 Mon Sep 17 00:00:00 2001 From: Thomas la Cour Date: Thu, 29 Aug 2024 18:01:14 +0200 Subject: [PATCH 3/4] good suggestion from Laytan --- json/odin_info/odin_info.odin | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/json/odin_info/odin_info.odin b/json/odin_info/odin_info.odin index 8d209ad..6563e13 100644 --- a/json/odin_info/odin_info.odin +++ b/json/odin_info/odin_info.odin @@ -11,12 +11,9 @@ import "core:os" import "core:strings" main :: proc() { - fmt.println("Odin builtin constants") + fmt.println("Some of Odin's builtin constants") - path: string = len(os.args) > 1 ? os.args[1] : "odin_info.json" - - builder := strings.builder_make() - defer strings.builder_destroy(&builder) + path := len(os.args) > 1 ? os.args[1] : "odin_info.json" info: struct { ODIN_OS: runtime.Odin_OS_Type, @@ -28,23 +25,26 @@ main :: proc() { ODIN_DEBUG: bool, } = {ODIN_OS, ODIN_ARCH, ODIN_ENDIAN, ODIN_VENDOR, ODIN_VERSION, ODIN_ROOT, ODIN_DEBUG} - fmt.println("Odin") + fmt.println("Odin:") fmt.printfln("%#v", info) - fmt.println("Json") - mo: json.Marshal_Options = { + 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) } - err := json.marshal_to_builder(&builder, info, &mo) - assert(err == json.Marshal_Data_Error.None) - if len(builder.buf) != 0 { - json_data := builder.buf[:] - fmt.printfln("%s", json_data) - fmt.printfln("Writing: %s", path) - ok := os.write_entire_file(path, json_data) - if !ok {fmt.eprintln("Unable to write file")} + + fmt.printfln("%s", json_data) + fmt.printfln("Writing: %s", path) + werr := os.write_entire_file_or_err(path, json_data) + if werr != nil { + fmt.eprintfln("Unable to write file: %v", werr) + os.exit(1) } - fmt.println("Done.") + fmt.println("Done") } From 529e13da33ac92393a6cd79dd01404859f2bc9d6 Mon Sep 17 00:00:00 2001 From: Thomas la Cour Date: Thu, 29 Aug 2024 18:22:31 +0200 Subject: [PATCH 4/4] -vet -strict-style --- json/odin_info/odin_info.odin | 1 - 1 file changed, 1 deletion(-) diff --git a/json/odin_info/odin_info.odin b/json/odin_info/odin_info.odin index 6563e13..8b17700 100644 --- a/json/odin_info/odin_info.odin +++ b/json/odin_info/odin_info.odin @@ -8,7 +8,6 @@ import "base:runtime" import "core:encoding/json" import "core:fmt" import "core:os" -import "core:strings" main :: proc() { fmt.println("Some of Odin's builtin constants")