github.com/aavshr/aws-sdk-go@v1.41.3/service/dynamodb/dynamodbattribute/marshaler_examples_test.go (about)

     1  package dynamodbattribute_test
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  
     7  	"github.com/aavshr/aws-sdk-go/aws"
     8  	"github.com/aavshr/aws-sdk-go/service/dynamodb"
     9  	"github.com/aavshr/aws-sdk-go/service/dynamodb/dynamodbattribute"
    10  )
    11  
    12  func ExampleMarshal() {
    13  	type Record struct {
    14  		Bytes   []byte
    15  		MyField string
    16  		Letters []string
    17  		Numbers []int
    18  	}
    19  
    20  	r := Record{
    21  		Bytes:   []byte{48, 49},
    22  		MyField: "MyFieldValue",
    23  		Letters: []string{"a", "b", "c", "d"},
    24  		Numbers: []int{1, 2, 3},
    25  	}
    26  	av, err := dynamodbattribute.Marshal(r)
    27  	fmt.Println("err", err)
    28  	fmt.Println("Bytes", av.M["Bytes"])
    29  	fmt.Println("MyField", av.M["MyField"])
    30  	fmt.Println("Letters", av.M["Letters"])
    31  	fmt.Println("Numbers", av.M["Numbers"])
    32  
    33  	// Output:
    34  	// err <nil>
    35  	// Bytes {
    36  	//   B: <binary> len 2
    37  	// }
    38  	// MyField {
    39  	//   S: "MyFieldValue"
    40  	// }
    41  	// Letters {
    42  	//   L: [
    43  	//     {
    44  	//       S: "a"
    45  	//     },
    46  	//     {
    47  	//       S: "b"
    48  	//     },
    49  	//     {
    50  	//       S: "c"
    51  	//     },
    52  	//     {
    53  	//       S: "d"
    54  	//     }
    55  	//   ]
    56  	// }
    57  	// Numbers {
    58  	//   L: [{
    59  	//       N: "1"
    60  	//     },{
    61  	//       N: "2"
    62  	//     },{
    63  	//       N: "3"
    64  	//     }]
    65  	// }
    66  }
    67  
    68  func ExampleUnmarshal() {
    69  	type Record struct {
    70  		Bytes   []byte
    71  		MyField string
    72  		Letters []string
    73  		A2Num   map[string]int
    74  	}
    75  
    76  	expect := Record{
    77  		Bytes:   []byte{48, 49},
    78  		MyField: "MyFieldValue",
    79  		Letters: []string{"a", "b", "c", "d"},
    80  		A2Num:   map[string]int{"a": 1, "b": 2, "c": 3},
    81  	}
    82  
    83  	av := &dynamodb.AttributeValue{
    84  		M: map[string]*dynamodb.AttributeValue{
    85  			"Bytes":   {B: []byte{48, 49}},
    86  			"MyField": {S: aws.String("MyFieldValue")},
    87  			"Letters": {L: []*dynamodb.AttributeValue{
    88  				{S: aws.String("a")}, {S: aws.String("b")}, {S: aws.String("c")}, {S: aws.String("d")},
    89  			}},
    90  			"A2Num": {M: map[string]*dynamodb.AttributeValue{
    91  				"a": {N: aws.String("1")},
    92  				"b": {N: aws.String("2")},
    93  				"c": {N: aws.String("3")},
    94  			}},
    95  		},
    96  	}
    97  
    98  	actual := Record{}
    99  	err := dynamodbattribute.Unmarshal(av, &actual)
   100  	fmt.Println(err, reflect.DeepEqual(expect, actual))
   101  
   102  	// Output:
   103  	// <nil> true
   104  }