Elm : How to Encode Nested objects - encode

Model
type alias Model {
name : String
, poi_coordinates : Coordinates
}
type alias Coordinates =
{
coord_type : String
, coordinates : List Float
}
poiFormEncoder : Model -> Encode.Value
poiFormEncoder model =
Encode.object
[
( "name", Encode.string model.name )
, ( "type", Encode.string model.poi_coordinates.coord_type)
, ( "poi_coordinates", Encode.array Encode.float (Array.fromList model.poi_coordinates.coordinates) )
]
Can i ask how to encode for this data type? I have no idea , and the encoder i did gives no coordinates fill. Any help is really appreciate. The Json file format is at below
[
{
"name": "Mcd",
"coordinates": {
"type": "Point",
"coordinates": [
101.856603,
2.924
]
}
},
.
.
.
]

You can nest calls to Json.Encode.object. Each time you want a new object in the output, you need another one, e.g:
poiFormEncoder : Model -> Encode.Value
poiFormEncoder model =
Encode.object
[ ( "name", Encode.string model.name )
, ( "coordinates"
, Encode.object
[ ( "type", Encode.string model.poi_coordinates.coord_type )
, ( "coordinates", Encode.list Encode.float model.poi_coordinates.coordinates )
]
)
]
This should make sense: it is a list of (key, value) pairs, and the value should be another object.
On a side note, it will depend on your use case, but your Coordinates type looks like a prime candidate for a custom Elm type, e.g:
type Coordinates
= Point { x : Float, y : Float }
| Polar { r : Float, t : Float }
| ...
If you find you are doing a lot of checking the string type value and then dealing with the coordinates accordingly, something like this might be a much nicer structure to use internally. Of course, the best representation will depend on how you are using the type.

Related

Cannot modify some values in a deep-copied nested Map

I'm new to Dart.
I have a constant Map that defines some kind of data schema, that way:
const Map<String, dynamic> FILE_STRUCT = {
"dt": {
"a": [],
"b": [],
"c": [],
},
"os": null,
"pm": null,
"p": "1",
};
Then, later, I want to use that structure to create a Map and add elements to it. So I deep-copy this Map to a new Map var, so I can add elements in it, that way:
Map data = {...FILE_STRUCT};
When I do data["os"] = "newValue"; I don't have any issue. But when I do:
List<List> aValues = [[1,2,3], [4,5,6]];
data["dt"]["a"] = aValues;
But when I do that, I get this error:
UnsupportedError (Unsupported operation: Cannot modify unmodifiable map)
I thought the var data was modifiable. So what is happening here, and what would be the proper way to modify the nested Map?

Look up key from Json Object - Neo4j

In Neo4j , is there a APOC function that i can use to get value a from jsonObject by passing a Key. For example :
My JsonObject is :
{ "masterName" : {"name1" : "A1" , "name2" : "A2", "name3": "A3", "name4" : "A4"}}
and while importing my csv that has "name" field (values : name1, name2, name3, etc) for which I want to lookup above JsonObjet and get respective value to create nodes.
Assuming that you have your json in a field called myJSON, you could
WITH ‘name1’ AS key, line
WITH apoc.convert.fromJsonMap(line.myJSON)[key] AS name

Creating fields in MongoDB

I would like to change my database from SQLite to MongoDB since mongo is schema less. In SQL database i had to create multiple rows for each attribute for the same sku(a product). I have to create n number of columns since each attribute have different specifications. Now in mongo I am planning to create only one document(row) for a sku having same id. To achieve this I would like to create a field(column) for specifications like html, pdf, description, etc. Now the last field is for attributes which has different values. I would like to store it in hash.(key value pairs). Does it make sense to store all the attributes in single cell? Am I going in right direction? Someone please suggest.
EDIT:
I want something like this.
My question is, in SQL i was creating columns for each attributes like attribute 1 name, value and attribute 2 name, value. This extends the row size. Now i want to store all the attributes in hash format(as shown in the image) since MongoDB is schema less. Is it possible? And does it makes sense? Is there any better option out?
Ultimately, how you store the data should be influenced by how you intend on accessing or updating the data, but one approach would be to create an embedded attributes object within each sku/product with all attributes for that sku/product:
Based on your example:
{
"sku_id" : 14628109,
"product_name" : "TE Connectivity",
"attributes" : {
"Widhth" : [ "4", "mm" ],
"Height" : [ "56", "cm" ],
"Strain_Relief_Body_Orientation" : "straight",
"Minimum_Operating_Temperature" : [ "40" , "C" ]
}
},
{
"sku_id" : 14628110,
"product_name" : "Tuning Holder",
"attributes" : {
"Widhth" : [ "7", "mm" ],
"diametr" : [ "78", "cm" ],
"Strain_Relief_Body_Orientation" : "straight",
"Minimum_Operating_Temperature" : [ "4" , "C" ]
}
},
{
"sku_id" : 14628111,
"product_name" : "Facing Holder",
"attributes" : {
"size" : [ "56", "nos" ],
"Height" : [ "89", "cm" ],
"Strain_Relief_Body_Orientation" : "straight",
"Minimum_Operating_Temperature" : [ "56" , "C" ]
}
}

