Re: Json output to container

2020-10-30 Thread Paul Backus via Digitalmars-d-learn

On Friday, 30 October 2020 at 19:07:20 UTC, Vino wrote:


Requirement: parse the json string and store the output to a 
container.


From,
Vino.B


Here's a working version of the code from your original post:

import asdf : parseJson;
import std.algorithm;
import std.container.array;
import std.stdio : writeln;
import std.typecons : Tuple, tuple;

void main()
{
string apidata1 = `{"items": [
{"name":"T01","hostname":"test01","pool":"Development"},
{"name":"T02","hostname":"test02","pool":"Quality"},
{"name":"T03","hostname":"test03","pool":"Production"}
]}`;

Array!(Tuple!(string, string, string)) data =
parseJson(apidata1)["items"]
.byElement
.map!(item => tuple(
item["name"].get!string("default"),
item["hostname"].get!string("default"),
item["pool"].get!string("default")
));

writeln(data[]);
}


Re: Json output to container

2020-10-30 Thread Vino via Digitalmars-d-learn

On Friday, 30 October 2020 at 19:07:20 UTC, Vino wrote:

On Friday, 30 October 2020 at 18:41:35 UTC, Paul Backus wrote:

On Friday, 30 October 2020 at 10:23:22 UTC, Vino wrote:

Hi,

  Request your help on the below code

[...]

 .filter!(a => a.(["items"].byElement))


What exactly are you trying to accomplish with this 
`a.(stuff)` syntax? As I'm sure you've discovered, it is not 
valid D, but it appears in several places in your code.


Hi,

 I was just trying the example code in the link 
https://code.dlang.org/packages/asdf, it is just an example, 
hence request your help to correct me.


Requirement: parse the json string and store the output to a 
container.


From,
Vino.B


Requirnment :

string apidata1 = `{"items":
  [
{"name":"T01","hostname":"test01"},
{"name":"T02","hostname":"test02"},
{"name":"T03","hostname":"test03"}
  ]
  }`;

 string apidata2 = `{"items":
  [
{"hostname":"test01","type":"Development"},
{"hostname":"test02","type":"Quality"},
{"hostname":"test03","type":"Production"}
  ]
  }`;

 string apidata2 = `{"items":
  [
{"type":"Development","Location":"L1"},
{"type":"Quality","Location":"L2"},
{type":"Production","Location":"L3"}
  ]
  }`;

1. Parse the apidata1(name,hostname)
2. For each "hostname" get the "type" data from api2(type)
3. For each "type" get the "Location: from api3(Location)
4. And store the result in a container with data 
(Name,Hostname,Type,Location)


From,
Vino.B



Re: Json output to container

2020-10-30 Thread Vino via Digitalmars-d-learn

On Friday, 30 October 2020 at 18:41:35 UTC, Paul Backus wrote:

On Friday, 30 October 2020 at 10:23:22 UTC, Vino wrote:

Hi,

  Request your help on the below code

[...]

 .filter!(a => a.(["items"].byElement))


What exactly are you trying to accomplish with this `a.(stuff)` 
syntax? As I'm sure you've discovered, it is not valid D, but 
it appears in several places in your code.


Hi,

 I was just trying the example code in the link 
https://code.dlang.org/packages/asdf, it is just an example, 
hence request your help to correct me.


Requirement: parse the json string and store the output to a 
container.


From,
Vino.B





Re: Json output to container

2020-10-30 Thread Paul Backus via Digitalmars-d-learn

On Friday, 30 October 2020 at 10:23:22 UTC, Vino wrote:

Hi,

  Request your help on the below code

[...]

 .filter!(a => a.(["items"].byElement))


What exactly are you trying to accomplish with this `a.(stuff)` 
syntax? As I'm sure you've discovered, it is not valid D, but it 
appears in several places in your code.


Re: Json output to container

2020-10-30 Thread Vino via Digitalmars-d-learn

On Friday, 30 October 2020 at 17:56:22 UTC, Andre Pany wrote:

On Friday, 30 October 2020 at 10:23:22 UTC, Vino wrote:

Hi,

  Request your help on the below code

Code:

import asdf: parseJson;
import std.algorithm;
import std.container.array;
import std.stdio: writeln;
import std.typecons: Tuple, tuple;

