github.com/aavshr/aws-sdk-go@v1.41.3/private/model/api/example_test.go (about)

     1  //go:build go1.10 && codegen
     2  // +build go1.10,codegen
     3  
     4  package api
     5  
     6  import (
     7  	"encoding/json"
     8  	"testing"
     9  )
    10  
    11  func buildAPI() *API {
    12  	a := &API{}
    13  
    14  	stringShape := &Shape{
    15  		API:       a,
    16  		ShapeName: "string",
    17  		Type:      "string",
    18  	}
    19  	stringShapeRef := &ShapeRef{
    20  		API:       a,
    21  		ShapeName: "string",
    22  		Shape:     stringShape,
    23  	}
    24  
    25  	intShape := &Shape{
    26  		API:       a,
    27  		ShapeName: "int",
    28  		Type:      "int",
    29  	}
    30  	intShapeRef := &ShapeRef{
    31  		API:       a,
    32  		ShapeName: "int",
    33  		Shape:     intShape,
    34  	}
    35  
    36  	nestedComplexShape := &Shape{
    37  		API:       a,
    38  		ShapeName: "NestedComplexShape",
    39  		MemberRefs: map[string]*ShapeRef{
    40  			"NestedField": stringShapeRef,
    41  		},
    42  		Type: "structure",
    43  	}
    44  
    45  	nestedComplexShapeRef := &ShapeRef{
    46  		API:       a,
    47  		ShapeName: "NestedComplexShape",
    48  		Shape:     nestedComplexShape,
    49  	}
    50  
    51  	nestedListShape := &Shape{
    52  		API:       a,
    53  		ShapeName: "NestedListShape",
    54  		MemberRef: *nestedComplexShapeRef,
    55  		Type:      "list",
    56  	}
    57  
    58  	nestedListShapeRef := &ShapeRef{
    59  		API:       a,
    60  		ShapeName: "NestedListShape",
    61  		Shape:     nestedListShape,
    62  	}
    63  
    64  	complexShape := &Shape{
    65  		API:       a,
    66  		ShapeName: "ComplexShape",
    67  		MemberRefs: map[string]*ShapeRef{
    68  			"Field": stringShapeRef,
    69  			"List":  nestedListShapeRef,
    70  		},
    71  		Type: "structure",
    72  	}
    73  
    74  	complexShapeRef := &ShapeRef{
    75  		API:       a,
    76  		ShapeName: "ComplexShape",
    77  		Shape:     complexShape,
    78  	}
    79  
    80  	listShape := &Shape{
    81  		API:       a,
    82  		ShapeName: "ListShape",
    83  		MemberRef: *complexShapeRef,
    84  		Type:      "list",
    85  	}
    86  
    87  	listShapeRef := &ShapeRef{
    88  		API:       a,
    89  		ShapeName: "ListShape",
    90  		Shape:     listShape,
    91  	}
    92  
    93  	listsShape := &Shape{
    94  		API:       a,
    95  		ShapeName: "ListsShape",
    96  		MemberRef: *listShapeRef,
    97  		Type:      "list",
    98  	}
    99  
   100  	listsShapeRef := &ShapeRef{
   101  		API:       a,
   102  		ShapeName: "ListsShape",
   103  		Shape:     listsShape,
   104  	}
   105  
   106  	input := &Shape{
   107  		API:       a,
   108  		ShapeName: "FooInput",
   109  		MemberRefs: map[string]*ShapeRef{
   110  			"BarShape":     stringShapeRef,
   111  			"ComplexField": complexShapeRef,
   112  			"ListField":    listShapeRef,
   113  			"ListsField":   listsShapeRef,
   114  		},
   115  		Type: "structure",
   116  	}
   117  	output := &Shape{
   118  		API:       a,
   119  		ShapeName: "FooOutput",
   120  		MemberRefs: map[string]*ShapeRef{
   121  			"BazShape":     intShapeRef,
   122  			"ComplexField": complexShapeRef,
   123  			"ListField":    listShapeRef,
   124  			"ListsField":   listsShapeRef,
   125  		},
   126  		Type: "structure",
   127  	}
   128  
   129  	inputRef := ShapeRef{
   130  		API:       a,
   131  		ShapeName: "FooInput",
   132  		Shape:     input,
   133  	}
   134  	outputRef := ShapeRef{
   135  		API:       a,
   136  		ShapeName: "FooOutput",
   137  		Shape:     output,
   138  	}
   139  
   140  	operations := map[string]*Operation{
   141  		"Foo": {
   142  			API:          a,
   143  			Name:         "Foo",
   144  			ExportedName: "Foo",
   145  			InputRef:     inputRef,
   146  			OutputRef:    outputRef,
   147  		},
   148  	}
   149  
   150  	a.Operations = operations
   151  	a.Shapes = map[string]*Shape{
   152  		"FooInput":           input,
   153  		"FooOutput":          output,
   154  		"string":             stringShape,
   155  		"int":                intShape,
   156  		"NestedComplexShape": nestedComplexShape,
   157  		"NestedListShape":    nestedListShape,
   158  		"ComplexShape":       complexShape,
   159  		"ListShape":          listShape,
   160  		"ListsShape":         listsShape,
   161  	}
   162  	a.Metadata = Metadata{
   163  		ServiceAbbreviation: "FooService",
   164  	}
   165  
   166  	a.BaseImportPath = "github.com/aavshr/aws-sdk-go/service/"
   167  
   168  	a.Setup()
   169  	return a
   170  }
   171  
   172  func TestExampleGeneration(t *testing.T) {
   173  	example := `{
   174    "version": "1.0",
   175    "examples": {
   176      "Foo": [
   177        {
   178          "input": {
   179            "BarShape": "Hello world",
   180            "ComplexField": {
   181              "Field": "bar",
   182              "List": [
   183                {
   184                  "NestedField": "qux"
   185                }
   186              ]
   187            },
   188            "ListField": [
   189              {
   190                "Field": "baz"
   191              }
   192            ],
   193            "ListsField": [
   194              [
   195                {
   196                  "Field": "baz"
   197                }
   198              ]
   199            ],
   200            "FieldDoesNotExistInModel": true
   201          },
   202          "output": {
   203            "BazShape": 1
   204          },
   205          "comments": {
   206            "input": {
   207            },
   208            "output": {
   209            }
   210          },
   211          "description": "Foo bar baz qux",
   212          "title": "I pity the foo"
   213        }
   214      ],
   215      "NotInTheModel": [
   216        {
   217          "input": {},
   218          "output": {},
   219          "comments": {
   220            "input": {
   221            },
   222            "output": {
   223            }
   224          },
   225          "description": "Look the other way",
   226          "title": "I am not modeled"
   227        }
   228      ]
   229    }
   230  }
   231  	`
   232  	a := buildAPI()
   233  	def := &ExamplesDefinition{}
   234  	err := json.Unmarshal([]byte(example), def)
   235  	if err != nil {
   236  		t.Error(err)
   237  	}
   238  	def.API = a
   239  
   240  	def.setup()
   241  	expected := `
   242  import (
   243  	"fmt"
   244  	"strings"
   245  	"time"
   246  
   247  	"` + SDKImportRoot + `/aws"
   248  	"` + SDKImportRoot + `/aws/awserr"
   249  	"` + SDKImportRoot + `/aws/session"
   250  	"` + SDKImportRoot + `/service/fooservice"
   251  	
   252  )
   253  
   254  var _ time.Duration
   255  var _ strings.Reader
   256  var _ aws.Config
   257  
   258  func parseTime(layout, value string) *time.Time {
   259  	t, err := time.Parse(layout, value)
   260  	if err != nil {
   261  		panic(err)
   262  	}
   263  	return &t
   264  }
   265  
   266  // I pity the foo
   267  //
   268  // Foo bar baz qux
   269  func ExampleFooService_Foo_shared00() {
   270  	svc := fooservice.New(session.New())
   271  	input := &fooservice.FooInput{
   272  		BarShape: aws.String("Hello world"),
   273  		ComplexField: &fooservice.ComplexShape{
   274  			Field: aws.String("bar"),
   275  			List: []*fooservice.NestedComplexShape{
   276  				{
   277  					NestedField: aws.String("qux"),
   278  				},
   279  			},
   280  		},
   281  		ListField: []*fooservice.ComplexShape{
   282  			{
   283  				Field: aws.String("baz"),
   284  			},
   285  		},
   286  		ListsField: [][]*fooservice.ComplexShape{
   287  			{
   288  				{
   289  					Field: aws.String("baz"),
   290  				},
   291  			},
   292  		},
   293  	}
   294  
   295  	result, err := svc.Foo(input)
   296  	if err != nil {
   297  		if aerr, ok := err.(awserr.Error); ok {
   298  			switch aerr.Code() {
   299  			default:
   300  				fmt.Println(aerr.Error())
   301  			}
   302  		} else {
   303  			// Print the error, cast err to awserr.Error to get the Code and
   304  			// Message from an error.
   305  			fmt.Println(err.Error())
   306  		}
   307  		return
   308  	}
   309  
   310  	fmt.Println(result)
   311  }
   312  `
   313  	if expected != a.ExamplesGoCode() {
   314  		t.Errorf("Expected:\n%s\nReceived:\n%s\n", expected, a.ExamplesGoCode())
   315  	}
   316  }
   317  
   318  func TestBuildShape(t *testing.T) {
   319  	a := buildAPI()
   320  	cases := []struct {
   321  		defs     map[string]interface{}
   322  		expected string
   323  	}{
   324  		{
   325  			defs: map[string]interface{}{
   326  				"barShape": "Hello World",
   327  			},
   328  			expected: "BarShape: aws.String(\"Hello World\"),\n",
   329  		},
   330  		{
   331  			defs: map[string]interface{}{
   332  				"BarShape": "Hello World",
   333  			},
   334  			expected: "BarShape: aws.String(\"Hello World\"),\n",
   335  		},
   336  	}
   337  
   338  	for _, c := range cases {
   339  		ref := a.Operations["Foo"].InputRef
   340  		shapeStr := defaultExamplesBuilder{}.BuildShape(&ref, c.defs, false)
   341  		if c.expected != shapeStr {
   342  			t.Errorf("Expected:\n%s\nReceived:\n%s", c.expected, shapeStr)
   343  		}
   344  	}
   345  }