isolated function mockFunction(record {|string operation; float operand1; float operand2;|} input) returns string{
return "mockFunction scuccessful";
}
public function main() returns error? {
string input = "{\"operation\":\"ADDITION\",\"operand1\":23.0,\"operand2\":43.0}";
map<json> & readonly inputJson = check input.fromJsonStringWithType();
any|error observation = function:call(mockFunction, inputJson);
io:println(observation);
}
I got the following error when I tried the above.
error: {ballerina/lang.function}IncompatibleArguments {"message":"arguments of incompatible types: argument list '(map<(json & readonly)> & readonly)' cannot be passed to function expecting parameter list '(ai.agent:record {| string operation; float operand1; float operand2; |})'"}
function:call()method will attempt to cast theinputJsonto the expected argument type. It gives the above error when the casting fails.fromJsonStringWithTypehttps://lib.ballerina.io/ballerina/lang.value/0.0.0#fromJsonStringWithType is a combination offromJsonStringandfromJsonWithType. Numbers are generally converted to decimal withfromJsonStringhttps://lib.ballerina.io/ballerina/lang.value/0.0.0#fromJsonString.Therefore, in this case,
operand1andoperand2aredecimaland notfloat. This is why the cast fails.The solution is to provide the exact type when doing the
fromJsonStringWithTypefromJsonStringWithTypewill creates a new value doing the numeric conversions, not a cast. Here thefromJsonStringstep will still result indecimalvalues for these fields, but thefromJsonWithTypestep will handle the numeric conversion when cloning.