CloudFormation Join in Tags

I'm running a CloudFormation template that uses the following snippet to tag various resources (this is a ELB tag, but others also exhibit this problem) I would expect this to produce a name tag of stackName-asgElb but it actually produces names such as olive-asg-asgElb-16GSCPHUFSWEN.
The stack name in this case was named olive-asg so I was expecting olive-asg-asgElb, without the -16GSCPHUFSWEN on the end.
Does anybody know where the seemingly random string on the end comes from?
CF template snippet:
Tags: [
{
Key: "Name",
Value: {
"Fn::Join": [
"-",
[
{
Ref: "AWS::StackName"
},
"asgElb"
]
]
}
}
]
That's interesting, I just tried it and I'm not able to reproduce the same results that you're seeing. It seems to be working as expected.
Here's the snippet I'm using in its entirety:
"ElasticLoadBalancer" : {
"Type" : "AWS::ElasticLoadBalancing::LoadBalancer",
"Properties" : {
"AvailabilityZones" : { "Fn::GetAZs" : "" },
"CrossZone" : "true",
"Listeners" : [ {
"LoadBalancerPort" : "80",
"InstancePort" : "80",
"Protocol" : "HTTP"
} ],
"Tags" : [
{
"Key" : "Name",
"Value" : { "Fn::Join" : [ "-", [ { "Ref" : "AWS::StackName" }, "MyELB"] ] }
}
]
}
},
The one noticeable difference I see in yours is that you're missing some of the quotes around the Tag stanza.
I feel foolish, the name tags are set correctly, I was looking at the physical IDs, not the name tags. The docs explaining how to control physical IDs are here.
Thanks to #alanwill for testing, and forcing me to go back through all the steps carefully!

Match column names to graph results in neo4j transactional REST response

A very cool feature of the neo4j transactional REST api endpoint is that you can get query results in graph format. However, I'm having trouble figuring out which node matches which column name when there are multiple nodes returned. Based on my testing the order of the nodes is not consistent with the column order.
For instance, in the example given in the documentation the query ends with
RETURN bike, p1, p2
and the response (edited) includes
"graph" : {
"nodes" : [ {
"id" : "4",
"labels" : [ "Bike" ],
"properties" : {
"weight" : 10
}
}, {
"id" : "5",
"labels" : [ "Wheel" ],
"properties" : {
"spokes" : 3
}
}, {
"id" : "6",
"labels" : [ "Wheel" ],
"properties" : {
"spokes" : 32
}
} ]
My question is, how do I know if the Wheel p1 corresponds to the node with id 5 or the node with id 6? Again, I don't think the order is guaranteed, at least as far as I can tell.
Actually, p1 and p2 are not nodes -- they are paths. In the Cypher code, one wheel is named frontWheel and the other is named backWheel.
To figure out the graph structure, you need to look at the relationships as well as the nodes in the returned graph data.
Here is another snippet from the results:
"relationships" : [ {
"id" : "0",
"type" : "HAS",
"startNode" : "4",
"endNode" : "5",
"properties" : {
"position" : 1
}
}, {
"id" : "1",
"type" : "HAS",
"startNode" : "4",
"endNode" : "6",
"properties" : {
"position" : 2
}
} ]
From the relationships data (especially startNode, endNode and the position property), you can tell that node 4 is bike, node 5 is frontWheel, and node 6 is backWheel.

Resources