Re: merging container arrays

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

On Saturday, 31 October 2020 at 15:16:22 UTC, Vino wrote:

Hi All,

   Request your help on the below code, the requirement is that 
result's are stored in one single container.


Code:
import asdf;
import std.algorithm: map;
import std.container.array;
import std.stdio: writeln;
import std.typecons: Tuple, tuple;
import std.range: lockstep;

auto api1()
{
 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)) data1 = 
parseJson(apidata1)["items"].byElement

 .map!(item => tuple(
 
item["name"].get!string("default"),
 
item["hostname"].get!string("default"),
 
item["pool"].get!string("default")

 ));
 return data1[];
}

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

 Array!(Tuple!(string, string)) data2 = 
parseJson(apidata2)["items"].byElement

 .map!(item => tuple(
 
item["hostname"].get!string("default"),
 
item["type"].get!string("default")

 ));
 return data2[];
}

auto api3()
{
 string apidata3 = `{"items": [
{"type":"Development","location":"L1"},
{"type":"Quality","location":"L2"},
{"type":"Production","location":"L3"}
  ]}`;

 Array!(Tuple!(string, string)) data3 = 
parseJson(apidata3)["items"].byElement

.map!(item => tuple(

item["type"].get!string("default"),

item["location"].get!string("default")

 ));
 return data3[];
}



void main()
{
 auto apidata1 = api1;
 auto apidata2 = api2;
 auto apidata3 = api3;
 if(!apidata1.empty) {
foreach(ref x , y; lockstep(apidata1[], apidata2[])) {
   if(x[1] == y[0]){
  writeln(y[1]);  // the output needs to be merged with 
apidata1

   }
}
 }
 if(!apidata2.empty) {
foreach(ref x, y; lockstep(apidata2[], apidata3[])) {
   if(x[1] == y[0]) {
  writeln(y[1]); // the output needs to be merged with 
apidata1

   }
}
 }
 writeln(apidata1[]); // Should 
contain(name,hostname,pool,type,location)

}

From,
Vino.B


Hi All,

   I was able to find a solution using a different approach, the 
code is in the link
https://run.dlang.io/is/Jx4NLw, request your help on to get the 
value using the Key


In PHP we can get the value of the associative array using the 
key as below


PHP:
foreach($array as $k) { print_r($k["Name"]); }

So request you help on how the same in d

Tired the below , no luck

1 > foreach(i; data.byKey) { writeln(i["Name"]); }

Error:
onlineapp.d(53,37): Error: cannot implicitly convert expression 
`"Name"` of type `string` to `ulong`


2 > foreach (ref i; data) writeln(i["Name"]);
3 > foreach(i; data.byValue) { writeln(i["Name"]); }
4 > foreach(i; data) { writeln(get(i["Name"])); }

Error:
onlineapp.d(53,38): Error: function 
`std.container.array.Array!string.Array.opIndex(ulong i) inout` 
is not callable using argument types `(string)`
onlineapp.d(53,38):cannot pass argument `"Name"` of type 
`string` to parameter `ulong i`


From,
Vino.B



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

2020-10-31 Thread Jesse Phillips via Digitalmars-d-learn

On Saturday, 31 October 2020 at 22:42:20 UTC, James Blachly wrote:

So I've been meaning to ask this as I have been learning Rust 
off-and-on recently for web development, and was impressed by 
the traits functionality. In particular, with traits and some 
agreed upon API, many packages are interchangeable in terms of 
various functionalities, including JSON 
serialization/deserialization.


What would be the nearest analog facility in D -- supposing we 
could agree on a standard API -- to facilitate pluggable 
serializers?


I am a big fan of asdf (and Steve, haven't tried iopipejson 
yet, but will do). It would be nice to not rewrite code to try 
a different serializer, and Rust is really neat in this regard.


Well I was putting this together, but didn't want to attempt 
submission until I felt I would be able to put in the time for 
the review process


https://github.com/JesseKPhillips/DIPs/blob/serialize/attribute/DIPs/1NNN-jkp.md


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

2020-10-31 Thread Imperatorn via Digitalmars-d-learn

