Example #1
0
// GenerateMainFile handles the main file generation for persistent
// connection rpc server
func (g *Generator) Generate(context *common.Context, schema *schema.Schema) ([]common.Output, error) {
	moduleName := context.ModuleNameFunc(schema.Title)
	outputs := make([]common.Output, 0)

	for _, def := range schema.Definitions {
		// create models only for objects
		if def.Type != nil {
			if t, ok := def.Type.(string); ok {
				if t != "object" {
					continue
				}
			}
		}

		f, err := generateMainFile(context, schema)
		if err != nil {
			return nil, err
		}

		path := fmt.Sprintf(
			"%s%s/main.go",
			context.Config.Target,
			moduleName,
		)

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

	}

	return outputs, nil

}
Example #2
0
// 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
}
Example #3
0
// Generate generates the tests for 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)

	// Generate test functions
	testFuncs, err := GenerateTestFuncs(s)
	if err != nil {
		return nil, err
	}

	path := fmt.Sprintf("%stests/testfuncs.go", context.Config.Target)

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

	// generate module test file
	mainTest, err := GenerateMainTestFileForModule(s)
	if err != nil {
		return nil, err
	}

	path = fmt.Sprintf(
		"%sworkers/%s/tests/common_test.go",
		context.Config.Target,
		moduleName,
	)

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

	// generate tests for the schema
	for _, def := range s.Definitions {
		testFile, err := GenerateTests(s.Title, def)
		if err != nil {
			return nil, err
		}
		path = fmt.Sprintf(
			"%sworkers/%s/tests/%s_test.go",
			context.Config.Target,
			moduleName,
			strings.ToLower(def.Title),
		)

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

	}

	return outputs, nil
}
Example #4
0
File: js.go Project: 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
}
Example #5
0
// 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
}
Example #6
0
// 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
}