I need to pass a UniqueIdentifier as a string between two services. Unfortunately UniqueIdentifier#toString and UniqueIdentifier.fromString do not work well if an externalId is set.
What is the canonical way of serializing and deserializing a UniqueIdentifier?
If you're using Corda's library to create a UniqueIdentifier set with a custom external id, you can see that the toString() will generate a pattern of ${externalId}_$id i.e dummyExternalId_10ed0cc3-7bdf-4000-b610-595e36667d7d.
So to covert it back to UniqueIdentifier from that string, just split by delimiter of _
if (p.text.contains("_")) {
val ids = p.text.split("_")
//Create UUID object from string.
val uuid: UUID = UUID.fromString(ids[1])
//Create UniqueIdentifier object using externalId and UUID.
return UniqueIdentifier(ids[0], uuid)
}
Link here
If you have underscore in external id, you'll probably need your own function.
val potentialIds = input.split("_")
// Drop last one and stitch back the external id
val externalIdString = potentialIds.dropLast(1).joinToString("_")
// Last one is the UUID
val uuid = UUID.fromString(potentialIds.last())
val finalUniqueIdentifier = UniqueIdentifier(externalIdString, uuid)
Wait a sec, do you have to create an UniqueIdentifier for some business of yours? In that case you can just create a UniqueIdentifier from scratch and pass it as a variable between services (transactions accept external attachment).
If you have to "extract" the uniqueIdentifier of your State I don't think that is possible.
If I missed the point I apologize in advance :D
Related
Let's say I have
#Id #GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name="trade_id")
int tradeId;
When I query this from the database, I will want to get the tradeId. However, when performing a Create, obviously I won't have the tradeId, since the Database is going to generate it when I insert it.
input TradeInput {
tradeId: Int!
...
But the schema SPQR is generating for me is setting this field to Not Null, however.
So my question is, how can prevent this field from automatically coming up as Not Null, so I won't have to send it down on Creates, but can retrieve it.
An int simply can not be null. But you have a few options:
Turn it into an ID scalar type (using #GraphQLId) instead of an Int
Give it a default value via #GraphQLInputField
Use Integer instead of int, as Integer is nullable
We are planning to move our existing services to move grpc service. so need to convert the services to proto Defined Message Type. In the reponse, we have map with customize object as key.
eg response:
//java
Map<key_object, Project>
//proto
map<key_object_not_supported, Project> projects = 3;
In the official doc, they mentioned,
where the key_type can be any integral or string type (so, any scalar
type except for floating point types and bytes). The value_type can be
any type
Is it any alternative ways to achieve customise object key map in the proto3 ?
Support for map is a rather new extension. If your key does not match the constraints, you can use the old way instead:
Define a key-pair message and use that as a repeated field. So, in your example:
message KeyValuePair {
key_object_not_supported key = 1;
Project value = 2;
}
message MyMap {
repeated KeyValuePair entries = 1;
}
I don't think I am asking the question correctly, but hopefully you know what I am asking.
What are pros and cons of using a string value to represent a database field (or any variable) vs using an enumeration or constant? I am not asking about the datatype, but hows its handled on the back-end. I'll use LINQ to SQL for an example.
My thinking is that by using an enumerable or constant it's: easier to read, ensures compatibly should the values ever need to be changed, and the value is hard coded -so to speak- so there are less chances of an error caused by a typo. On the flip side, do I really need a class/structure with member enumerations that essentially act as a look up for the value I want?
Using an Constant
Module Trip
Public Const OPEN As String = "Open"
Public Const PENDING_PAYMENT As String = "Pending Payment"
Public Const CANCELLED As String = "Cancelled"
Public Const CLOSED As String = "Closed"
End Module
Dim product = From p In db.Payments
Where p.PaymentId = PaymentId
For Each item In product
item.Status = PayStatus.PENDING_PAYMENT
Next
Using a string
Dim product = From p In db.Payments
Where p.PaymentId = PaymentId
For Each item In product
item.Status = "Pending Payment"
Next
As one of the comments says, the common way to deal with this is using a lookup table in the database. In its most simple form, you would have a class, let's say PaymentStatus:
Class PaymentStatus
Public Property Id As Integer
Public Property Name As String
End Class
and Payment would have reference property like
Public Property PaymentStatus As PaymentStatus
This way, you can always get the options from the database and you will never make a typo in code. It's also much easier to add options or to change descriptions.
For instance, think of what you need to do if you'd decide that "Cancelled" needs to be differentiated into "Cancelled by user" (the old status) and "Cancelled by system" (a new status introduced by new business logic). You'd need a script to update all records in the database to the new string (and change the code, but you'd be changing code anyway). A lookup table allows you to update only one record (and add a new one in this example).
When I am running a DeleteItemRequest on a dynamoDB table, I am getting an exception which says "the provided key size doesn't match with that of the schema".
All I am doing is
DeleteItemRequest deleteRequest = new DeleteItemRequest().withTableName(dynamoDbTableName).withKey(key);
client.deleteItem(deleteRequest);
Do I need to specify something more? Am I missing something?
It could mean that the key passed to the method does not match the type of the primary key in the table. For example, you are passing a numerical key but the table uses a string key. The type of the key depends on the method used when creating the AttributeValue. The method withN() creates a numerical key, while the method .withS() creates a string key.
Numerical key example:
Key key = new Key().withHashKeyElement(new AttributeValue().withN("120"));
String key example:
Key key = new Key().withHashKeyElement(new AttributeValue().withS("johndoe"));
There are methods for other types as well, like binary types and sets. See the javadoc for the AttributeValue class for more details.
Consider the following code:
class Foo(var name: String = "bar")
Now i try to get the value and the correct type of it via reflection:
val foo = new Foo
val field = foo.getClass.getDeclaredField("name")
field.setAccessible(true)
//This is where it doesn't work
val value = field.get(????)
I tried things like field.get(foo), but that just returns an java.lang.Object but no String. Basically I need the correct type, because I want to invoke a method on it (e. g. toCharArray).
What is the suggested way to do that?
As others have mentioned, the reflection methods return Object so you have to cast. You may be better using the method that the Scala compiler creates for field access rather than having to change the visibility of the private field. (I'm not even sure if the name private field is guaranteed to be the same as that of the accessor methods.)
val foo = new Foo
val method = foo.getClass.getDeclaredMethod("name")
val value = method.get(foo).asInstanceOf[String]
getDeclaredField is a method of java.lang.Class.
You have to change foo.getDeclaredField("name") to foo.getClass.getDeclaredField("name") (or classOf[Foo].getDeclaredField("name")) to get the field.
You can get the type with getType method in class Field but it won't help you because it returns Class[_]. Given than you know that the type is a String you can always cast the value returned using field.get(foo).asInstanceOf[String]
AFAIK, reflection always work with Object, and you have to cast the results yourself.
This is how one can get list of fieldnames and its value of a case class:
First, using reflection, get fields info as follows -
val TUPLE2_OF_FIELDNAME_TO_GETTERS = typeOf[<CLASS>].members
.filter(!_.isMethod)
.map(x => (x.name.toString, classOf[<CLASS>].getDeclaredMethod(x.name.toString.trim)))
How to use it?
getFieldNameAndValue(obj: <CLASS>): Seq[(String, String)] {
var output = Seq[(String, String)]()
for(fieldToGetter <- TUPLE2_OF_FIELDNAME_TO_GETTERS) {
val fieldNameAsString = fieldToGetter._1
val getter = fieldToGetter._2
val fieldValue = getter.invoke(obj).toString
output += (fieldName, fieldValue)
}
}
foo.getClass.getDeclaredField("name").getString(foo)
should work if you want to avoid asInstanceOf. get* is available for various types