GObject: how to reset property to the default value? - gobject

I have a GObject which has a property of type GObject. I know that I can set this property like this:
g_object_set (G_OBJECT (my_object), "my-property", my_value_for_property, NULL);
But how do I reset the property to its default value? Probably, this line seems intuitive:
g_object_set_property (G_OBJECT (my_object), "my-property", NULL);
But what if I the default value ob "my-property" is non-null pointer to object? And anyway this line doesn't work. It seems I cannot just pass NULL to g_object_set_property()

You need to get the GParamSpec for the property (essentially, the definition of the property on the class) using g_object_class_find_property(), then get its default value using g_param_spec_get_default_value().
Something like the following should work:
GParamSpec *pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (my_object), "my-property");
const GValue *default_value = g_param_spec_get_default_value (pspec);
g_object_set_property (my_object, "my-property", default_value);

Related

How to Set Variable from Request in Postman

I am trying to write some tests for Postman. Many of the requests require an API key that gets returned by an initial GET request.
To set something hard-coded that is not dynamic, it looks like the test code is of the form
let variable = pm.iterationData.get("variable");
console.log("Variable will be set to", variable);
How do I set a return value as a global variable and then set that as a header parameter?
#Example if you setting ApiToken that is dynamic.
Under Tests tab in postman.
Put the following code.
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("Token", jsonData.token);
Under specific environment put variable name as "Token" and current value will be set automatically.
access the variable using {{variable_name}}
#Example: {{token}}
You can specify that variable value in the request Headers by using the {{var_name}} syntax. Instead of any hardcoded value that you may have been using.
You would previously have had to set the value using the pm.globals.set()syntax.
If you do not have the SDK active (monthly payment required to Postman). You can retrieve the values through __environment and __globals.
//Use of environment
postman.setEnvironmentVariable("my_value", "This is a value");
//...
let myValueVar = postman.__environment["my_value"];
console.log("Variable value is:", myValueVar);
//shows: Variable value is: This is a value
//Use of Globals
postman.setGlobalVariable("my_value2", "This is a value of globals");
//...
let myValueVar2 = postman.__globals["my_value2"];
console.log("Variable value is:", myValueVar2);
//shows: Variable value is: This is a value of globals
Also you can see your globals variables in Environments->Globals of your Posmant client.

Using dot-notation to access Immutable.js Records

I read here that is possible to access a prop of a Record object using dot-notation like person.Name X person.get('name'). However it isn't working to me if I try to access it using the property directly.
My code:
new Record({
id: 123,
description: 'Foo',
imgSrc: 'https://foo.co'
}))
If I try to access the property description directly like person.description it doesn't work. I should use person.get('description') rather access it directly.
What I'm doing wrong since it should let me access the property directly?
You're a using a Map, but property access using dot notation is only available on Immutable.Record. Here's the relevant section on the site: https://facebook.github.io/immutable-js/docs/#/Record
//first make a template
var MyThingie = Record({id:123, description:'Foo', imgSrc: 'https://foo.co'});
//now create records
var myRecord = new MyThingie({description:'Not the default description'});
//outputs 'Not the default description'
console.log(myRecord.description);

Saving JS Object (variant or var) in settings

When I write this:
property bool token: false
Settings {
property alias token: root.token
}
and then change token somewhere in application to true, next time when I run the application token is true.
But when I write this:
property var token: null
Settings {
property alias token: root.token
}
and change token to {'a': 'b'} for example, next time I run the application token is null like Settings doesn't save JS Object. Any idea what's wrong here?
Update:
It seems that problem is not with saving the js object {'a': 'b'}, QML saves it successfully, the problem is with null if I change it to false everything works alright. I guess it's related to this question Storing data using Settings element in QML but I don't understand why null is being post processed, though using false instead of null as default value for this property solves the problem I don't like it as I don't want false here, it's not appropriate morally, I want null.
Update 2:
I found my solution, setting no default value for property (not false nor null) seems to solve the issue and it seems appropriate morally like this:
property var token
Settings {
property alias token: root.token
}
I don't understand why null doesn't work yet. Any explanation would be welcome.
Setting no default value for property (not false nor null) seems to solve the issue:
property var token
Settings {
property alias token: root.token
}

