Search code examples
ckotlinpointersavrokotlin-native

Problem with using Kotlin c-interop for Avro C


We are making a kotlin multiplatform app with the use of Avro. Avro java is being used for android and the plan is to use Avro C for iOS. We have Avro C integrated into the kotlin app, but we are having issues with the c-interop functions. I think it has something to do with the way we are passing in our out variables (references).

Kotlin code:

var json: String =
                "{"
                "  \"type\": \"record\","
                "  \"name\": \"list\","
                "  \"fields\": ["
                "    { \"name\": \"x\", \"type\": \"int\" },"
                "    { \"name\": \"y\", \"type\": \"int\" },"
                "    { \"name\": \"next\", \"type\": [\"null\",\"list\"]}"
                "  ]"
                "}";

var avro_c_schema: avro_schema_t? = null
var error: avro_schema_error_t? = null

var resultInt = avro_schema_from_json(json, json.length, cValuesOf(avro_c_schema), cValuesOf(error))

The c function is returning 0, which means there were not any errors, but the reference variables avro_c_schema and error are null. My guess it has something to do with the interop, maybe we are not passing them in incorrectly.

Here's avro_schema_from_json_t function source code.

And this is Avro C documentation.


Solution

  • From cValuesOf documentation:

    Returns sequence of immutable values CValues to pass them to C code.

    So I don't think that the correct method in your case. Try this instead:

    memScoped {
        val avro_c_schema = alloc<avro_schema_t>()
        val error = alloc<avro_schema_error_t>()
    
        var resultInt = avro_schema_from_json(json, json.length, avro_c_schema.ptr, error.ptr)
    }