github.com/blp1526/goa@v1.4.0/goagen/codegen/publicizer_test.go (about)

     1  package codegen_test
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/goadesign/goa/design"
     7  	"github.com/goadesign/goa/dslengine"
     8  	"github.com/goadesign/goa/goagen/codegen"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("Struct publicize code generation", func() {
    14  	Describe("Publicizer", func() {
    15  		var att *design.AttributeDefinition
    16  		var sourceField, targetField string
    17  		Context("given a simple field", func() {
    18  			BeforeEach(func() {
    19  				att = &design.AttributeDefinition{Type: design.Integer}
    20  				sourceField = "source"
    21  				targetField = "target"
    22  			})
    23  			Context("with init false", func() {
    24  				It("simply copies the field over", func() {
    25  					publication := codegen.Publicizer(att, sourceField, targetField, false, 0, false)
    26  					Ω(publication).Should(Equal(fmt.Sprintf("%s = %s", targetField, sourceField)))
    27  				})
    28  			})
    29  			Context("with init true", func() {
    30  				It("initializes and copies the field copies the field over", func() {
    31  					publication := codegen.Publicizer(att, sourceField, targetField, false, 0, true)
    32  					Ω(publication).Should(Equal(fmt.Sprintf("%s := %s", targetField, sourceField)))
    33  				})
    34  			})
    35  		})
    36  		Context("given an object field", func() {
    37  			BeforeEach(func() {
    38  				att = &design.AttributeDefinition{
    39  					Type: design.Object{
    40  						"foo": &design.AttributeDefinition{Type: design.String},
    41  						"bar": &design.AttributeDefinition{Type: design.Any},
    42  						"baz": &design.AttributeDefinition{Type: design.Any},
    43  					},
    44  					Validation: &dslengine.ValidationDefinition{
    45  						Required: []string{"bar"},
    46  					},
    47  				}
    48  				sourceField = "source"
    49  				targetField = "target"
    50  			})
    51  			It("copies the struct fields", func() {
    52  				publication := codegen.Publicizer(att, sourceField, targetField, false, 0, false)
    53  				Ω(publication).Should(Equal(objectPublicizeCode))
    54  			})
    55  		})
    56  		Context("given a user type", func() {
    57  			BeforeEach(func() {
    58  				att = &design.AttributeDefinition{
    59  					Type: &design.UserTypeDefinition{
    60  						AttributeDefinition: &design.AttributeDefinition{
    61  							Type: &design.Object{
    62  								"foo": &design.AttributeDefinition{Type: design.String},
    63  							},
    64  						},
    65  						TypeName: "TheUserType",
    66  					},
    67  				}
    68  				sourceField = "source"
    69  				targetField = "target"
    70  			})
    71  			It("calls Publicize on the source field", func() {
    72  				publication := codegen.Publicizer(att, sourceField, targetField, false, 0, false)
    73  				Ω(publication).Should(Equal(fmt.Sprintf("%s = %s.Publicize()", targetField, sourceField)))
    74  			})
    75  		})
    76  		Context("given an array field", func() {
    77  			Context("that contains primitive fields", func() {
    78  				BeforeEach(func() {
    79  					att = &design.AttributeDefinition{
    80  						Type: &design.Array{
    81  							ElemType: &design.AttributeDefinition{
    82  								Type: design.String,
    83  							},
    84  						},
    85  					}
    86  					sourceField = "source"
    87  					targetField = "target"
    88  				})
    89  				It("copies the array fields", func() {
    90  					publication := codegen.Publicizer(att, sourceField, targetField, false, 0, false)
    91  					Ω(publication).Should(Equal(fmt.Sprintf("%s = %s", targetField, sourceField)))
    92  				})
    93  			})
    94  			Context("that contains user defined fields", func() {
    95  				BeforeEach(func() {
    96  					att = &design.AttributeDefinition{
    97  						Type: &design.Array{
    98  							ElemType: &design.AttributeDefinition{
    99  								Type: &design.UserTypeDefinition{
   100  									AttributeDefinition: &design.AttributeDefinition{
   101  										Type: design.Object{
   102  											"foo": &design.AttributeDefinition{Type: design.String},
   103  										},
   104  									},
   105  									TypeName: "TheUserType",
   106  								},
   107  							},
   108  						},
   109  					}
   110  					sourceField = "source"
   111  					targetField = "target"
   112  				})
   113  				It("copies the array fields", func() {
   114  					publication := codegen.Publicizer(att, sourceField, targetField, false, 0, false)
   115  					Ω(publication).Should(Equal(arrayPublicizeCode))
   116  				})
   117  			})
   118  		})
   119  		Context("given a hash field", func() {
   120  			Context("that contains primitive fields", func() {
   121  				BeforeEach(func() {
   122  					att = &design.AttributeDefinition{
   123  						Type: &design.Hash{
   124  							KeyType: &design.AttributeDefinition{
   125  								Type: design.String,
   126  							},
   127  							ElemType: &design.AttributeDefinition{
   128  								Type: design.String,
   129  							},
   130  						},
   131  					}
   132  					sourceField = "source"
   133  					targetField = "target"
   134  				})
   135  				It("copies the hash fields", func() {
   136  					publication := codegen.Publicizer(att, sourceField, targetField, false, 0, false)
   137  					Ω(publication).Should(Equal(fmt.Sprintf("%s = %s", targetField, sourceField)))
   138  				})
   139  			})
   140  			Context("that contains user defined fields", func() {
   141  				BeforeEach(func() {
   142  					att = &design.AttributeDefinition{
   143  						Type: &design.Hash{
   144  							KeyType: &design.AttributeDefinition{
   145  								Type: &design.UserTypeDefinition{
   146  									AttributeDefinition: &design.AttributeDefinition{
   147  										Type: &design.Object{
   148  											"foo": &design.AttributeDefinition{Type: design.String},
   149  										},
   150  									},
   151  									TypeName: "TheKeyType",
   152  								},
   153  							},
   154  							ElemType: &design.AttributeDefinition{
   155  								Type: &design.UserTypeDefinition{
   156  									AttributeDefinition: &design.AttributeDefinition{
   157  										Type: &design.Object{
   158  											"bar": &design.AttributeDefinition{Type: design.String},
   159  										},
   160  									},
   161  									TypeName: "TheElemType",
   162  								},
   163  							},
   164  						},
   165  					}
   166  					sourceField = "source"
   167  					targetField = "target"
   168  				})
   169  				It("copies the hash fields", func() {
   170  					publication := codegen.Publicizer(att, sourceField, targetField, false, 0, false)
   171  					Ω(publication).Should(Equal(hashPublicizeCode))
   172  				})
   173  			})
   174  		})
   175  	})
   176  })
   177  
   178  const (
   179  	objectPublicizeCode = `target = &struct {
   180  	Bar interface{} ` + "`" + `form:"bar" json:"bar" yaml:"bar" xml:"bar"` + "`" + `
   181  	Baz interface{} ` + "`" + `form:"baz,omitempty" json:"baz,omitempty" yaml:"baz,omitempty" xml:"baz,omitempty"` + "`" + `
   182  	Foo *string ` + "`" + `form:"foo,omitempty" json:"foo,omitempty" yaml:"foo,omitempty" xml:"foo,omitempty"` + "`" + `
   183  }{}
   184  if source.Bar != nil {
   185  	target.Bar = source.Bar
   186  }
   187  if source.Baz != nil {
   188  	target.Baz = source.Baz
   189  }
   190  if source.Foo != nil {
   191  	target.Foo = source.Foo
   192  }`
   193  
   194  	arrayPublicizeCode = `target = make([]*TheUserType, len(source))
   195  for i0, elem0 := range source {
   196  	target[i0] = elem0.Publicize()
   197  }`
   198  
   199  	hashPublicizeCode = `target = make(map[*TheKeyType]*TheElemType, len(source))
   200  for k0, v0 := range source {
   201  	var pubk0 *TheKeyType
   202  	if k0 != nil {
   203  		pubk0 = k0.Publicize()
   204  	}
   205  	var pubv0 *TheElemType
   206  	if v0 != nil {
   207  		pubv0 = v0.Publicize()
   208  	}
   209  	target[pubk0] = pubv0
   210  }`
   211  )