Should this Compile?

2017-10-03 Thread SamwiseFilmore via Digitalmars-d-learn
I've created toString() for a struct (which is a lot more 
complicated than what I've provided here) that returns a large 
number of concatenated strings. Here is the example:


struct Card {
// Flesh out these enums appropriately
CardSuit suit;
CardValue value;
Facing facing;

string toString() {
return
this.value.toString() ~
" of " ~
this.suit.toString() ~
this.suit == CardSuit.diamonds ? "" : "\t" ~
"\tfacing " ~
this.facing.toString();
}
}

This code does not compile with this error message:
Error: incompatible types for ((toString(this.p_value) ~ " of " ~ 
toString(this.p_suit)) ~ (this.p_suit)): 'string' and 'CardSuit'


Am I using the ternary operator correctly here, or is this an 
issue with dmd? I'm using dmd v2.076.0.


This is the ideal way to implement this function, although I 
could do it other ways.


Re: Should this Compile?

2017-10-03 Thread SamwiseFilmore via Digitalmars-d-learn

On Tuesday, 3 October 2017 at 22:37:17 UTC, SamwiseFilmore wrote:
Am I using the ternary operator correctly here, or is this an 
issue with dmd? I'm using dmd v2.076.0.


I wrapped the ternary in parentheses, and it compiled. Still, I'm 
wondering about this behavior.


Re: Should this Compile?

2017-10-03 Thread SamwiseFilmore via Digitalmars-d-learn
On Tuesday, 3 October 2017 at 23:13:00 UTC, Jonathan M Davis 
wrote:
On Tuesday, October 03, 2017 22:42:35 SamwiseFilmore via 
Digitalmars-d-learn wrote:
On Tuesday, 3 October 2017 at 22:37:17 UTC, SamwiseFilmore 
wrote:
> Am I using the ternary operator correctly here, or is this 
> an issue with dmd? I'm using dmd v2.076.0.


I wrapped the ternary in parentheses, and it compiled. Still, 
I'm wondering about this behavior.


Operator precedence makes it quite clear how the ternary 
operator is supposed to function, and if you're ever not sure, 
then put parens around it.


https://wiki.dlang.org/Operator_precedence

- Jonathan M Davis


Thanks, that clears things up. I appreciate it!


Issues with Vibe.d Dynamic HTML with JSON

2017-11-01 Thread SamwiseFilmore via Digitalmars-d-learn
I've got a serialized JSON structure that looks something like 
this:


{
"title": "Webpage title",
"major_categories": [
{
"title": "Major Category title",
"categories": [
{
"title": "Minor Category title",
"links": [
{
"url": "http://myurl.something.com";,
"label": "Text to display"
}
]
}
]
}
]
}

Of course the arrays have more elements (the entire file is about 
450 lines). My thought was to simply iterate through this 
structure using std.json and vibe.d/diet and basically build a 
webpage that way. My D get request handler is fairly simple:


void get() {
JSONValue json = parseJSON(new File("links.json", 
"r").readln('\x00'));

string title = json["title"].str;
JSONValue[] major_categories = json["major_categories"].array;
render!("index.dt", title, major_categories);
}

My Diet is a little more complex, with three layers of foreach 
loops:


doctype html
html
head
title #{title}
body
h1 #{title}

- foreach (major; major_categories)
h2 #{major["title"].str}
- foreach (minor; major["categories"].array)
h4 #{minor["title"].str}
p
- foreach (item; minor["links"].array)
a(href="#{item[\"url\"].str}") 
#{item["label"].str}

br

I realize all that is hard to read, sorry. :/

Anyway, my problem is that when I get about to line 320 of the 
JSON, the diet stops generating HTML. I've spit out the whole 
JSON run through the parser and back to string, and that has all 
the data, including the stuff that is cut out in the Diet. 
Basically I'm pretty sure it's not std.json (not 100% sure, since 
the operations I do in the diet are more complex, but 
essentially...). Does anyone have any insight to shine on this?


Re: Issues with Vibe.d Dynamic HTML with JSON

2017-11-02 Thread SamwiseFilmore via Digitalmars-d-learn

On Thursday, 2 November 2017 at 08:40:28 UTC, bauss wrote:
Do you get a response back with rendered html or does the 
connection get dropped?


No, the html does come in, and the whole content of the rendered 
page is sent to the browser. The page has closing head and body 
tags.


Have you tried to cut down the amount of data and see if it 
will render it all? That should eliminate whether it's the 
amount of data or what.


I'll play with that. Would there be any reason for my data to get 
randomly truncated?


Re: Issues with Vibe.d Dynamic HTML with JSON

2017-11-02 Thread SamwiseFilmore via Digitalmars-d-learn

On Thursday, 2 November 2017 at 18:51:09 UTC, bauss wrote:

On Thursday, 2 November 2017 at 18:48:10 UTC, bauss wrote:

Before you did:
render!("index.dt", title, major_categories);

Have you tried to check the contents of "major_categories" 
making sure it's all there. Just to figure out whether the 
problem is in the view rendering or the json parsing.


I checked, it seemed as though the JSON was all there.


Also alternatively have you tried "vibe.data.json"?

http://vibed.org/api/vibe.data.json/


I just converted my code to use vibe's library instead of the 
phobos Json implementation.


Now I'm not getting any output whatsoever. I'm wondering if it's 
an issue with Diet not wanting to do that many levels of nested 
iterations...


Also the vibe.data.json.Json.toString() method does something 
awful to that Json string. After I fix that I might be able to 
get more debugging info...


Here's my Diet. I basically just pass a Json object to render!:

doctype html
html
head
title #{json["title"].get!string}
body
- import std.stdio : writeln;
- import vibe.data.json : Json;
h1 #{json["title"].get!string}

- foreach (major; json["major_categories"].get!(Json[])) {
- string major_category_name = 
major["title"].get!string;

- major_category_name.writeln();
h2 #{major_category_name}
- foreach (minor; major["categories"].get!(Json[])) {
h4 #{minor["title"].get!string}
p
- foreach (item; minor["links"].get!(Json[])) 
{
a(href="#{item[\"url\"].get!string}") 
#{item["label"].get!string}

br
- }
- }
- }