Is there a generic way of setting field values in mapper from list of values? - lift

Is there a way of creating method for setting the value in the Model's fields without setting the values explicitly like -
ModelName.create.fieldName1("value").fieldName2("value2") and so on
Can we iterate through all available fields of that model and set their values form some list-of-values ?
something like ...
Model.allFields.foreach((fld)=> {
fld.set(valueList(indx)); indx+=1
}
Actually I want to set values into all models using some generic method that works for all models.

According to my comment:
val list = List(...)
val record = YourRecordClass.createRecord
record.allFields.zip(list).foreach {case(field,value) => field.setFromAny(value)}

Related

ag-grid setModel equivalent of selectValue

Using ag-grid 23.2.1, it looks like our current approach to filtering is on it's way out and listed as deprecated. Reading over the setModel documentation, I don't readily see a way to replace some of our selectValue, selectNothing, etc. usage.
Example 1: we clear the selection and then pick a couple of values on some condition else we select all
let filterInstance = this.gridOptions.api.getFilterInstance('make');
this.cacheFilterModel = this.gridOptions.api.getFilterModel();
if (condition) {
filterInstance.selectNothing();
filterInstance.selectValue('thing1');
filterInstance.selectValue('thing2');
}
else {
filterInstance.selectEverything();
}
filterInstance.applyModel();
this.gridOptions.api.onFilterChanged();
Example 2: we have some filter values in a column toggle-able via checkboxes, which call a method like the below. if one of the values is checked it gets added to the existing filter, if unchecked the value is removed. There could be values already filtered and I don't really see a way to contextually select/unselect values using setModel.
filterExample (make, add) {
let filterInstance = this.gridOptions.api.getFilterInstance('make');
if (add) {
filterInstance.selectValue(make);
}
else {
filterInstance.unselectValue(make);
}
filterInstance.applyModel();
this.gridOptions.api.onFilterChanged();
}
Are there setModel equivalents for these?
I had a similar issue with setColumnFilter where I had to pass a few values. You can use filterInstance.setModel to set these values by passing an array like so:
filterInstance.setModel({ values: ['value1', 'value2'] })
filterInstance.applyModel();
gridOptions.api.onFilterChanged()
here is a link to how to do it from the docs. I found this hard to find on google so providing it here.
https://www.ag-grid.com/javascript-grid-filter-set-api/

Access protobuf field name dynamically with scala

I am new to Scala. Writing my first application.
I have defined my proto file with fields email_id and phone_number which is request definition for grpc call
I can access values by dot operator like params.emailId
Now what I am trying to do is I have one array of mandatory fields. I want to check the values for those fields defined in an array with input request parameters.
How can i access this params.{field name from array} to check for not empty values.
Getting error for below code with :
val mandatoryFields = Array("emailId","phoneNumber")
println(params.emailId) //works
for (fields <- mandatoryFields) {
println(fields)
println(params.fields) // getting error
}
It has function 'in.getFieldByNumber()' where you can fetch value by index location, is there any function available like getFieldByName() or something like that.
Although it's been a long from the question date, I don't think it is answerless. Actually, I have one:
Using toPMessage method, you'll have your protobuffer case class as an instance of PMessage object. Then you could retrieve the Map[FieldDescriptor, PValue]. Finding field values by name would be like:
val fieldDescriptorPValueMap: Map[FieldDescriptor, PValue] = params.toPMessage.value
mandatoryFields.foreach(fieldName=>{
println(
fieldDescriptorPValueMap
.filter(entry => fieldName == entry._1.name)
.values
.head.as[String]
)
})

How to negate class selector in Cytoscape.js?

I want to select all elements that do not have the class "myclass". How can I do that in Cytoscape.js?
According to http://js.cytoscape.org/#selectors/data, "[^name] Matches elements if the specified data attribute is not defined", however a class is not a data attribute and ^.myclass does not work, neither does :not(.myclass).
The error is The selector :not(.myclass) is invalid.
Is there a way to negate classes?
If you want to get the negative class selector, you can do this:
cy.elements().not(cy.$('.yourClass'));
// in more detail
var allElements = cy.elements(); // get all elements
var negators = cy.$('.yourClass'); // get all elements with the class to negate
var result = allElements.not(negators); // gets the difference between the two collections
If you really want to achieve this by using selectors only, then you might add a data field to each element which has myclass (this can be done while adding the class), and then use [^myclass]

Delete an element in a set by name

I have the following set:
class Element (var Name:String, var Description: String)
var MoreElement: Set[Element] = Set(E1, E2, E3, ...)
How do I delete an Element in a set MoreElement by name.
I found this solution:
MoreElement -= (MoreElement find (_.Name == "nameOfElementToRemove")).get
but I would not use the get, because if you does not find the item is thrown an exception, however I do not want no exception.
MoreElement = MoreElement filterNot (_.Name == "nameOfElementToRemove")
The direct answer to you question is to use filter, meaning something like:
moreElements = moreElements.filter( _.name != "nameOfElementToRemove")
Note this will scan the set. If you want a set indexed by name, you should really use a Map.
However, some caveats:
A set is a collection of unique elements. In order to compare elements in the set, it uses the contained type's equality operator. In your case, the Element class needs to define the 'equals' method (and hashCode) so the set can effectively compare instances.
In addition, you need to keep in mind that Set is an immutable class in Scala, so in your example you're really creating a new set, despite using an operator that appears to modify the existing set.
If you want a mutable set, you need to import scala.collection.mutable.Set.

How to update a property using Type.GetProperties() method?

I've a collection of a class' properties and would like to update each one's value by iterating over the collection through the index.
1) I create the collection of properties this way
private PropertyInfo[] GetPropertiesOfMyClass()
{
Type myType = (typeof(myClass));
PropertyInfo[] PropertyInfoArray = myType.GetProperties(
BindingFlags.Public |
BindingFlags.Instance);
return PropertyInfoArray;
}
2)Now, I'd like to set up the value of each one depending on the index this way
public void UpdateProperty(MyClass instanceOfMyClass, string valueToUpdate, int index)
{
//TODO:
//1. Get an individual property from the GetPropertyOfMyClass() using index
//2. Update the value of an individual property of the instanceOfMyClass
}
I'd like to be able to call UpdateProperty from a Controller like this:
UpdateProperty(instanceOfMyClass, valueToUpdate, indexOfTheProperty);
Honestly, I do not know how to involve the instanceOfMyClass in the game as GetProperty only plays with myClass.
Since I saw that I can use Name, PropertyType, ... to get information on the property. So, I've tried also GetPropertyOfMyClass()[index].SetValue(...), but I was lost in the arguments of its constructor, so I abandoned.
What I want is to be able to update the value of a property in my collection just by using the index.
Thanks for helping
Your guess was correct. You use SetValue() to update the value - this is how to do it:
GetPropertyOfMyClass()[index].SetValue( instanceOfMyClass, valueToUpdate, null);
The last argument can be null:
Optional index values for indexed properties. This value should be null for non-indexed properties.