Example #1
0
func (a *appGenerator) Generate() error {

	app, err := a.makeCodegenApp()
	if err != nil {
		return err
	}

	if a.DumpData {
		bb, err := json.MarshalIndent(app, "", "  ")
		if err != nil {
			return err
		}
		fmt.Fprintln(os.Stdout, string(bb))
		return nil
	}

	errChan := make(chan error, 100)
	wg := nsync.NewControlWaitGroup(20)

	if a.GenOpts.IncludeModel {
		log.Printf("rendering %d models", len(app.Models))
		for _, mod := range app.Models {
			if len(errChan) > 0 {
				wg.Wait()
				return <-errChan
			}
			modCopy := mod
			wg.Do(func() {
				modCopy.IncludeValidator = true // a.GenOpts.IncludeValidator
				modCopy.IncludeModel = true
				gen := &definitionGenerator{
					Name:             modCopy.Name,
					SpecDoc:          a.SpecDoc,
					Target:           filepath.Join(a.Target, a.ModelsPackage),
					Data:             &modCopy,
					IncludeModel:     true,
					IncludeStruct:    true,
					IncludeValidator: true,
				}
				if err := gen.generateModel(); err != nil {
					errChan <- err
				}
			})
		}
	}
	wg.Wait()

	if a.GenOpts.IncludeHandler {
		for _, opg := range app.OperationGroups {
			opgCopy := opg
			for _, op := range opgCopy.Operations {
				if len(errChan) > 0 {
					wg.Wait()
					return <-errChan
				}
				opCopy := op
				wg.Do(func() {
					gen := &opGen{
						data:              &opCopy,
						pkg:               opgCopy.Name,
						cname:             swag.ToGoName(opCopy.Name),
						IncludeHandler:    a.GenOpts.IncludeHandler,
						IncludeParameters: a.GenOpts.IncludeParameters,
						IncludeResponses:  a.GenOpts.IncludeResponses,
						Doc:               a.SpecDoc,
						Analyzed:          a.Analyzed,
						Target:            filepath.Join(a.Target, a.ServerPackage),
						APIPackage:        a.APIPackage,
					}

					if err := gen.Generate(); err != nil {
						errChan <- err
					}
				})
			}
		}
	}

	if a.GenOpts.IncludeSupport {
		wg.Do(func() {
			if err := a.GenerateSupport(&app); err != nil {
				errChan <- err
			}
		})
	}
	wg.Wait()
	if len(errChan) > 0 {
		return <-errChan
	}
	return nil
}
Example #2
0
func (c *clientGenerator) Generate() error {
	app, err := c.makeCodegenApp()
	if app.Name == "" {
		app.Name = "APIClient"
	}
	app.DefaultImports = []string{filepath.ToSlash(filepath.Join(baseImport(c.Target), c.ModelsPackage))}
	if err != nil {
		return err
	}

	if c.DumpData {
		bb, _ := json.MarshalIndent(swag.ToDynamicJSON(app), "", "  ")
		fmt.Fprintln(os.Stdout, string(bb))
		return nil
	}

	errChan := make(chan error, 100)
	wg := nsync.NewControlWaitGroup(20)

	if c.GenOpts.IncludeModel {
		for _, mod := range app.Models {
			if len(errChan) > 0 {
				wg.Wait()
				return <-errChan
			}
			modCopy := mod
			wg.Do(func() {
				modCopy.IncludeValidator = true // a.GenOpts.IncludeValidator
				gen := &definitionGenerator{
					Name:    modCopy.Name,
					SpecDoc: c.SpecDoc,
					Target:  filepath.Join(c.Target, c.ModelsPackage),
					Data:    &modCopy,
				}
				if err := gen.generateModel(); err != nil {
					errChan <- err
				}
			})
		}
	}

	wg.Wait()
	if c.GenOpts.IncludeHandler {
		sort.Sort(app.OperationGroups)
		for i := range app.OperationGroups {
			opGroup := app.OperationGroups[i]
			opGroup.DefaultImports = []string{filepath.ToSlash(filepath.Join(baseImport(c.Target), c.ModelsPackage))}
			opGroup.RootPackage = c.ClientPackage
			app.OperationGroups[i] = opGroup
			sort.Sort(opGroup.Operations)
			for _, op := range opGroup.Operations {
				if len(errChan) > 0 {
					wg.Wait()
					return <-errChan
				}
				opCopy := op
				if opCopy.Package == "" {
					opCopy.Package = c.Package
				}
				wg.Do(func() {
					if err := c.generateParameters(&opCopy); err != nil {
						errChan <- err
					}
				})
				wg.Do(func() {
					if err := c.generateResponses(&opCopy); err != nil {
						errChan <- err
					}
				})
			}
			app.DefaultImports = append(app.DefaultImports, filepath.ToSlash(filepath.Join(baseImport(c.Target), c.ClientPackage, opGroup.Name)))
			if err := c.generateGroupClient(opGroup); err != nil {
				return err
			}
		}
		wg.Wait()
	}

	if c.GenOpts.IncludeSupport {
		wg.Do(func() {
			if err := c.generateFacade(&app); err != nil {
				errChan <- err
			}
		})
	}

	wg.Wait()

	if len(errChan) > 0 {
		return <-errChan
	}

	return nil
}