Skip to content

Commit

Permalink
except for non utf8 valid files all work
Browse files Browse the repository at this point in the history
  • Loading branch information
burner committed Dec 22, 2023
1 parent 9f6245b commit 1109c92
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 35 deletions.
6 changes: 2 additions & 4 deletions fakerjs2generator/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ import generator;
void main() {
writeln("Edit source/app.d to start your project.");

/*
foreach(d; dirEntries("faker/src/locale/", SpanMode.shallow)) {
string n = d.name;
ptrdiff_t s = n.lastIndexOf('/');
n = n[s + 1 .. $ - 3];
writeln(n);
Language en = parseLanguage(n);
}
*/

Language en = parseLanguage("en");
generate(en, "en");
//Language en = parseLanguage("en");
//generate(en, "en");
}
17 changes: 9 additions & 8 deletions fakerjs2generator/source/defis.d
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module defis;

import std.typecons : Nullable;
import std.sumtype;

struct Mustache {
Expand Down Expand Up @@ -161,7 +162,7 @@ struct LocationFolder {
string[] city_prefix;
string[] city_suffix;
string[] country;
string[] county;
SumType!(ForwardToOther,string[]) county;
string[] default_country;
string[] direction;
string[] direction_abbr;
Expand All @@ -185,25 +186,25 @@ struct MusicFolder {
}

struct Title {
string[] descriptor;
string[] level;
string[] job;
Nullable!(string[]) descriptor;
Nullable!(string[]) level;
Nullable!(string[]) job;
}

struct PersonFolder {
string[] bio_part;
Mustache[] bio_pattern;
string[] bio_supporter;
string[] female_first_name;
SumType!(string[],ForwardToOther) female_middle_name;
SumType!(ForwardToOther,string[]) female_middle_name;
string[] female_prefix;
SumType!(string[],MergeArray) first_name;
SumType!(MergeArray,string[]) first_name;
string[] gender;
Mustache[] job_title_pattern;
SumType!(string[],MergeArray) last_name;
SumType!(MergeArray,string[]) last_name;
MustacheWeight[] last_name_pattern;
string[] male_first_name;
SumType!(string[],ForwardToOther) male_middle_name;
SumType!(ForwardToOther,string[]) male_middle_name;
string[] male_prefix;
string[] middle_name;
MustacheWeight[] name;
Expand Down
32 changes: 26 additions & 6 deletions fakerjs2generator/source/generator.d
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,28 @@ void traverse(T,Out)(T t, ref Out o, string[] path) {
genStringArray(t, o, path);
} else static if(is(T == Number[])) {
//genNumberArray(t, o, path);
} else static if(is(T == ForwardToOther)) {
writeln("ForwardToOther");
genForwardToOther(t, o, path);
} else static if(is(T == SumType!(TT), TT...)) {
static foreach(R; TT) {
t.match!(
(R r) { writeln("SumType " ~ R.stringof); traverse(r, o, path); }
, (_) { return; }
);
}
writeln("SumType");
enum m = buildForwarder!(TT);
pragma(msg, m);
mixin(m);
} else {
writefln("Unhandled %s", T.stringof);
}
}
}

void genForwardToOther(Out)(ForwardToOther fto, ref Out o, string[] path) {
auto app = appender!string();
iformat(app, 1, "string %s() {\n", pathToFuncName(path));
iformat(app, 2, "return %s();\n", fto.fwd);
iformat(app, 0, "}\n", fto.fwd);
writeln(app.data);
}

void genStringArray(Out)(string[] strs, ref Out o, string[] path) {
iformat(o, 1, "string %s() {\n", pathToFuncName(path));
iformat(o, 2, "const string[] strs =\n\t\t[ ");
Expand Down Expand Up @@ -101,3 +112,12 @@ void str80(Out)(ref Out o, string[] strs, size_t tabs) {
}
}
}

string buildForwarder(TT...)() {
string ret = "t.match!(";
static foreach(T; TT) {
ret ~= "\n\t(" ~ T.stringof ~ " h) { writefln(\"\\tMatch %s\", `" ~ T.stringof ~ "`); traverse(h, o, path); }, ";
}
ret ~= "\n);\n";
return ret;
}
75 changes: 58 additions & 17 deletions fakerjs2generator/source/parser.d
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module parser;

import std.array : array;
import std.algorithm.searching : endsWith, startsWith;
import std.ascii : isAlphaNum;
import std.algorithm.searching : all, endsWith, startsWith;
import std.algorithm.iteration : filter, joiner, map, splitter;
import std.exception : enforce;
import std.conv : to;
Expand All @@ -13,6 +14,7 @@ import std.stdio;
import std.typecons : Nullable, nullable;
import std.traits : FieldNameTuple, isArray, isIntegral, isSomeString;
import std.uni : toLower;
import std.utf : byDchar;
import std.sumtype;

