How to access attributes from subtypes - graphengine

I followed the TSL documentation on attributes and created two classes:
[GraphNode]
cell struct Fruit
{
string color;
}
[GraphNode, BaseType : Fruit]
cell struct Apple
{
string variety;
}
I used the generated code like this:
Apple apple = new Apple();
Console.WriteLine(apple.variety);
Console.WriteLine(apple.color);
The code can access the variety field, but cannot access the color field:
'Apple' does not contain a definition for 'color' and no extension method 'color' accepting a first argument of type 'Apple' could be found (are you missing a using directive or an assembly reference?)
How can I access the inherited attribute?

Those attributes do not have an intrinsic meaning apart from the meaning you attach to them. "BaseType" does not make Apple derive from Fruit. You can just ask in your code for the value of the attribute "BaseType" on your cell.

Related

Kotlin make constructor of data class accept both List and MutableList but store a mutable instance of them

I want to make a data class which can accept both list and mutable-list and if the list is instance of MutableList then directly make it a property else if it is a List then convert it into a MutableList and then store it.
data class SidebarCategory(val title: String, val groups: MutableList<SidebarGroup>) {
constructor(title: String, groups: List<SidebarGroup>) :
this(title, if (groups is MutableList<SidebarGroup>) groups else groups.toMutableList())
}
In the above code Platform declaration clash: The following declarations have the same JVM signature error is thrown by the secondary constructor of the class (2nd line).
How should I approach this? Should I use a so called fake constructor (Companion.invoke()) or is there any better work-around?
List and MutableList are mapped to the same java.util.List class (mapped-types), so from JMV it will look like SidebarCategory has two identical constructors.
Instead of List, you can use Collection in the second constructor.
Use Collection instead of List, and then make an init block that sets it equal to a mutable list, as so:
data class SidebarCategory(val title: String, groups: Collection<SidebarGroup>) {
val groups = mutableListOf<>(groups)
}

Converting Powershell Array to Text so it can be exported to CSV or HTML

