dummyMultipartFile returns a dummy implementation of the multipart decoders and encoders.
(genpkg string, root *expr.RootExpr, svc *expr.HTTPServiceExpr, services *ServicesData)
| 133 | // dummyMultipartFile returns a dummy implementation of the multipart decoders |
| 134 | // and encoders. |
| 135 | func dummyMultipartFile(genpkg string, root *expr.RootExpr, svc *expr.HTTPServiceExpr, services *ServicesData) *codegen.File { |
| 136 | mpath := "multipart.go" |
| 137 | if _, err := os.Stat(mpath); !os.IsNotExist(err) { |
| 138 | return nil // file already exists, skip it. |
| 139 | } |
| 140 | var ( |
| 141 | sections []*codegen.SectionTemplate |
| 142 | mustGen bool |
| 143 | |
| 144 | scope = codegen.NewNameScope() |
| 145 | ) |
| 146 | // determine the unique API package name different from the service names |
| 147 | for _, httpSvc := range root.API.HTTP.Services { |
| 148 | s := services.Get(httpSvc.Name()) |
| 149 | if s == nil { |
| 150 | panic("unknown http service, " + httpSvc.Name()) // bug |
| 151 | } |
| 152 | if s.Service == nil { |
| 153 | panic("unknown service, " + httpSvc.Name()) // bug |
| 154 | } |
| 155 | scope.Unique(s.Service.PkgName) |
| 156 | } |
| 157 | { |
| 158 | specs := make([]*codegen.ImportSpec, 0, 2) |
| 159 | specs = append(specs, &codegen.ImportSpec{Path: "mime/multipart"}) |
| 160 | data := services.Get(svc.Name()) |
| 161 | specs = append(specs, &codegen.ImportSpec{ |
| 162 | Path: path.Join(genpkg, data.Service.PathName), |
| 163 | Name: scope.Unique(data.Service.PkgName, "svc"), |
| 164 | }) |
| 165 | |
| 166 | apiPkg := scope.Unique(strings.ToLower(codegen.Goify(root.API.Name, false)), "api") |
| 167 | sections = []*codegen.SectionTemplate{codegen.Header("", apiPkg, specs)} |
| 168 | for _, e := range data.Endpoints { |
| 169 | if e.MultipartRequestDecoder != nil { |
| 170 | mustGen = true |
| 171 | sections = append(sections, &codegen.SectionTemplate{ |
| 172 | Name: "dummy-multipart-request-decoder", |
| 173 | Source: httpTemplates.Read(dummyMultipartRequestDecoderT), |
| 174 | Data: e.MultipartRequestDecoder, |
| 175 | }) |
| 176 | } |
| 177 | if e.MultipartRequestEncoder != nil { |
| 178 | mustGen = true |
| 179 | sections = append(sections, &codegen.SectionTemplate{ |
| 180 | Name: "dummy-multipart-request-encoder", |
| 181 | Source: httpTemplates.Read(dummyMultipartRequestEncoderT), |
| 182 | Data: e.MultipartRequestEncoder, |
| 183 | }) |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | if !mustGen { |
| 188 | return nil |
| 189 | } |
| 190 | return &codegen.File{ |
| 191 | Path: mpath, |
| 192 | SectionTemplates: sections, |
no test coverage detected