MCPcopy Create free account
hub / github.com/goadesign/goa / generateConvertFileForPath

Function generateConvertFileForPath

codegen/service/convert.go:152–333  ·  view source on GitHub ↗

generateConvertFileForPath generates a single convert.go file for the given path containing the specified conversions and creations

(
	convertPath string,
	conversions []*expr.TypeMap,
	creations []*expr.TypeMap,
	service *expr.ServiceExpr,
	svc *Data,
)

Source from the content-addressed store, hash-verified

150// generateConvertFileForPath generates a single convert.go file for the given path
151// containing the specified conversions and creations
152func generateConvertFileForPath(
153 convertPath string,
154 conversions []*expr.TypeMap,
155 creations []*expr.TypeMap,
156 service *expr.ServiceExpr,
157 svc *Data,
158) (*codegen.File, error) {
159 if len(conversions) == 0 && len(creations) == 0 {
160 return nil, nil
161 }
162
163 // Determine package name from path
164 var convertPkgName string
165 if len(conversions) > 0 {
166 if loc := codegen.UserTypeLocation(conversions[0].User); loc != nil {
167 convertPkgName = loc.PackageName()
168 } else {
169 convertPkgName = svc.PkgName
170 }
171 } else if len(creations) > 0 {
172 if loc := codegen.UserTypeLocation(creations[0].User); loc != nil {
173 convertPkgName = loc.PackageName()
174 } else {
175 convertPkgName = svc.PkgName
176 }
177 }
178
179 // Retrieve external packages info
180 ppm := make(map[string]string)
181 for _, c := range conversions {
182 pkgImport, alias, err := getExternalTypeInfo(c.External)
183 if err != nil {
184 return nil, err
185 }
186 ppm[pkgImport] = alias
187 }
188 for _, c := range creations {
189 pkgImport, alias, err := getExternalTypeInfo(c.External)
190 if err != nil {
191 return nil, err
192 }
193 ppm[pkgImport] = alias
194 }
195 pkgs := make([]*codegen.ImportSpec, 0, len(ppm)+2)
196 for pp, alias := range ppm {
197 pkgs = append(pkgs, &codegen.ImportSpec{Name: alias, Path: pp})
198 }
199
200 // Build header section
201 pkgs = append(pkgs, &codegen.ImportSpec{Path: "context"}, codegen.GoaImport(""))
202 sections := []*codegen.SectionTemplate{
203 codegen.Header(service.Name+" service type conversion functions", convertPkgName, pkgs),
204 }
205
206 var (
207 names = map[string]struct{}{}
208 transFuncs []*codegen.TransformFunctionData
209 )

Callers 1

ConvertFilesFunction · 0.85

Calls 15

GoTypeNameMethod · 0.95
AddMetaMethod · 0.95
NameMethod · 0.95
UserTypeLocationFunction · 0.92
GoaImportFunction · 0.92
HeaderFunction · 0.92
NewNameScopeFunction · 0.92
NewAttributeContextFunction · 0.92
GoTransformFunction · 0.92
AppendHelpersFunction · 0.92
IsObjectFunction · 0.92

Tested by

no test coverage detected