github.com/shogo82148/goa-v1@v1.6.2/goagen/codegen/publicizer_test.go (about)

     1  package codegen_test
     2  
     3  import (
     4  	"fmt"
     5  
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/gomega"
     8  	"github.com/shogo82148/goa-v1/design"
     9  	"github.com/shogo82148/goa-v1/dslengine"
    10  	"github.com/shogo82148/goa-v1/goagen/codegen"
    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 an object field with struct:field:name metadata", func() {
    57  			BeforeEach(func() {
    58  				att = &design.AttributeDefinition{
    59  					Type: design.Object{
    60  						"foo": &design.AttributeDefinition{
    61  							Type:     design.String,
    62  							Metadata: dslengine.MetadataDefinition{"struct:field:name": []string{"MetaFoo"}},
    63  						},
    64  						"bar": &design.AttributeDefinition{
    65  							Type:     design.Any,
    66  							Metadata: dslengine.MetadataDefinition{"struct:field:name": []string{"MetaBar"}},
    67  						},
    68  						"baz": &design.AttributeDefinition{
    69  							Type:     design.Any,
    70  							Metadata: dslengine.MetadataDefinition{"struct:field:name": []string{"MetaBaz"}},
    71  						},
    72  					},
    73  					Validation: &dslengine.ValidationDefinition{
    74  						Required: []string{"bar"},
    75  					},
    76  				}
    77  				sourceField = "source"
    78  				targetField = "target"
    79  			})
    80  			It("copies the struct fields", func() {
    81  				publication := codegen.Publicizer(att, sourceField, targetField, false, 0, false)
    82  				Ω(publication).Should(Equal(objectPublicizeCodeWithMeta))
    83  			})
    84  		})
    85  		Context("given a user type", func() {
    86  			BeforeEach(func() {
    87  				att = &design.AttributeDefinition{
    88  					Type: &design.UserTypeDefinition{
    89  						AttributeDefinition: &design.AttributeDefinition{
    90  							Type: &design.Object{
    91  								"foo": &design.AttributeDefinition{Type: design.String},
    92  							},
    93  						},
    94  						TypeName: "TheUserType",
    95  					},
    96  				}
    97  				sourceField = "source"
    98  				targetField = "target"
    99  			})
   100  			It("calls Publicize on the source field", func() {
   101  				publication := codegen.Publicizer(att, sourceField, targetField, false, 0, false)
   102  				Ω(publication).Should(Equal(fmt.Sprintf("%s = %s.Publicize()", targetField, sourceField)))
   103  			})
   104  		})
   105  		Context("given an array field", func() {
   106  			Context("that contains primitive fields", func() {
   107  				BeforeEach(func() {
   108  					att = &design.AttributeDefinition{
   109  						Type: &design.Array{
   110  							ElemType: &design.AttributeDefinition{
   111  								Type: design.String,
   112  							},
   113  						},
   114  					}
   115  					sourceField = "source"
   116  					targetField = "target"
   117  				})
   118  				It("copies the array fields", func() {
   119  					publication := codegen.Publicizer(att, sourceField, targetField, false, 0, false)
   120  					Ω(publication).Should(Equal(fmt.Sprintf("%s = %s", targetField, sourceField)))
   121  				})
   122  			})
   123  			Context("that contains user defined fields", func() {
   124  				BeforeEach(func() {
   125  					att = &design.AttributeDefinition{
   126  						Type: &design.Array{
   127  							ElemType: &design.AttributeDefinition{
   128  								Type: &design.UserTypeDefinition{
   129  									AttributeDefinition: &design.AttributeDefinition{
   130  										Type: design.Object{
   131  											"foo": &design.AttributeDefinition{Type: design.String},
   132  										},
   133  									},
   134  									TypeName: "TheUserType",
   135  								},
   136  							},
   137  						},
   138  					}
   139  					sourceField = "source"
   140  					targetField = "target"
   141  				})
   142  				It("copies the array fields", func() {
   143  					publication := codegen.Publicizer(att, sourceField, targetField, false, 0, false)
   144  					Ω(publication).Should(Equal(arrayPublicizeCode))
   145  				})
   146  			})
   147  		})
   148  		Context("given a hash field", func() {
   149  			Context("that contains primitive fields", func() {
   150  				BeforeEach(func() {
   151  					att = &design.AttributeDefinition{
   152  						Type: &design.Hash{
   153  							KeyType: &design.AttributeDefinition{
   154  								Type: design.String,
   155  							},
   156  							ElemType: &design.AttributeDefinition{
   157  								Type: design.String,
   158  							},
   159  						},
   160  					}
   161  					sourceField = "source"
   162  					targetField = "target"
   163  				})
   164  				It("copies the hash fields", func() {
   165  					publication := codegen.Publicizer(att, sourceField, targetField, false, 0, false)
   166  					Ω(publication).Should(Equal(fmt.Sprintf("%s = %s", targetField, sourceField)))
   167  				})
   168  			})
   169  			Context("that contains user defined fields", func() {
   170  				BeforeEach(func() {
   171  					att = &design.AttributeDefinition{
   172  						Type: &design.Hash{
   173  							KeyType: &design.AttributeDefinition{
   174  								Type: &design.UserTypeDefinition{
   175  									AttributeDefinition: &design.AttributeDefinition{
   176  										Type: &design.Object{
   177  											"foo": &design.AttributeDefinition{Type: design.String},
   178  										},
   179  									},
   180  									TypeName: "TheKeyType",
   181  								},
   182  							},
   183  							ElemType: &design.AttributeDefinition{
   184  								Type: &design.UserTypeDefinition{
   185  									AttributeDefinition: &design.AttributeDefinition{
   186  										Type: &design.Object{
   187  											"bar": &design.AttributeDefinition{Type: design.String},
   188  										},
   189  									},
   190  									TypeName: "TheElemType",
   191  								},
   192  							},
   193  						},
   194  					}
   195  					sourceField = "source"
   196  					targetField = "target"
   197  				})
   198  				It("copies the hash fields", func() {
   199  					publication := codegen.Publicizer(att, sourceField, targetField, false, 0, false)
   200  					Ω(publication).Should(Equal(hashPublicizeCode))
   201  				})
   202  			})
   203  		})
   204  	})
   205  })
   206  
   207  const (
   208  	objectPublicizeCode = `target = &struct {
   209  	Bar interface{} ` + "`" + `form:"bar" json:"bar" yaml:"bar" xml:"bar"` + "`" + `
   210  	Baz interface{} ` + "`" + `form:"baz,omitempty" json:"baz,omitempty" yaml:"baz,omitempty" xml:"baz,omitempty"` + "`" + `
   211  	Foo *string ` + "`" + `form:"foo,omitempty" json:"foo,omitempty" yaml:"foo,omitempty" xml:"foo,omitempty"` + "`" + `
   212  }{}
   213  if source.Bar != nil {
   214  	target.Bar = source.Bar
   215  }
   216  if source.Baz != nil {
   217  	target.Baz = source.Baz
   218  }
   219  if source.Foo != nil {
   220  	target.Foo = source.Foo
   221  }`
   222  
   223  	objectPublicizeCodeWithMeta = `target = &struct {
   224  	MetaBar interface{} ` + "`" + `form:"bar" json:"bar" yaml:"bar" xml:"bar"` + "`" + `
   225  	MetaBaz interface{} ` + "`" + `form:"baz,omitempty" json:"baz,omitempty" yaml:"baz,omitempty" xml:"baz,omitempty"` + "`" + `
   226  	MetaFoo *string ` + "`" + `form:"foo,omitempty" json:"foo,omitempty" yaml:"foo,omitempty" xml:"foo,omitempty"` + "`" + `
   227  }{}
   228  if source.MetaBar != nil {
   229  	target.MetaBar = source.MetaBar
   230  }
   231  if source.MetaBaz != nil {
   232  	target.MetaBaz = source.MetaBaz
   233  }
   234  if source.MetaFoo != nil {
   235  	target.MetaFoo = source.MetaFoo
   236  }`
   237  
   238  	arrayPublicizeCode = `target = make([]*TheUserType, len(source))
   239  for i0, elem0 := range source {
   240  	target[i0] = elem0.Publicize()
   241  }`
   242  
   243  	hashPublicizeCode = `target = make(map[*TheKeyType]*TheElemType, len(source))
   244  for k0, v0 := range source {
   245  	var pubk0 *TheKeyType
   246  	if k0 != nil {
   247  		pubk0 = k0.Publicize()
   248  	}
   249  	var pubv0 *TheElemType
   250  	if v0 != nil {
   251  		pubv0 = v0.Publicize()
   252  	}
   253  	target[pubk0] = pubv0
   254  }`
   255  )