Skip to content
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

implement getNodeByPath D API #103

Merged
merged 3 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions .github/workflows/d-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
60 changes: 49 additions & 11 deletions d/build/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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("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)
{
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 .. $])
{
Expand Down Expand Up @@ -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("+/");
Expand Down Expand Up @@ -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();
Expand All @@ -228,6 +265,7 @@ void main() @trusted
remove(DATA_FILE);
prepareDataFile();
walkDirs(DATA_PATH);
saveGetNodeByPath();
}
catch (Exception e)
{
Expand Down
46 changes: 45 additions & 1 deletion d/source/kuliya/kuliya.d
Original file line number Diff line number Diff line change
@@ -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);
}
Loading