예제 #1
0
파일: statements.go 프로젝트: Zenithar/gene
// Generate generates the basic CRUD statements for the models
func (g *Generator) Generate(context *common.Context, s *schema.Schema) ([]common.Output, error) {
	outputs := make([]common.Output, 0)

	moduleName := context.FieldNameFunc(s.Title)

	settings := geneddl.GenerateSettings(geneddl.GeneratorName, moduleName, s)

	for _, def := range common.SortedObjectSchemas(s.Definitions) {

		settingsDef := geneddl.SetDefaultSettings(geneddl.GeneratorName, settings, def)
		settingsDef.Set("tableName", context.FieldNameFunc(def.Title))

		f, err := GenerateModelStatements(context, settingsDef, def)
		if err != nil {
			return outputs, err
		}

		outputs = append(outputs, common.Output{
			Content: f,
			Path: fmt.Sprintf(
				"%s%s_statements.go",
				context.Config.Target,
				strings.ToLower(def.Title),
			),
		})

	}

	return outputs, nil
}
예제 #2
0
파일: js.go 프로젝트: Zenithar/gene
// Generate generates and writes the errors of the schema
func (g *generator) Generate(context *common.Context, s *schema.Schema) ([]common.Output, error) {
	moduleName := context.ModuleNameFunc(s.Title)
	outputs := make([]common.Output, 0)

	for _, def := range common.SortedObjectSchemas(s.Definitions) {
		output, err := GenerateAPI(context.Config.Target, moduleName, def)
		if err != nil {
			return nil, err
		}

		outputs = append(outputs, output)
	}

	return outputs, nil
}
예제 #3
0
파일: functions.go 프로젝트: Zenithar/gene
// Generate generates and writes the errors of the schema
func (g *Generator) Generate(context *common.Context, s *schema.Schema) ([]common.Output, error) {
	temp := template.New("constructors.tmpl").Funcs(context.TemplateFuncs)
	if _, err := temp.Parse(FunctionsTemplate); err != nil {
		return nil, err
	}

	moduleName := context.ModuleNameFunc(s.Title)

	outputs := make([]common.Output, 0)

	for _, def := range common.SortedObjectSchemas(s.Definitions) {

		data := struct {
			ModuleName string
			Schema     *schema.Schema
		}{
			ModuleName: moduleName,
			Schema:     def,
		}

		var buf bytes.Buffer

		if err := temp.ExecuteTemplate(&buf, "constructors.tmpl", data); err != nil {
			return nil, err
		}

		path := fmt.Sprintf(
			"%s%s/api/%s.go",
			context.Config.Target,
			moduleName,
			strings.ToLower(s.Title),
		)

		api, err := format.Source(buf.Bytes())
		if err != nil {
			return nil, err
		}

		outputs = append(outputs, common.Output{
			Content: api,
			Path:    path,
		})
	}

	return outputs, nil
}
예제 #4
0
파일: clients.go 프로젝트: Zenithar/gene
// Generate generates the client package for given schema
func (c *Generator) Generate(context *common.Context, s *schema.Schema) ([]common.Output, error) {
	tmpl := template.New("clients.tmpl").Funcs(context.TemplateFuncs)
	if _, err := tmpl.Parse(ClientsTemplate); err != nil {
		return nil, err
	}

	moduleName := context.ModuleNameFunc(s.Title)
	outputs := make([]common.Output, 0)

	for _, def := range common.SortedObjectSchemas(s.Definitions) {

		var buf bytes.Buffer

		data := struct {
			ModuleName string
			Schema     *schema.Schema
		}{
			ModuleName: moduleName,
			Schema:     def,
		}

		if err := tmpl.Execute(&buf, data); err != nil {
			return nil, err
		}

		f, err := format.Source(buf.Bytes())
		if err != nil {
			return nil, err
		}

		path := fmt.Sprintf(
			"%s%s/clients/%s.go",
			context.Config.Target,
			moduleName,
			context.FileNameFunc(def.Title),
		)

		outputs = append(outputs, common.Output{Content: f, Path: path})
	}

	return outputs, nil
}
예제 #5
0
파일: errors.go 프로젝트: Zenithar/gene
// Generate generates and writes the errors of the schema
func (g *Generator) Generate(context *common.Context, s *schema.Schema) ([]common.Output, error) {
	temp := template.New("errors.tmpl").Funcs(context.TemplateFuncs)
	if _, err := temp.Parse(ErrorsTemplate); err != nil {
		return nil, err
	}

	outputs := make([]common.Output, 0)

	for _, def := range common.SortedObjectSchemas(s.Definitions) {
		data := struct {
			Schema *schema.Schema
		}{
			Schema: def,
		}

		var buf bytes.Buffer

		if err := temp.ExecuteTemplate(&buf, "errors.tmpl", data); err != nil {
			return nil, err
		}

		f, err := writers.Clear(buf)
		if err != nil {
			return nil, err
		}

		path := fmt.Sprintf(
			"%s/%s.go",
			context.Config.Target,
			strings.ToLower(def.Title),
		)

		outputs = append(outputs, common.Output{
			Content: f,
			Path:    path,
		})

	}

	return outputs, nil
}
예제 #6
0
파일: models.go 프로젝트: Zenithar/gene
func (g *Generator) Generate(context *common.Context, schema *schema.Schema) ([]common.Output, error) {
	outputs := make([]common.Output, 0)

	for _, def := range common.SortedObjectSchemas(schema.Definitions) {
		f, err := GenerateModel(def)
		if err != nil {
			return nil, err
		}

		path := fmt.Sprintf(
			"%s/%s.go",
			context.Config.Target,
			strings.ToLower(def.Title),
		)

		outputs = append(outputs, common.Output{
			Content: f,
			Path:    path,
		})
	}

	return outputs, nil
}