Corda - Purpose of NULL_PARTY - corda

What is the purpose of NullKeys.NULL_PARTY?
For example, when should I use
party: AbstractParty = NullKeys.NULL_PATRY
Rather than
party: AbstractParty? = null

party: AbstractParty? = null
The above defines a nullable variable i.e. party will either be an AbstractParty or it will be null
party: AbstractParty = NullKeys.NULL_PARTY
The above on the other hand, will never result in party being null, but rather you'll end up with an AnonymousParty with a null public key.
NULL_PARTY could come in useful during unit testing, particularly when testing for equality, but it's not advised to use it for production code.

!= NULL_PARTY works just as well as != null and removes the nuisance of not-null assertion/safety operators when you can be sure the value will not be used, e.g. an owner for a state that has not been purchased by one.
See also sample uses in Corda's repo.

Related

Doctrine compare two objects

Hi I need to compare two objects in doctrine. I have customer repository and entity. This is my code,
public function index(CarAdRepository $carAdRepository, CustomerRepository $customerRepository): Response {
$cus = $customerRepository->findAll();
$customer = new Customer();
$customer->setTitle('Mr');
$customer->setName('aaa');
$customer->setLastName('bbb');
if($customer == $cus[0]){
echo 'ddd';
}else{
echo 'no';
}
}
in my table I have this values,
But I always get no. It would be great if someone can help
Doctrine implements IdentityMap pattern that ensures that you're always receiving same object for same database row, but only if it was loaded from identity map.
In your case you're comparing some arbitrary object with entity fetched from database using PHP comparison operator. In other words you're checking if 2 objects are equal, but there is no such built-in functionality in PHP.
You have to implement objects comparison function by yourself to achieve your goal because actual comparison logic may vary.
UPDATE:
Simplest example of comparison in your case is property-by-property comparison:
private function compare(Customer $a, Customer $b)
{
return $a->getTitle() === $b->getTitle() &&
$a->getName() === $b->getName() &&
$a->getLastName() === $b->getLastName();
}
It also may be worth to move this method directly into Customer entity with name like isEqual().
It is also possible to implement more generic approach by using reflection, but it may bring certain level of complexity in a case if some non-trivial comparison will need to be involved.

Unable to resolve flow annotation error when using immutablejs records and calling update on one

I see the following record type code:
type AppProps = {
+fetches: Map<string, number>,
};
export const makeApp: RecordFactory<AppProps> = Immutable.Record({
fetches: Immutable.Map()
});
export type App = RecordOf<AppProps>;
Now I have a call that uses the record's update function:
const state = makeApp({});
const result = state.update('fetches', val =>
val.set(action.meta.actionBase, 1)
);
All unit tests pass, behaviour is good, but I get a flow error:
Error:(40, 18) Missing type annotation for T. T is a type
parameter declared in RecordInstance [1] and was implicitly
instantiated at call of method update [2].
I have an idea what is going on here, but I don't know flow well known to actually fix this, or even come up with a workaround. Please help!
ImmutableJS version "immutable": "^4.0.0-rc.12",
Flow is asking for a concrete type argument for T, which is defined in the update function of state.
Here's another example of a cause and fix of this error message:
Missing annotation error
Fixed
If you provide the type signature of state.update, I may be able to provide more information.

How do I type a function with input and output objects with the same keys but different value types?

Basically, I have a function that will transform an object into a different object, and it's like a dictionary, but I don't know how to type it.
var myFunctions = {
a: () => something1,
b: () => something2,
[...]
}
gets transformed into
var myObject = {
a: something1,
b: something2
[...]
}
With Flow 0.33+ you can use $ObjMap
type ExtractCodomain = <V>(v: () => V) => V;
declare function f<O>(o: O): $ObjMap<O, ExtractCodomain>;
I don't think you can do this with Flow. The closest you can get is probably this:
function<T>(obj: T): ([key: $Keys<T>]: boolean)
That function is typed to return an object with the same key as input object, but with boolean-only values (as an example, you can specify another type). Sorry to disappoint, but it's hard to type highly dynamic code with Flow in general.
Note that the $Keys feature is undocumented because it's not part of the public API, so its behavior is defined solely by its implementation (in other words, it can change anytime).
If you're interested in the details of Flow's type system, check out the typings that come with flow in its own /lib directory, for example https://github.com/facebook/flow/blob/master/lib/core.js – you'll see that some things like Object.assign are special-cased, so you might not be able to re-implement such things in your own code.
Also, check out http://sitr.us/2015/05/31/advanced-features-in-flow.html for other "dollar features" such as $Shape and $Diff – it's partially outdated, but can give some good pointers.
#Nikita gave you the best answer for now. That said, the use-case you talked about is being discussed in the issues on the FlowType repository. It may land soon.
As of right now, if you've got mixed type, I'll just fallback to any
function<T>(obj: T): ([key: $Keys<T>]: any)
This way, at least the key names are validated. I expect within a few more versions of Flow, this problem will get solved.

