Nil values not allowed for gRPC in Ruby (proto3) - ruby-on-rails

Hi I am using gRPC Ruby plugin to communicate to a service. The proto definition contains(proto3):
uint32 id = 1;
But, when I assign nil to id, it throws an error - expected number for integral field. But for strings, nil values work fine. How do I allow nil values for integral / float fields?

The default values for strings in proto3 is empty -
https://github.com/google/protobuf/issues/359
Hence it can receive the nil value without returning an error, Integers on the other hand have default values set to 0. Try passing 0 in case you do not want to pass any value.
Suggestion: - In case you do not wish to pass any tuples for a message written in proto3 you can leave the message blank which is a very valid way to define a message.
For Instance:-
syntax="proto3"
service Foo{
rpc Index(Empty) Returns(Nothing){}
}
message Empty{}
message Nothing{}

Related

Why is Dafny allowing uninitialized return result?

In this method:
datatype Results = Foo | Bar
method test() returns (r:Result)
{
}
Dafny verifies OK and test() returns Foo. Which is technically correct (it does return a value of the correct type) however I was expecting Dafny to complain that the result has not been set by the method itself. What test() is doing is similar to doing:
return;
in a C function that is supposed to return an int.
Is there a way to make Dafny verify that a methods results are always set before the method returns?
The flag you want is /definiteAssignment:2:
/definiteAssignment:<n>
0 - ignores definite-assignment rules; this mode is for testing only--it is
not sound
1 (default) - enforces definite-assignment rules for compiled variables and fields
whose types do not support auto-initialization and for ghost variables
and fields whose type is possibly empty
2 - enforces definite-assignment for all non-yield-parameter
variables and fields, regardless of their types
3 - like 2, but also performs checks in the compiler that no nondeterministic
statements are used; thus, a program that passes at this level 3 is one
that the language guarantees that values seen during execution will be
the same in every run of the program
This is what Dafny says on your code says with that flag:
test.dfy(5,0): Error: out-parameter 'r', which is subject to definite-assignment rules, might be uninitialized at this return point

What's the difference between ? and ! in collections like Map, List and Set?

What's the difference between ? and ! when used in a collection in Dart?
Say, I have:
var list = [1, 2];
Now, I can either use
print(list?[0]); // prints 1
or
print(list![0]); // also prints 1
Both of them seems to do the same job, so what's the difference?
Both of them seem to do the same job because your list is of type List<int> (non-nullable) and not the List<int>? (nullable). If your list had been of nullable type like:
List<int>? list;
you'd see the difference.
Using? (Null aware operator)
It would be safe to use ? because in case list is null, list?[0] would still print null rather than throwing an error.
print(list?[0]); // Safe
or you could also use ?? to provide a default value.
print(list?[0] ?? -1); // Safe. Providing -1 as default value in case the list is null
Using ! (Bang operator)
However, ! throws runtime error because you're explicitly saying that your list is not null and you're downcasting it from nullable to non-nullable:
print(list![0]); // Not safe. May cause runtime error if list is null
This is equivalent to
print((list as List)[0]);

Why jsonDecode in Dart has no type and no documentation?

As you can see in https://api.dart.dev/stable/2.7.1/dart-convert/jsonDecode.html, it has no type and no documentation. I don't know which methods I can invoke on the result neither I don't know which type to but on a parameter that should be a json object.
Why is Dart like this? And what are the advantages?
It does have documentation, and you are linking to it.
If you want it to have more documentation, then that is reasonable. The returned value is admittedly not documented very well.
The function jsonDecode is a shorthand for json.decode, which again forwards to JsonDecoder.convert.
It returns a "JSON value" object which depends on the JSON source that it decodes.
A "JSON value" can be any of:
* null
* an int
* a double
* a String
* a bool (true or false)
* a List<dynamic> containing zero or more JSON values.
* a Map<String, dynamic> mapping keys to JSON values.
Those are also the same values that are accepted by the JsonEncoder which converts object structures to JSON strings.
Since these types have no common superclass other than Object, the function cannot have a return type which is more specific than dynamic or Object.
The chosen return type is dynamic because the dynamic type allows the receiver to optimistically call any member on the value. They might know that the value will always be a map, so they can just do jsonParse(jsonSource)["key"] to look up a value. Obviously, if the source was not a JSON object, that call will fail.
If you don't know which type the result is, you have to check:
var data = jsonDecode(jsonSource);
if (data is Map<String, dynamic>) {
something something data["key"] something
} else if (data is List<dynamic>) {
something something list[2] something
} else ... etc ...
A valid JSON file is actually a valid Dart expression too. The value returned by jsonDecode is similar to the value you would get if you wrote the JSON code directly as Dart code (in Dart 1 it was exactly the same, in Dart 2, the Dart code might infer a more precise type for maps and lists).

Syntax for setting/updating register fields in Map CRDT on server

What is the syntax for setting lwwreg register values in CRDT Map on server side in Riak? I tried a code like below which doesn't seem to be valid:
%% Obj is a map object to which we want to add/set a register "uname" with value
%% "ahmed"
riak_kv_crdt:update(Obj,<<"testing">>,{crdt_op,riak_dt_map,
{update,[{assign,<<"uname">>,<<"ahmed">>}]},undefined})
I get an error about the operation being invalid - I looked around in source code for riak_dt_map.erl but still can't figure out correct syntax:
> riak_kv_crdt:update(Obj,<<"testing">>,{crdt_op,riak_dt_map,{update,
[{assign,<<"uname">>,<<"ahmed">>}]},undefined}).
** exception error: no function clause matching
riak_dt_map:apply_ops([{assign,<<"uname">>,<<"ahmed">>}],
{<<"testing">>,1},
{[{<<"testing">>,1}],
.....
Will appreciate pointers on correct syntax.
Figured it out. The correct syntax is below - the key must be accompanied by the type of the field which is riak_dt_lwwreg in this case, and assign operation must be specified for register value - so, the syntax becomes:
riak_kv_crdt:update(Obj,<<"testing">>,{crdt_op,riak_dt_map,{update,
[{update,{<<"uname">>,riak_dt_lwwreg},{assign,<<"ahmed">>}}]},undefined})

Using the TYPE command inside a Redis / Lua Script

I am attempting to use the Redis TYPE command inside a Lua script (executed via EVAL)
local key_type = redis.call("TYPE", key)
According to the Redis documentation, this should return a string of "none", "zset" etc.
However the type of the returned value is a lua table. Comparing the value to a string always returns false.
I've managed to get around the problem by changing the call to
local key_type = redis.call("TYPE", key)["ok"]
This value is indeed a string and does work in string comparison commands. I am worried that this is a bug in my particular version of Redis and it will break in future versions when I upgrade.
Does anyone know if this is expected behaviour, or a bug?
The TYPE command returns a status reply (a.k.a simple string), e.g "+list\r\n".
On Redis scripting side, call is implemented by luaRedisCallCommand which performs the real Redis command behind the scenes.
Once successfully executed, this function converts the command result with redisProtocolToLuaType.
When a status reply is encountered, this function creates a Lua table with "ok" as key, and the status reply as value (see redisProtocolToLuaType_Status). So:
there is no bug,
this is why redis.call("TYPE", key) is a table (and thus you need to get the value for the "ok" key as you did, to get key's type as a string).
Note: when you directly return the table, Redis takes care to get the value associated to the "ok" key, and returns it as a status reply, e.g:
> EVAL 'return redis.call("TYPE", "foo")'
set
See this code section for more details.

Resources