You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by mg...@apache.org on 2022/11/14 13:36:38 UTC

[avro] branch branch-1.11 updated: Add README example for usage of recursive schemata in JS (#1965)

This is an automated email from the ASF dual-hosted git repository.

mgrigorov pushed a commit to branch branch-1.11
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/branch-1.11 by this push:
     new 3ef76193f Add README example for usage of recursive schemata in JS (#1965)
3ef76193f is described below

commit 3ef76193f7a1186a06f70a6859afddd6c2bc6525
Author: Benjamin G <52...@users.noreply.github.com>
AuthorDate: Mon Nov 14 14:35:20 2022 +0100

    Add README example for usage of recursive schemata in JS (#1965)
    
    * update js readme
    
    * add negative example
    
    * fix typo
    
    * update comment
    
    * add one more comment
    
    Co-authored-by: Benjamin Graedig <be...@flaschenpost.de>
    (cherry picked from commit 330918c9ab7a772430e4c6b63ecb0376fafa4297)
---
 lang/js/README.md | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/lang/js/README.md b/lang/js/README.md
index c470abaa2..119c1a745 100644
--- a/lang/js/README.md
+++ b/lang/js/README.md
@@ -97,6 +97,47 @@ var avro = require('avro-js');
     .on('data', function (record) { /* Do something with the record. */ });
   ```
 
++ Implement recursive schemata (due to lack of duck-typing):
+
+  ```javascript
+    // example type: linked list with one long-int as element value
+    const recursiveRecordType =  avro.parse({
+      "type": "record",
+      "name": "LongList",
+      "fields" : [
+        {"name": "value", "type": "long"},             
+        {"name": "next", "type": ["null", "LongList"]} // optional next element via recursion
+      ]
+    });
+
+    // will work
+    const validRecursiveRecordDTO = {
+      value: 1,
+      next: {
+        // no duck-typing support: from first nested level on the 
+        // recursive type has to be explicitly specified.
+        LongList: {
+          value: 2,
+          next: null
+        }
+      }
+    };
+    const serializedValid = recursiveRecordType.parse(validRecursiveRecordDTO);
+    
+
+    // will throw error
+    const invalidRecursiveRecordDTO = {
+      value: 1,
+      next: {
+          value: 2,
+          next: null
+      }
+    };
+    const serializedInvalid = recursiveRecordType.parse(invalidRecursiveRecordDTO);
+
+
+  ```
+
 
 [node.js]: https://nodejs.org/en/
 [readable-stream]: https://nodejs.org/api/stream.html#stream_class_stream_readable