github.com/hamba/avro@v1.8.0/example_test.go (about)

     1  package avro_test
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"log"
     7  
     8  	"github.com/hamba/avro"
     9  )
    10  
    11  func ExampleParse() {
    12  	schema, err := avro.Parse(`{
    13  	    "type": "record",
    14  	    "name": "simple",
    15  	    "namespace": "org.hamba.avro",
    16  	    "fields" : [
    17  	        {"name": "a", "type": "long"},
    18  	        {"name": "b", "type": "string"}
    19  	    ]
    20  	}`)
    21  	if err != nil {
    22  		log.Fatal(err)
    23  	}
    24  
    25  	fmt.Println(schema.Type())
    26  	// Outputs: record
    27  }
    28  
    29  func ExampleNewDecoder() {
    30  	schema := `{
    31  	    "type": "record",
    32  	    "name": "simple",
    33  	    "namespace": "org.hamba.avro",
    34  	    "fields" : [
    35  	        {"name": "a", "type": "long"},
    36  	        {"name": "b", "type": "string"}
    37  	    ]
    38  	}`
    39  
    40  	type SimpleRecord struct {
    41  		A int64  `avro:"a"`
    42  		B string `avro:"b"`
    43  	}
    44  
    45  	r := bytes.NewReader([]byte{}) // Your reader goes here
    46  	decoder, err := avro.NewDecoder(schema, r)
    47  	if err != nil {
    48  		fmt.Println("error:", err)
    49  	}
    50  
    51  	simple := SimpleRecord{}
    52  	if err := decoder.Decode(&simple); err != nil {
    53  		fmt.Println("error:", err)
    54  	}
    55  
    56  	fmt.Printf("%+v", simple)
    57  }
    58  
    59  func ExampleNewDecoderForSchema() {
    60  	schema := avro.MustParse(`{
    61  	    "type": "record",
    62  	    "name": "simple",
    63  	    "namespace": "org.hamba.avro",
    64  	    "fields" : [
    65  	        {"name": "a", "type": "long"},
    66  	        {"name": "b", "type": "string"}
    67  	    ]
    68  	}`)
    69  
    70  	type SimpleRecord struct {
    71  		A int64  `avro:"a"`
    72  		B string `avro:"b"`
    73  	}
    74  
    75  	r := bytes.NewReader([]byte{0x36, 0x06, 0x66, 0x6F, 0x6F}) // Your reader goes here
    76  	decoder := avro.NewDecoderForSchema(schema, r)
    77  
    78  	simple := SimpleRecord{}
    79  	if err := decoder.Decode(&simple); err != nil {
    80  		fmt.Println("error:", err)
    81  	}
    82  
    83  	fmt.Printf("%+v", simple)
    84  
    85  	//Output: {A:27 B:foo}
    86  }
    87  
    88  func ExampleUnmarshal() {
    89  	schema := avro.MustParse(`{
    90  	    "type": "record",
    91  	    "name": "simple",
    92  	    "namespace": "org.hamba.avro",
    93  	    "fields" : [
    94  	        {"name": "a", "type": "long"},
    95  	        {"name": "b", "type": "string"}
    96  	    ]
    97  	}`)
    98  
    99  	type SimpleRecord struct {
   100  		A int64  `avro:"a"`
   101  		B string `avro:"b"`
   102  	}
   103  
   104  	data := []byte{0x36, 0x06, 0x66, 0x6F, 0x6F} // Your Avro data here
   105  	simple := SimpleRecord{}
   106  	if err := avro.Unmarshal(schema, data, &simple); err != nil {
   107  		fmt.Println("error:", err)
   108  	}
   109  
   110  	fmt.Printf("%+v", simple)
   111  
   112  	//Output: {A:27 B:foo}
   113  }
   114  
   115  func ExampleNewEncoder() {
   116  	schema := `{
   117  	    "type": "record",
   118  	    "name": "simple",
   119  	    "namespace": "org.hamba.avro",
   120  	    "fields" : [
   121  	        {"name": "a", "type": "long"},
   122  	        {"name": "b", "type": "string"}
   123  	    ]
   124  	}`
   125  
   126  	type SimpleRecord struct {
   127  		A int64  `avro:"a"`
   128  		B string `avro:"b"`
   129  	}
   130  
   131  	w := &bytes.Buffer{}
   132  	encoder, err := avro.NewEncoder(schema, w)
   133  	if err != nil {
   134  		fmt.Println("error:", err)
   135  	}
   136  
   137  	simple := SimpleRecord{A: 27, B: "foo"}
   138  	if err := encoder.Encode(simple); err != nil {
   139  		fmt.Println("error:", err)
   140  	}
   141  
   142  	fmt.Println(w.Bytes())
   143  
   144  	//Output: [54 6 102 111 111]
   145  }
   146  
   147  func ExampleNewEncoderForSchema() {
   148  	schema := avro.MustParse(`{
   149  	    "type": "record",
   150  	    "name": "simple",
   151  	    "namespace": "org.hamba.avro",
   152  	    "fields" : [
   153  	        {"name": "a", "type": "long"},
   154  	        {"name": "b", "type": "string"}
   155  	    ]
   156  	}`)
   157  
   158  	type SimpleRecord struct {
   159  		A int64  `avro:"a"`
   160  		B string `avro:"b"`
   161  	}
   162  
   163  	w := &bytes.Buffer{}
   164  	encoder := avro.NewEncoderForSchema(schema, w)
   165  
   166  	simple := SimpleRecord{A: 27, B: "foo"}
   167  	if err := encoder.Encode(simple); err != nil {
   168  		fmt.Println("error:", err)
   169  	}
   170  
   171  	fmt.Println(w.Bytes())
   172  
   173  	//Output: [54 6 102 111 111]
   174  }
   175  
   176  func ExampleMarshal() {
   177  	schema := avro.MustParse(`{
   178  	    "type": "record",
   179  	    "name": "simple",
   180  	    "namespace": "org.hamba.avro",
   181  	    "fields" : [
   182  	        {"name": "a", "type": "long"},
   183  	        {"name": "b", "type": "string"}
   184  	    ]
   185  	}`)
   186  
   187  	type SimpleRecord struct {
   188  		A int64  `avro:"a"`
   189  		B string `avro:"b"`
   190  	}
   191  
   192  	simple := SimpleRecord{A: 27, B: "foo"}
   193  	b, err := avro.Marshal(schema, simple)
   194  	if err != nil {
   195  		fmt.Println("error:", err)
   196  	}
   197  
   198  	fmt.Println(b)
   199  
   200  	//Output: [54 6 102 111 111]
   201  }