-
Notifications
You must be signed in to change notification settings - Fork 98
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
Add Vale #463
Comments
I did attempt this. I had to parse the json manually. This is how far I got: import stdlib.*;
import stdlib.path.*;
struct Post {
_id str;
title str;
tags List<str>;
}
func contains(haystack str, needle str) bool {
i = 0;
while i <= haystack.len() - needle.len() {
// Extract a substring of the same length as the needle
substring = haystack.slice(i, i + needle.len());
// Check if the substring matches the needle
if substring == needle {
return true; // Found a match
}
i = i + 1;
}
return false;
}
func combineTags(parts List<str>) List<str> {
isTitleOrID = (s str) => {s.contains("_id") or s.contains("title")};
result = List<str>();
allTags = "";
foreach s in parts {
if isTitleOrID(s) {
result.add(s);
}else {
allTags = allTags + s;
}
}
result.add(allTags);
return result;
}
func cleanUp(input str) str{
r1 = "{";
r2 = "}";
r3 = "\"";
r4 = "[";
r5 = "]";
return input.replaceAll(r1,"").replaceAll(r2,"").replaceAll(r3,"").replaceAll(r4,"").replaceAll(r5,"").trim();
}
func findValue(kvPairs List<(str, str)>, key str) str {
foreach [k, v] in kvPairs {
if k == key {
return v;
}
}
return "";
}
func parsePost(input str) Post{
elements_temp = input.split(",");
elements = combineTags(elements_temp);
kvpairs = List<(str, str)>();
foreach s in elements {
parts = s.split(":");
key = parts[0].cleanUp();
value = parts[1].cleanUp();
kvpairs.add((key, value));
}
tags = kvpair.findValue("tags").split(",").map((s) => s.trim());
return Post(kvpair.findValue("_id"), kvpair.findValue("title"), tags);
}
func parseJson(input str) List<Post> {
json = input.slice(1,input.len() - 1);
separator = """},""";
objects = json.split(separator);
result = List<Post>();
foreach s in objects {
result.add(parsePost(s));
}
return result;
}
exported func main() {
contents = readFileAsString("../demo.vale");
// trimmed = contents.slice(1, contents.len() - 1);
println( contents.len() );
jsonString = """
[
{
"_id": "h5cwwbua",
"title": "Recognize through morning miss guess cover sure.",
"tags": [
"mongodb",
"redis",
"svelte",
"tutorial"
]
},
{
"_id": "jf0da9ku",
"title": "Player anything recent democratic.",
"tags": [
"flask",
"java",
"react",
"spring"
]
}
]
""";
println( (&parseJson(jsonString)).len() );
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/ValeLang/Vale
The text was updated successfully, but these errors were encountered: