{"action":"created","issue":{"url":"https://api.github.com/repos/mtth/avsc/issues/208","repository_url":"https://api.github.com/repos/mtth/avsc","labels_url":"https://api.github.com/repos/mtth/avsc/issues/208/labels{/name}","comments_url":"https://api.github.com/repos/mtth/avsc/issues/208/comments","events_url":"https://api.github.com/repos/mtth/avsc/issues/208/events","html_url":"https://github.com/mtth/avsc/issues/208","id":367503025,"node_id":"MDU6SXNzdWUzNjc1MDMwMjU=","number":208,"title":"Can I dynamically add a type to a union?","user":{"login":"djpate","id":512939,"node_id":"MDQ6VXNlcjUxMjkzOQ==","avatar_url":"https://avatars1.githubusercontent.com/u/512939?v=4","gravatar_id":"","url":"https://api.github.com/users/djpate","html_url":"https://github.com/djpate","followers_url":"https://api.github.com/users/djpate/followers","following_url":"https://api.github.com/users/djpate/following{/other_user}","gists_url":"https://api.github.com/users/djpate/gists{/gist_id}","starred_url":"https://api.github.com/users/djpate/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/djpate/subscriptions","organizations_url":"https://api.github.com/users/djpate/orgs","repos_url":"https://api.github.com/users/djpate/repos","events_url":"https://api.github.com/users/djpate/events{/privacy}","received_events_url":"https://api.github.com/users/djpate/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-10-06T22:54:58Z","updated_at":"2018-10-10T00:36:49Z","closed_at":null,"author_association":"NONE","body":"I'm very new to avro but I've been trying to write a blockchain storage where transactions are stored in Avro and I'm stuck.\r\n\r\nLet's say I have the following schema for my block.\r\n\r\n```json\r\n{\r\n  \"type\": \"record\",\r\n  \"name\": \"block\",\r\n  \"fields\": [\r\n    {\r\n      \"name\": \"index\",\r\n      \"type\": \"long\"\r\n    },\r\n    {\r\n      \"name\": \"timestamp\",\r\n      \"type\": {\r\n        \"type\": \"long\",\r\n        \"logicalType\": \"timestamp-millis\"\r\n      }\r\n    },\r\n    {\r\n      \"name\": \"previous_block\",\r\n      \"type\": \"string\"\r\n    },\r\n    {\r\n      \"name\": \"transactions\",\r\n      \"type\": {\r\n        \"type\": \"array\",\r\n        \"items\": [\r\n          {\r\n            \"type\": \"record\",\r\n            \"namespace\": \"transactions\",\r\n            \"name\": \"inception\",\r\n            \"fields\": [\r\n              {\r\n                \"name\": \"id\",\r\n                \"type\": \"string\"\r\n              },\r\n              {\r\n                \"name\": \"timestamp\",\r\n                \"type\": {\r\n                  \"type\": \"long\",\r\n                  \"logicalType\": \"timestamp-millis\"\r\n                }\r\n              }\r\n            ]\r\n          }\r\n        ]\r\n      }\r\n    },\r\n    {\r\n      \"name\": \"signature\",\r\n      \"type\": \"string\"\r\n    }\r\n  ]\r\n}\r\n```\r\n\r\nInitially the only transaction type that exist is a `Inception` event. Now If I want to add a new transaction type say `User` and add it to the possible values in the transactions union is that possible?\r\n\r\nI was going with something like this :\r\n\r\n```js\r\n    addTransactionType(schemaDefinition) {\r\n      let schema = this.schemaForDefinition(schemaDefinition)\r\n      let transactionField = this.models['block'].fields.find((field) => {\r\n        return field.name == 'transactions'\r\n      })\r\n      console.log(transactionField.type)\r\n      // => <ArrayType {\"items\":[\"transactions.inception\"]}>\r\n      //transactionField.type.items.push(schema) <-- doesn't work\r\n    }\r\n```\r\n\r\nAny pointers would be awesome!"},"comment":{"url":"https://api.github.com/repos/mtth/avsc/issues/comments/428398780","html_url":"https://github.com/mtth/avsc/issues/208#issuecomment-428398780","issue_url":"https://api.github.com/repos/mtth/avsc/issues/208","id":428398780,"node_id":"MDEyOklzc3VlQ29tbWVudDQyODM5ODc4MA==","user":{"login":"mtth","id":1216372,"node_id":"MDQ6VXNlcjEyMTYzNzI=","avatar_url":"https://avatars3.githubusercontent.com/u/1216372?v=4","gravatar_id":"","url":"https://api.github.com/users/mtth","html_url":"https://github.com/mtth","followers_url":"https://api.github.com/users/mtth/followers","following_url":"https://api.github.com/users/mtth/following{/other_user}","gists_url":"https://api.github.com/users/mtth/gists{/gist_id}","starred_url":"https://api.github.com/users/mtth/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mtth/subscriptions","organizations_url":"https://api.github.com/users/mtth/orgs","repos_url":"https://api.github.com/users/mtth/repos","events_url":"https://api.github.com/users/mtth/events{/privacy}","received_events_url":"https://api.github.com/users/mtth/received_events","type":"User","site_admin":false},"created_at":"2018-10-10T00:36:49Z","updated_at":"2018-10-10T00:36:49Z","author_association":"OWNER","body":"This will work if you add the schema to the underlying type's _schema_ (rather than to the type itself). Assuming you don't have any custom attributes in your schema, something like this should do the trick:\r\n\r\n```js\r\n// From your snippet, it looks like this.models['block'] is a type.\r\nconst oldSchema = this.models['block'].schema({exportAttrs: true});\r\n// Now add the nested schema to the union.\r\noldSchema.fields\r\n  .find(field => field.name === 'transactions')\r\n  .items.push(schema)\r\n// Finally, rebuild a type with the augmented schema.\r\nconst newType = avro.Type.forSchema(oldSchema);\r\n```\r\n\r\nIf your schemas are custom, [`type.schema()`](https://github.com/mtth/avsc/wiki/API#typeschemaopts) might not return all the information you need and you might need to keep track of the original schemas alongside the types."}}
{"actor":{"display_login":"mtth"}}