我想为以下Typescript代码的模式注册表创建一个Avro模式:
export type Value = {
[key: string]:
| Value
| Value[]
| string
| number;
};
它是递归映射类型。我知道可以像下面这样创建递归记录,但它是不同的用例。
export type Node = {
value: number;
leafs: Node[];
}
我尝试了不同的方法,包括命名类型和模式引用,但都导致发布模式时出现验证错误。
需要但无效的简化模式(不包括递归数组)如下所示:
{
"type": "record",
"name": "Value",
"namespace": "com.namespace",
"fields": [
{ "name": "itemValues", "type": { "type": "map", "values": ["string", "int", "itemValues"] } }
]
}
此模式的大多数变体都会导致错误:org. apache.avro.SchemaParseException:未定义名称:“itemValue”
我找不到类似场景的例子,想知道是否有可能创建这样的场景?限制很可能是Avro中缺乏命名联合和映射类型。
我想实现JSON一个例子:
{
"itemValues": {
"validA": "sth",
"validB": [],
"validC": 8,
"recursiveProperty": {
"anyMap": { "sth": "else" }
}
}
}
你的schema的问题在于你的值
列表是["string","int","itemValue"]
,但是解析器在抱怨,因为你已经告诉它应该有某种类型itemValue
而你还没有定义。你定义的唯一类型是Value
。
这是固定模式(包括添加Value
数组作为潜在类型之一:
{
"type": "record",
"name": "Value",
"namespace": "com.namespace",
"fields": [
{ "name": "itemValues", "type": { "type": "map", "values": ["string", "int", "Value", {"type": "array", "items": "Value"}] } }
]
}