On Saturday, 31 October 2020 at 22:42:20 UTC, James Blachly wrote:

On 10/30/20 1:56 PM, Steven Schveighoffer wrote:

[...]


So I've been meaning to ask this as I have been learning Rust 
off-and-on recently for web development, and was impressed by 
the traits functionality. In particular, with traits and some 
agreed upon API, many packages are interchangeable in terms of 
various functionalities, including JSON 
serialization/deserialization.


What would be the nearest analog facility in D -- supposing we 
could agree on a standard API -- to facilitate pluggable 
serializers?


I am a big fan of asdf (and Steve, haven't tried iopipejson 
yet, but will do). It would be nice to not rewrite code to try 
a different serializer, and Rust is really neat in this regard.


An interface defined in the standard library


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

2020-10-31 Thread Adam D. Ruppe via Digitalmars-d-learn

On Saturday, 31 October 2020 at 22:42:20 UTC, James Blachly wrote:
be the nearest analog facility in D -- supposing we could agree 
on a standard API -- to facilitate pluggable serializers?


interfaces?

could even be informal interfaces where you just use the same 
function names


so something in the middle, like how isInputRange works.


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

2020-10-31 Thread James Blachly via Digitalmars-d-learn

On 10/30/20 1:56 PM, Steven Schveighoffer wrote:
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


So I've been meaning to ask this as I have been learning Rust off-and-on 
recently for web development, and was impressed by the traits 
functionality. In particular, with traits and some agreed upon API, many 
packages are interchangeable in terms of various functionalities, 
including JSON serialization/deserialization.


What would be the nearest analog facility in D -- supposing we could 
agree on a standard API -- to facilitate pluggable serializers?


I am a big fan of asdf (and Steve, haven't tried iopipejson yet, but 
will do). It would be nice to not rewrite code to try a different 
serializer, and Rust is really neat in this regard.




merging container arrays

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

Hi All,

   Request your help on the below code, the requirement is that 
result's are stored in one single container.


Code:
import asdf;
import std.algorithm: map;
import std.container.array;
import std.stdio: writeln;
import std.typecons: Tuple, tuple;
import std.range: lockstep;

auto api1()
{
 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)) data1 = 
parseJson(apidata1)["items"].byElement

 .map!(item => tuple(
 
item["name"].get!string("default"),
 
item["hostname"].get!string("default"),
 
item["pool"].get!string("default")

 ));
 return data1[];
}

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

 Array!(Tuple!(string, string)) data2 = 
parseJson(apidata2)["items"].byElement

 .map!(item => tuple(
 
item["hostname"].get!string("default"),
 
item["type"].get!string("default")

 ));
 return data2[];
}

auto api3()
{
 string apidata3 = `{"items": [
{"type":"Development","location":"L1"},
{"type":"Quality","location":"L2"},
{"type":"Production","location":"L3"}
  ]}`;

 Array!(Tuple!(string, string)) data3 = 
parseJson(apidata3)["items"].byElement

.map!(item => tuple(

item["type"].get!string("default"),

item["location"].get!string("default")

 ));
 return data3[];
}



void main()
{
 auto apidata1 = api1;
 auto apidata2 = api2;
 auto apidata3 = api3;
 if(!apidata1.empty) {
foreach(ref x , y; lockstep(apidata1[], apidata2[])) {
   if(x[1] == y[0]){
  writeln(y[1]);  // the output needs to be merged with 
apidata1

   }
}
 }
 if(!apidata2.empty) {
foreach(ref x, y; lockstep(apidata2[], apidata3[])) {
   if(x[1] == y[0]) {
  writeln(y[1]); // the output needs to be merged with 
apidata1

   }
}
 }
 writeln(apidata1[]); // Should 
contain(name,hostname,pool,type,location)

}

From,
Vino.B



Re: Json output to container

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

On Friday, 30 October 2020 at 19:33:43 UTC, Paul Backus wrote:

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

[...]


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;

[...]


Hi Paul,

  Thank you very much, your solution resolved most of our issues.

From,
Vino.B