import json2;
Expand Down Expand Up @@ -42,6 +44,8 @@ T parseFolder(T)(string[] path) {
__traits(getMember, ret, mem) = parseMergeArray(path ~ mem);
} else static if(is(MemType == ForwardToOther)) {
__traits(getMember, ret, mem) = parseForwardToOther(path ~ mem);
} else static if(is(MemType == Number[])) {
//writeln("Number[]");
} else {
__traits(getMember, ret, mem) = parseStruct!(MemType)(path ~ mem);
}
Expand Down Expand Up @@ -112,9 +116,10 @@ MergeArray parseMergeArray(string[] path) {
enforce(s.endsWith(post)
, format("not mergeArray postfix '%s'", s));
s = s[pre.length .. $ - post.length];
return MergeArray(s.splitter(",")
ret = MergeArray(s.splitter(",")
.map!(it => it.strip())
.array);
return ret;
}
}

Expand Down Expand Up @@ -153,13 +158,27 @@ ForwardToOther parseForwardToOther(string[] path) {
string f = openAndTrimFile(path);
ForwardToOther ret;
if(!f.empty) {
ret.fwd = f.strip();
string t = f.strip();
t = t.strip(";");
t = t.strip();
bool isValidFwd = t.byDchar().all!(c => isAlphaNum(c) || c == '_');
if(t.length < 16) {
writefln("%s %s", t, isValidFwd);
}
if(isValidFwd) {
ret.fwd = t;
return ret;
}
}
return ret;
}

T parseStruct(T)(JSONValue j) {
static if(isArray!(T) && !isSomeString!(T)) {
static if(is(T == Nullable!F, F)) {
return j.isNull()
? T.init
: nullable(parseStruct!(F)(j));
} else static if(isArray!(T) && !isSomeString!(T)) {
enforce(j.type == JSONType.array
, format("Not an array but %s", j.toPrettyString()));
alias ET = ElementEncodingType!(T);
Expand Down Expand Up @@ -204,18 +223,25 @@ T parseStruct(T)(JSONValue j) {
enforce(j.type == JSONType.object
, format("expected an object got '%s'", j.toPrettyString()));
JSONValue* mPtr = memJS in j;
enforce(mPtr !is null
, format("%s not in '%s'", memJS, j.toPrettyString()));

alias MemType = typeof(__traits(getMember, T, mem));
static if(is(MemType == string)) {
__traits(getMember, ret, mem) = (*mPtr).get!string();
} else static if(is(MemType == SumType!(TT), TT...)) {
writeln("SumType ", T.stringof);
} else static if(isIntegral!(MemType)) {
__traits(getMember, ret, mem) = (*mPtr).get!int();
static if(is(MemType == Nullable!F, F)) {
if(mPtr !is null) {
__traits(getMember, ret, mem) = parseStruct!(F)(*mPtr);
}
} else {
__traits(getMember, ret, mem) = parseStruct!(MemType)(*mPtr);
enforce(mPtr !is null
, format("%s not in '%s'", memJS, j.toPrettyString()));

static if(is(MemType == string)) {
__traits(getMember, ret, mem) = (*mPtr).get!string();
} else static if(is(MemType == SumType!(TT), TT...)) {
writeln("SumType ", T.stringof);
} else static if(isIntegral!(MemType)) {
__traits(getMember, ret, mem) = (*mPtr).get!int();
} else {
__traits(getMember, ret, mem) = parseStruct!(MemType)(*mPtr);
}
}
}}
return ret;
Expand All @@ -224,14 +250,29 @@ T parseStruct(T)(JSONValue j) {

SumType!(TT) parseSumType(TT...)(string[] path) {
SumType!(TT) ret;
string f = openAndTrimFile(path);
static foreach(T; TT) {
try {
static if(is(T == string[])) {
ret = parseStringArray(path);
} else static if(is(MemType == ForwardToOther)) {
ret = parseForwardToOther(path);
string[] r = parseStringArray(path);
if(!r.empty) {
ret = r;
return ret;
}
} else static if(is(T == ForwardToOther)) {
ForwardToOther r = parseForwardToOther(path);
if(!r.fwd.empty) {
ret = r;
return ret;
}
} else static if(is(T == MergeArray)) {
ret = parseMergeArray(path);
MergeArray r = parseMergeArray(path);
if(!r.arrayNames.empty) {
ret = r;
return ret;
}
} else {
static assert(false, T.stringof ~ " Not Handled");
}
} catch(Exception e) {
}
Expand Down

0 comments on commit 1109c92

Please sign in to comment.