Init
This commit is contained in:
13
.vscode/launch.json
vendored
Normal file
13
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Launch",
|
||||
"command": "odin run .",
|
||||
"cwd": "${workspaceFolder}"
|
||||
},
|
||||
]
|
||||
}
|
||||
36
helper_procs.odin
Normal file
36
helper_procs.odin
Normal file
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import sp "core:path/slashpath"
|
||||
import "core:crypto"
|
||||
import "core:encoding/uuid"
|
||||
import "core:os/os2"
|
||||
|
||||
get_data_directory :: proc() -> (result: string, error: os2.Error) {
|
||||
exe_dir, err := os2.get_executable_directory(context.allocator)
|
||||
if err != os2.General_Error.None {
|
||||
return "", error
|
||||
}
|
||||
|
||||
data_path := sp.join({exe_dir, "data"})
|
||||
if (!os2.is_directory(data_path))
|
||||
{
|
||||
mkdir_err := os2.make_directory(data_path)
|
||||
if mkdir_err != os2.General_Error.None
|
||||
{
|
||||
return "", mkdir_err
|
||||
}
|
||||
}
|
||||
|
||||
return data_path, os2.General_Error.None
|
||||
}
|
||||
|
||||
create_uuid :: proc() -> string {
|
||||
new_uuid: string
|
||||
|
||||
{
|
||||
context.random_generator = crypto.random_generator()
|
||||
new_uuid = uuid.to_string_allocated(uuid.generate_v7())
|
||||
}
|
||||
|
||||
return new_uuid
|
||||
}
|
||||
27
main.odin
Normal file
27
main.odin
Normal file
@@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import "core:fmt"
|
||||
import "core:os"
|
||||
|
||||
json_path :: "test_dataset.json"
|
||||
|
||||
|
||||
main :: proc() {
|
||||
test_dataset := create_test_dataset()
|
||||
write_dataset_to_json(test_dataset, json_path)
|
||||
|
||||
parsed_dataset, ok := parse_json_to_dataset(json_path)
|
||||
if !ok {
|
||||
fmt.eprintfln("Failed to parse json tto dataset")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.println("Dataset:", parsed_dataset.name)
|
||||
for szob in parsed_dataset.objects {
|
||||
fmt.println("Object:", szob.id)
|
||||
fmt.println("Fields:")
|
||||
for field in szob.fields {
|
||||
fmt.println(" ", field.id, ":", field.type, "=", field.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
5
schema.json
Normal file
5
schema.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "Session Zero Core Items",
|
||||
"uuid": "12345-12345-12345",
|
||||
"id": "sz-core"
|
||||
}
|
||||
55
sz_parser.odin
Normal file
55
sz_parser.odin
Normal file
@@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import sz "szlib"
|
||||
|
||||
import "core:encoding/json"
|
||||
import "core:fmt"
|
||||
import "core:os"
|
||||
|
||||
|
||||
write_dataset_to_json :: proc(dataset: sz.Dataset, json_path: string) -> (ok: bool) {
|
||||
|
||||
json_data, err := json.marshal(dataset, {pretty = true, use_enum_names = true})
|
||||
if err != nil {
|
||||
fmt.eprintfln("Unable to marshal JSON: %v", err)
|
||||
return false
|
||||
}
|
||||
|
||||
werr := os.write_entire_file_or_err(json_path, json_data)
|
||||
if werr != nil {
|
||||
fmt.eprintfln("Unable to write JSON: %v", err)
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
parse_json_to_dataset :: proc(json_path: string) -> (result: sz.Dataset, ok: bool) {
|
||||
dataset: sz.Dataset
|
||||
|
||||
data, data_ok := os.read_entire_file_from_filename(json_path)
|
||||
if !data_ok {
|
||||
fmt.eprintln("Failed to load the file")
|
||||
return dataset, false
|
||||
}
|
||||
defer delete(data)
|
||||
|
||||
json_data, err := json.parse(data)
|
||||
if err != .None {
|
||||
fmt.eprintln("Failed to parse JSON file.")
|
||||
fmt.eprintln("Error: ", err)
|
||||
return dataset, false
|
||||
}
|
||||
defer json.destroy_value(json_data)
|
||||
|
||||
root := json_data.(json.Object)
|
||||
|
||||
error := json.unmarshal(data, &dataset)
|
||||
if error != nil {
|
||||
fmt.eprintln("Could not unmarshal json data to Dataset:", error)
|
||||
return dataset, false
|
||||
}
|
||||
|
||||
return dataset, true
|
||||
}
|
||||
37
szlib/data.odin
Normal file
37
szlib/data.odin
Normal file
@@ -0,0 +1,37 @@
|
||||
package szlib
|
||||
|
||||
Dataset :: struct {
|
||||
name: string,
|
||||
id: string,
|
||||
uuid: string,
|
||||
data_object_type: string,
|
||||
objects: [dynamic]SzDataObject
|
||||
}
|
||||
|
||||
SzDataObject :: struct {
|
||||
name: string,
|
||||
id: string,
|
||||
fields: [dynamic]SzField
|
||||
}
|
||||
|
||||
SzField :: struct {
|
||||
id: string,
|
||||
type: SzFieldType,
|
||||
is_list: bool,
|
||||
value: string,
|
||||
}
|
||||
|
||||
SzDataObjectTemplate :: struct {
|
||||
|
||||
}
|
||||
|
||||
SzTemplateField :: struct {
|
||||
id: string,
|
||||
type: SzFieldType,
|
||||
is_list: bool,
|
||||
default_value: string,
|
||||
}
|
||||
|
||||
SzFieldType :: enum {
|
||||
TEXT, NUMBER, BOOL, FORMULA, REF
|
||||
}
|
||||
26
test_dataset.json
Normal file
26
test_dataset.json
Normal file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"name": "Test Dataset",
|
||||
"id": "test_dataset",
|
||||
"uuid": "019bd8f5-7e61-7cac-b4b6-630aba8721a8",
|
||||
"data_object_type": "items",
|
||||
"objects": [
|
||||
{
|
||||
"name": "Test Item",
|
||||
"id": "test-item",
|
||||
"fields": [
|
||||
{
|
||||
"id": "cost",
|
||||
"type": "NUMBER",
|
||||
"is_list": false,
|
||||
"value": "100"
|
||||
},
|
||||
{
|
||||
"id": "some_string",
|
||||
"type": "TEXT",
|
||||
"is_list": false,
|
||||
"value": "This is some text"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
40
tests.odin
Normal file
40
tests.odin
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import sz "szlib"
|
||||
|
||||
create_test_dataset :: proc() -> sz.Dataset {
|
||||
|
||||
test_dataset: sz.Dataset = {
|
||||
name = "Test Dataset",
|
||||
id = "test_dataset",
|
||||
uuid = create_uuid(),
|
||||
data_object_type = "items",
|
||||
objects = make([dynamic]sz.SzDataObject)
|
||||
}
|
||||
|
||||
test_dataset_object: sz.SzDataObject = {
|
||||
id = "test-item",
|
||||
name = "Test Item",
|
||||
fields = make([dynamic]sz.SzField)
|
||||
}
|
||||
|
||||
test_field: sz.SzField = {
|
||||
id = "cost",
|
||||
type = .NUMBER,
|
||||
is_list = false,
|
||||
value = "100"
|
||||
}
|
||||
|
||||
test_field2: sz.SzField = {
|
||||
id = "some_string",
|
||||
type = .TEXT,
|
||||
is_list = false,
|
||||
value = "This is some text"
|
||||
}
|
||||
|
||||
append(&test_dataset_object.fields, test_field)
|
||||
append(&test_dataset_object.fields, test_field2)
|
||||
append(&test_dataset.objects, test_dataset_object)
|
||||
|
||||
return test_dataset
|
||||
}
|
||||
Reference in New Issue
Block a user