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, )
| 150 | // generateConvertFileForPath generates a single convert.go file for the given path |
| 151 | // containing the specified conversions and creations |
| 152 | func 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 | ) |
no test coverage detected