From 0a474a6e86c56cdbeaedcb3e5eb31f95f5646733 Mon Sep 17 00:00:00 2001 From: omdxp Date: Thu, 20 Jun 2024 21:24:25 +0200 Subject: [PATCH 1/3] make schemas static --- d/build/source/app.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/d/build/source/app.d b/d/build/source/app.d index 7b0349e..6c90317 100644 --- a/d/build/source/app.d +++ b/d/build/source/app.d @@ -86,7 +86,7 @@ void saveToFile(const Schema schema, string path) @trusted // replace / with _ in filePath const auto filePathUnderscored = filePath.replace("/", "_"); File file = File(DATA_FILE, "a"); - file.writeln("immutable Schema ", filePathUnderscored, " = Schema("); + file.writeln("static Schema ", filePathUnderscored, " = Schema("); file.writeln(" Name(\"", schema.name.ar, "\", \"", schema.name.en, "\", \"", schema.name.fr, "\"),"); file.writeln(" Type.", schema.type, ","); if (schema.terms.isNull) From 59ed2f28440aa91ba32e8be280eb6e08538f7662 Mon Sep 17 00:00:00 2001 From: omdxp Date: Thu, 20 Jun 2024 22:14:10 +0200 Subject: [PATCH 2/3] impl getNodeByPath api --- d/build/source/app.d | 58 +++++++++++++++++++++++++++++++++------- d/source/kuliya/kuliya.d | 46 ++++++++++++++++++++++++++++++- 2 files changed, 93 insertions(+), 11 deletions(-) diff --git a/d/build/source/app.d b/d/build/source/app.d index 6c90317..0c8cacb 100644 --- a/d/build/source/app.d +++ b/d/build/source/app.d @@ -18,6 +18,10 @@ const string DATA_PATH = "../../_data/"; Data D file to save the schema +/ const string DATA_FILE = "../source/kuliya/data.d"; +/++ +Schemas with file paths ++/ +SchemaWithFilePath[] schemas; /++ Kuliya types @@ -72,6 +76,16 @@ struct Schema Nullable!Terms terms; } +/++ +Schema with file path ++/ +struct SchemaWithFilePath +{ + const Schema schema; + string filePath; + string filePathUnderscored; +} + /++ Save schema to file Params: @@ -85,18 +99,20 @@ void saveToFile(const Schema schema, string path) @trusted const auto filePath = pathParts[3 .. $ - 1].join("/"); // replace / with _ in filePath const auto filePathUnderscored = filePath.replace("/", "_"); + // save schema with file path + schemas ~= SchemaWithFilePath(schema, filePath, filePathUnderscored); + // save schema to data file File file = File(DATA_FILE, "a"); file.writeln("static Schema ", filePathUnderscored, " = Schema("); file.writeln(" Name(\"", schema.name.ar, "\", \"", schema.name.en, "\", \"", schema.name.fr, "\"),"); file.writeln(" Type.", schema.type, ","); if (schema.terms.isNull) { - file.writeln(" Terms.init"); + file.writeln(" null"); } else { - // file.writeln(" Nullable!Terms({", schema.terms.get.perYear, ", [", schema.terms.get.slots.join(", "), "]});"); // no propery join on const(int[]) error - file.write(" Terms(", schema.terms.get.perYear, ", [", schema + file.write(" new Terms(", schema.terms.get.perYear, ", [", schema .terms.get.slots[0]); foreach (i, slot; schema.terms.get.slots[1 .. $]) { @@ -156,8 +172,6 @@ void prepareDataFile() @trusted file.writeln("// This is an auto generated file, do not edit it!"); file.writeln("module kuliya.data;"); file.writeln(); - file.writeln("import std.typecons : Nullable;"); - file.writeln(); file.writeln("/++"); file.writeln("Kuliya types"); file.writeln("+/"); @@ -207,13 +221,36 @@ void prepareDataFile() @trusted file.writeln(" /// Type"); file.writeln(" Type type;"); file.writeln(" /// Terms"); - file.writeln(" Nullable!Terms terms;"); + file.writeln(" Terms* terms;"); + file.writeln("}"); file.writeln(); - file.writeln(" this(Name name, Type type, Terms terms)"); + file.close(); +} + +/++ +Save to data the implementation of the getNodeByPath function ++/ +void saveGetNodeByPath() @trusted +{ + File file = File(DATA_FILE, "a"); + file.writeln("/++"); + file.writeln("Get node by path"); + file.writeln("Params:"); + file.writeln(" path = Path of the node"); + file.writeln("Returns:"); + file.writeln(" Schema"); + file.writeln("+/"); + file.writeln("Schema* _getNodeByPath(string path)"); + file.writeln("{"); + file.writeln(" switch (path)"); file.writeln(" {"); - file.writeln(" this.name = name;"); - file.writeln(" this.type = type;"); - file.writeln(" this.terms = Nullable!Terms(terms);"); + foreach (schema; schemas) + { + file.writeln(" case \"", schema.filePath, "\":"); + file.writeln(" return &", schema.filePathUnderscored, ";"); + } + file.writeln(" default:"); + file.writeln(" return null;"); file.writeln(" }"); file.writeln("}"); file.writeln(); @@ -228,6 +265,7 @@ void main() @trusted remove(DATA_FILE); prepareDataFile(); walkDirs(DATA_PATH); + saveGetNodeByPath(); } catch (Exception e) { diff --git a/d/source/kuliya/kuliya.d b/d/source/kuliya/kuliya.d index 49f70f5..4507d5c 100644 --- a/d/source/kuliya/kuliya.d +++ b/d/source/kuliya/kuliya.d @@ -1,6 +1,50 @@ module kuliya.kuliya; +import kuliya.data; + +public +{ + /++ + Get kuliya schema node by a given path. + Params: + path = Kuliya path based on the location for `info.json` file in `_data/` folder (.eg `"usto/fmi"`). + Returns: + Pointer to kuliya schema if the path exists. + +/ + Schema* getNodeByPath(string path) + { + return _getNodeByPath(path); + } +} + +/// Get existing schema by path +unittest +{ + Schema* schema = getNodeByPath("umkb"); + assert(schema !is null); + assert(schema.name.ar == "جامعة محمد خيضر بسكرة"); + assert(schema.name.en == "University of Mohamed Khider Biskra"); + assert(schema.name.fr == "Université Mohamed Khider Biskra"); + assert(schema.type == Type.UNIVERSITY); +} + +/// Get null for non-existing schema by path +unittest +{ + Schema* schema = getNodeByPath("does/not/exist"); + assert(schema is null); +} + +/// Get Schema with terms by path unittest { - assert(1 == 1); + Schema* schema = getNodeByPath("umkb/fst/dee/sec"); + assert(schema !is null); + assert(schema.terms !is null); + assert(schema.terms.perYear == 2); + assert(schema.terms.slots.length == 4); + assert(schema.terms.slots[0] == 7); + assert(schema.terms.slots[1] == 8); + assert(schema.terms.slots[2] == 9); + assert(schema.terms.slots[3] == 10); } From b2f56f11ad8a1c99258aafd297002385b2272453 Mon Sep 17 00:00:00 2001 From: omdxp Date: Thu, 20 Jun 2024 22:19:19 +0200 Subject: [PATCH 3/3] update d workflow --- .github/workflows/d-checks.yml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/workflows/d-checks.yml b/.github/workflows/d-checks.yml index aaa56a5..1f2b3db 100644 --- a/.github/workflows/d-checks.yml +++ b/.github/workflows/d-checks.yml @@ -14,8 +14,12 @@ jobs: - uses: actions/checkout@v4 - uses: dlang-community/setup-dlang@4c99aa991ce7d19dd3064de0a4f2f6b2f152e2d7 - - name: Build & Test + - name: Build data.d + run: | + cd d/build + dub + + - name: Run tests run: | cd d - dub build --compiler=$DC - dub test --compiler=$DC + dub test