I'm trying to convert the output of a powershell (AWS Tools) command to strings so that I can export them to CSV or HTML. I for the life of me can't figure it out. I've seen comments on hashtables, naming elements, etc. Nothing seems to help me. (I'm very much a newbie).
This is what I got.
This command
(Get-IAMAccountAuthorizationDetail).UserDetailList | Select UserName, Grouplist
Will output this (with better spacing):
UserName GroupList
-------- ---------
User1 {Admins,Test}
User2 {Admins}
I cant' seem to figure out how to get this data so that it can be converted to CSV or HTML. Those brackets are an indication its an object, array or something. Can someone show me the code that would convert this to text or something that the Convertto-CVS o Convertto-HTML commands would work.
The output (subset) of the Get-Member Command is this:
TypeName : Amazon.IdentityManagement.Model.UserDetail
Name : Equals
MemberType : Method
Definition : bool Equals(System.Object obj)
TypeName : Amazon.IdentityManagement.Model.UserDetail
Name : GetHashCode
MemberType : Method
Definition : int GetHashCode()
TypeName : Amazon.IdentityManagement.Model.UserDetail
Name : GroupList
MemberType : Property
Definition : System.Collections.Generic.List[string] GroupList {get;set;}
Thanks
You could do something like the following, which will create a semi-colon delimited list within the GroupList cell:
(Get-IAMAccountAuthorizationDetail).UserDetailList |
Select-Object UserName,#{n='GroupList';e={$_.Grouplist -join ';'}}
Explanation:
The syntax #{n='Name';e={Expression}} is called a calculated property as explained at Select-Object. Here is some information about the calculated property:
It is a hash table with custom properties.
The first property is Name, which is a label for your expression output. n,Name,l, and label are all acceptable property names for that property.
The value passed to n is just a string that you are creating. It is the property name that will show up in your output, and it does not need to already exist in your object. Your actual property is called GroupList. As an example with n='All The Groups', the property name would becomeAll The Groups` in your output. There is nothing wrong with reusing the same name the property currently has.
The Expression or e is the ScriptBlock, which is why it is surrounded by {}. The ScriptBlock is responsible for producing the value in your custom property.
$_ is the current pipeline object passed into the ScriptBlock. This means if you have a collection (just like you do in your case), $_ will represent each of those items in order.
If you want to add another calculated property, just add a comma after the last and use the calculated property syntax like so:
Select-Object #{n='CustomProperty1';e={$_.ObjectProperty1}},#{n='CustomProperty2';e={$_.ObjectProperty2}}

Does #DynamoDBAttribute support document paths in the attribute name?

I've checked the DynamoDB documentation, and I can't find anything to confirm or deny whether this is allowed.
Is it valid to use a Document Path for the attributeName of #DynamoDBAttribute, as in this code snippet?
#DynamoDBDocument
public class MyClass {
#DynamoDBAttribute(attributeName="object.nestedObject.myAttribute")
private String myAttribute;
.
.
.
// Getters & Setters, etc
}
Edit: Just to be clear, I am specifically trying to find out whether document paths are valid in the #DynamoDBAttribute Java annotation as a way to directly access a nested value. I know that document paths work in general when specifying a query, but this question is specifically about DynamoDBMapper annotations.
Yes, the attribute name can have Dot on it. However, in my opinion, it is not recommended to have Dot on attribute name. Usually, the Dot will be used to navigate the tree in Map attribute.
The following are the naming rules for DynamoDB:
All names must be encoded using UTF-8, and are case-sensitive.
Table names and index names must be between 3 and 255 characters long,
and can contain only the following characters:
a-z
A-Z
0-9
_ (underscore)
(dash)
. (dot)
Attribute names must be between 1 and 255 characters long.
Accessing Map Elements:-
The dereference operator for a map element is . (a dot). Use a dot as
a separator between elements in a map:
MyMap.nestedField
MyMap.nestedField.deeplyNestedField
I can create the item with attribute name containing Dot and query the item using FilterExpression successfully.
It works similarly in all language AWS SDKs. As long as data type is defined as String, it would work as expected.
Some JS examples:-
Create Item:-
var table = "Movies";
var year = 2017;
var title = "putitem data test 2";
var dotAttr = "object.nestedObject.myAttribute";
var params = {
TableName:table,
Item:{
"yearkey": year,
"title": title,
"object.nestedObject.myAttribute": "S123"
},
ReturnValues : 'NONE'
};
Update:-
It works fine with #DynamoDBAttribute annotation as well.
private String dotAttr;
#DynamoDBAttribute(attributeName = "object.nestedObject.myAttribute")
public String getDotAttr() {
return dotAttr;
}
It is not possible to reference a nested path using the attribute name in a #DynamoDBAttribute. I needed to use a POJO type with an added#DynamoDBDocument annotation to represent each level of nesting.

Capture JSON property names with regex

I want to create a regex that will capture property names in JSON objects. So I can color their property names. Then, in loop (in TypeScript), I will add a span with class to color captured matches.
For example:
I have an object that looks like this
{"restriction_data" : "ALL","old" : null,"new" : ["ALL"],"record_type" : "product"}
I want to get restriction_data, old, new, record_type from regex and make it red in color.
Other JSON that I can get is:
{"category":"category is mandatory","field":"[u'NONE'] is invalid field. Found: NONE","description":"description is mandatory"}
And same, I want to get category, field, description and make it red.
I tried \"(.*?)\" regex, but it doesn't quite work for me.

Syntax for list delimiters in template

I'm writing an application that allows the user to configure the output using templates. For example:
Variables:
name = "BoppreH"
language = "Python"
Template:
My name is {name} and I like {language}.
Output:
My name is BoppreH and I like Python.
This works fine for simple data, like strings and numbers, but I can't find a good syntax for lists, more specifically for their delimiters.
fruits = ["banana", "apple", "watermelon"]
I like {???}.
I like banana, apple, watermelon.
In this case the desired delimiter was a comma, but how can the user specify that? Is there some template format with this feature?
I'm more concerned about making the syntax simple to understand, regardless of language or library.
Implement filters, and require their use for non-scalar types.
I like {fruits|join:", "}.
Typically, a list contains an unknown number of members, and sometimes variables/placeholders of its own. An example might be listing the name of a person along with their phone numbers. The desired output might look something like this:
John Doe
555-1212
555-1234
In a templating system that supported this, you'd need two types of variables: One that designated a placeholder for a value (like the curly braces you're using now), and another to denote the start and end of the list. So, something like this:
{name}
{{phone_numbers}}{phone}{{/phone_numbers}}
Your array of values might look like this:
values = [names: "John Doe", phone_numbers: [ phone: "555-1212", phone: "555-1234"]]
Each value in the "phone_numbers" array would create a new instance of everything that existed between {{phone_numbers}} and {{/phone_numbers}}, placing the values contained within those two "tags".