How to check if SimpleList or SimpleRecord is empty

I got the following issue. I am trying to use the With or WithMany instruction
to retrieve a linked list of roles of an business relation via an
outer join. The referential integrity is in place on the database but
the primary key on the roles table is a composite key. That's the
reason i use an OuterJoin clause because I get an exception
otherwise .
When the query gets executed the results are exactly as I expected and
nicely filled with data. Nevertheless there are certain cases where
there are not yet roles available in the database. So I would expect
that in those cases the returned SimpleList (Roles below) would be
null, cause there is not data available. Instead Simple.Data returns a
SimpleLIst and if i expand the Dynamic View in debug then it says
'Empty: No further information on this object could be discovered".
Even if i traverse further down and i retrieve the first object in the
SimpleList, it even returns a SimpleRecord with the same information
as above in debug. Only after I request a property of the SimpleRecord
I get some information that the record was empty because then it
returns null.
To come to the bottom line... is there anybody who can tell me how to
check if a SimpleList or SimpleRecord is empty or null without
traversing down the hierarchy?
I am using Simple.Data 0.16.1.0 (due to policy i can't use the
beta yet)
Thanks in advance for reading the whole story...
Below is the code sample:
dynamic businessRelationRoles;
var query = db.Zakenrelaties.As("BusinessRelations")
.All()
.OuterJoin(db.Zakenrelaties_Rollen.As("Roles"), out businessRelationRoles)
.On(zr_ID: db.Zakenrelaties.zr_ID)
.With(businessRelationRoles);
foreach (var relation in query)
{
//Get the SimpleList as IEnumerable
IEnumerable<dynamic> roles = relation.Roles;
//Get the first available SimpleRecord
var role = roles.First();
//Check if any record was returned..This passes always?? Even if the SimpleList was empty
if (role != null)
{
//Get the id of the role. returns null if SimpleRecord was empty
var roleId = role.zrro_id;
}
}
Is there anybody who can help me out?
Belatedly, and for information purposes only, this was a bug and got fixed in the 0.17 (aka 1.0-RC0) release.

Symfony 1.4 | How can I store NULL for DateTime-fields?

When I try to store NULL for a DateTime-field it always comes out as 0000-00-00 00:00:00. I have set up the schema and the database correctly in order to be able to store NULL values. (allowed "NULL" to be stored and even set the default value for those datetime-fields to NULL).
I tried:
1.) The "pre-binding"-approach (=> in the actions.class)
$values = $request->getParameter($form->getName());
$values['datetime_field'] = null;
$form->bind($values, $request->getFiles($form->getName()));
2.) The "pre-saving"-approach (=> in the ctions.class)
if ($form->isValid())
{
$form->getObject()->setDateTimeField(null);
$form->save();
3.) The "override updateObject()"-approach (=> in the forms-class)
public function updateObject($values = null)
{
$object = parent::updateObject($values);
$object->setDateTimeField(null);
return $object;
}
There must be something I haven't thought of... why is there always 0000-00-00 00:00:00 stored instead of NULL? Really "nothing" needs to be stored, so the DBMS could handle the "NULL-insertion" itself...
Any help/hint is HIGHLY appreciated! :)
Are you (a) using Doctrine, and (b) using "datetime" as the field type in your schema? Try switching to "timestamp" -- as the data types section of the Doctrine documentation says, this is the correct type for a field which stores both dates and times. It will map to MySQL's datetime type in the database.
Are you sure that in your schema, have you got "notnull: false" for this field?
Also check that in your SQL schema, i.e. in phpmyadmin, that the datetime/timestamp field is NULL under the Default column
date_time_field datetime DEFAULT NULL,

Resources