Javafx: updating cell using choicebox in the GUI doesn't work

I have a problem in a Tableview created using javafx. I have set the edititable="true" on the fxml file of the tabel, then in the controller I execute
#FXML
private TableColumn<ARule,Object> rankCol;
rankCol.setCellValueFactory(new PropertyValueFactory<ARule, Object>("label")); rankCol.setCellFactory(ChoiceBoxTableCell.forTableColumn(Main.getlabelSample()));
rankCol.setOnEditCommit(e -> {System.out.println("something happens!");});
To create in the column rank, a choicebox to change te value of the property.
The ARule has a property field and the getter and setters:
private SimpleObjectProperty label;
public SimpleObjectProperty labelProperty() {
return label;
}
public void setLabel(Object label) {
this.label.set(label);
}
public Object getLabel(){
return this.label.getValue();
}
The function Main.getlabelSample() retrun this object filled with Strings or Integer
private static final ObservableList<Object> labelSample = FXCollections.observableArrayList();
The problem is that in the interface I can edit the column and it displays the correct value in the labelSample list, the problem is that it doesn't change the value of the ARule object, this is highlighted by the missing call of the setOnEditCommit handler. The value on the GUI is the new one selected but the value saved on the items in the table is the old one.
I have also a separated button to change the value of that column on the selected row and if I trigger that, the values changes for "real" (both on the GUI and on the model).
What could be the error in the code?
The default edit commit behavior of the column is set as the onEditCommit property. If you call
rankCol.setOnEditCommit(...);
then you set this property to something else, i.e. you remove the default behavior.
If you want to add additional behavior to the default, use addEventHandler(...) instead of setOnEditCommit(...):
rankCol.addEventHandler(TableColumn.editCommitEvent(), e -> {
System.out.println("Something happens");
});
Find the answer the line of code:
rankCol.setOnEditCommit(e -> {System.out.println("something happens!");});
for some reason overwrite the default behaviour of updating the cell changing the code into
rankCol.setOnEditCommit(e -> {
e.getTableView().getItems().get(e.getTablePosition().getRow()).setLabel(e.getNewValue());
System.out.println("Something happens!");});
Resolved the problem. At the moment I don't know why this is happening.

Why does this Machine.Fakes parameter matching throw an exception?

I'm using Machine.Fakes.NSubstitute and want to "fake" a return value such that if the input parameter matches a specific value it returns the mock object, otherwise it returns null.
I tried the following:
host.WhenToldTo(h => h.GetTenantInstance(Param.Is(new Uri("http://foo.bar"))))
.Return(new TenantInstance());
But it throws the following exception:
System.InvalidCastException: Unable to cast object of type
'System.Linq.Expressions.NewExpression' to type
'System.Linq.Expressions.ConstantExpression'.
My current workaround is to do the following:
host.WhenToldTo(h => h.GetTenantInstance(Param.IsAny<Uri>()))
.Return<Uri>(uri => uri.Host == "foo.bar" ? new TenantInstance() : null);
Which is a bit smelly.
I see three aspects here:
When a method with a reference type return value is called on a mock object and no behavior has been set up for the call, the mock object will return a mock. If you want it to return null instead, you have to configure that explicitly. Thus, it is not enough to set up
host.WhenToldTo(h => h.GetTenantInstance(Param.Is(new Uri("http://foo.bar"))))
.Return(new TenantInstance());
You also have to set up the other case with something like this:
host.WhenToldTo(h => h.GetTenantInstance(Param<Uri>.Matches(x => !x.Equals(new Uri("http://foo.bar")))))
.Return((TenantInstance)null);
I find your "workaround" solution more elegant than these two setups.
When you match a method call argument for equality, there is no need to use Param.Is(). You can simply set up the behavior with
host.WhenToldTo(h => h.GetTenantInstance(new Uri("http://foo.bar")))
.Return(new TenantInstance());
The fact that you get an exception when using Param.Is() here is a shortcoming of Machine.Fakes. I see not reason why this should not work. I will correct that at some point and let you know.