void main()
{
  string apidata1 = `{"items":
  [
{"name":"T01","hostname":"test01","pool":"Development"},
{"name":"T02","hostname":"test02","pool":"Quality"},
{"name":"T03","hostname":"test03","pool":"Production"}
  ]
  }`;
  auto data = Array!(Tuple!(string, string, string)) 
(parseJson(apidata1)

 .filter!(a => a.(["items"].byElement))
 .map!(a => 
tuple(a.(["name"].get!string("default")), 
a.(["hostname"].get!string("default")), 
a.(["pool"].get!string("default");

  writeln(data[]);
}

From,
Vino.B


What do you want to achieve and what is the problem with the 
code you posted?


Kind regards
Andre


Hi Andre,

   We wanted to store the output of the json data in array 
container, the main goal of this request is that we are currently 
planning to migrate our existing code which was written in PHP to 
D, and our existing code deal's with many api calls with json 
output, so we are evaluating the feasibility, of various json 
package's available in D, one of the code in our existing project 
is as below


Example code:
api1 : http://test.com/server
This api1 contains data "id" and "servername"

api2 :  http://test.com/type
This api2 contains data "servername" and "type"

api3 :  http://test.com/catagore
This api3 contains data "type" and "category"

so we fetch the data using the above api's and store the data in 
PHP multi dimensional array like "ID", "ServerName", "Type" and 
"category"(temporary storage) and then we update the information 
in this array into MySQL table.


From,
Vino.B




Why is vibe.d json serializer/deserializer so complex?

2020-10-30 Thread Steven Schveighoffer via Digitalmars-d-learn
I was looking to report an enhancement request to vibe.data.json (filed 
here: https://github.com/vibe-d/vibe.d/issues/2490), and I started 
looking at the serialization code for vibe. It's really really 
complicated, and I'm just wondering if this is a case of 
overengineering, or if there's a logic behind making this so 
complicated. My iopipejson serializer is super simple comparatively.


Is there a benefit to having all the complexity? I know they support 
both json and bson, but I cannot follow the code very well, and I'm not 
sure what happens where, and which types are responsible for what.


-Steve


Re: Json output to container

2020-10-30 Thread Andre Pany via Digitalmars-d-learn

On Friday, 30 October 2020 at 10:23:22 UTC, Vino wrote:

Hi,

  Request your help on the below code

Code:

import asdf: parseJson;
import std.algorithm;
import std.container.array;
import std.stdio: writeln;
import std.typecons: Tuple, tuple;

void main()
{
  string apidata1 = `{"items":
  [
{"name":"T01","hostname":"test01","pool":"Development"},
{"name":"T02","hostname":"test02","pool":"Quality"},
{"name":"T03","hostname":"test03","pool":"Production"}
  ]
  }`;
  auto data = Array!(Tuple!(string, string, string)) 
(parseJson(apidata1)

 .filter!(a => a.(["items"].byElement))
 .map!(a => 
tuple(a.(["name"].get!string("default")), 
a.(["hostname"].get!string("default")), 
a.(["pool"].get!string("default");

  writeln(data[]);
}

From,
Vino.B


What do you want to achieve and what is the problem with the code 
you posted?


Kind regards
Andre


Re: How would I go about unittesting a vibe.d app?

2020-10-30 Thread rinfz via Digitalmars-d-learn

On Friday, 30 October 2020 at 13:26:59 UTC, rinfz wrote:

...


Seems I have got something working using the fluent-asserts-vibe 
package.


How would I go about unittesting a vibe.d app?

2020-10-30 Thread rinfz via Digitalmars-d-learn
For example, I am trying to test a route. Within the unittest I 
setup the server locally (note: is it possible to start the 
server once for all unit tests?), then call listenHTTP. After the 
listen call I make a client request to localhost with requestHTTP 
and I get the following error:


WARNING: HTTPClientResponse not fully processed before being 
finalized


The code for reference:

unittest {
// imports...
auto listener = setupServer(); // makes the listenHTTP call
scope(exit) listener.stopListening();

requestHTTP(
  environment["HOST"] ~ "/register",
  (scope req) {
req.method = HTTPMethod.POST;
req.writeFormBody(["foo": "bar"]);
  },
  (scope res) {
assert (res.statusCode == 200);
  },
);
}

Note: I am running tests using dub if that is relevant.


Re: What is the difference between enum and shared immutable?

2020-10-30 Thread Max Samukha via Digitalmars-d-learn

On Thursday, 29 October 2020 at 16:45:51 UTC, Ali Çehreli wrote:



immutable string p;

shared static this() {
  p = environment["PATH"];  // <-- Run time
}



Immutable static variables being implicitly non-thread-local is a 
design mistake. Thread-local immutable variables have the same 
right to exist as immutable class/struct instance fields.


This should be possible without hacks:

immutable int val; // thread-specific value

static this() {
val = ...;
}


...because it is logically the same as:

struct V {
immutable int val;
}

V val;

static this() {
val = V(...);
}


Json output to container

2020-10-30 Thread Vino via Digitalmars-d-learn

Hi,

  Request your help on the below code

Code:

import asdf: parseJson;
import std.algorithm;
import std.container.array;
import std.stdio: writeln;
import std.typecons: Tuple, tuple;

void main()
{
  string apidata1 = `{"items":
  [
{"name":"T01","hostname":"test01","pool":"Development"},
{"name":"T02","hostname":"test02","pool":"Quality"},
{"name":"T03","hostname":"test03","pool":"Production"}
  ]
  }`;
  auto data = Array!(Tuple!(string, string, string)) 
(parseJson(apidata1)

 .filter!(a => a.(["items"].byElement))
 .map!(a => tuple(a.(["name"].get!string("default")), 
a.(["hostname"].get!string("default")), 
a.(["pool"].get!string("default");

  writeln(data[]);
}

From,
Vino.B


Re: What is the difference between enum and shared immutable?

2020-10-30 Thread Daniel Kozak via Digitalmars-d-learn
On Thu, Oct 29, 2020 at 4:13 PM H. S. Teoh via Digitalmars-d-learn <
digitalmars-d-learn@puremagic.com> wrote:

>
> But why can't that be treated differently from explicitly writing @safe
> on a declaration?  I mean, yeah, it's easier to implement the compiler
> that way, but ease of implementation shouldn't count against proper
> language design!
>
>
> T
>
> --
> Doubt is a self-fulfilling prophecy.
>

But what about this:

shared {
some_ type some_var;
immutable int x = 1;
}

There are many ways to define it and last time when I was looking at how
this is implemented in D frontend it was implemented as a bitmask variable
and you have no context from where it comes.
So only way would be to disallow this within lexer which I do not see as a
good options