levo

package module
v0.0.0-...-868ac54 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2014 License: Apache-2.0 Imports: 16 Imported by: 0

README

Code Generation With Levolib

At the most basic level, this library accepts a context and produces source code files. The information within a context acts like instructions; it provides levolib with the models and templates it will need, instructions regarding which model(s) to use when filling each template, and a few other things as well.

Levolib provides BeginContext which returns an initalized context. A context object provides methods for adding new templates and models and mappings to itself. Once the context is complete, it can be passed to levolib's ProcessMappings method, which will start generating source code files.

Building a context piece by piece isn't necessary. Levolib also provides GetJSONConfigurationAdapter, which returns an adapter specialized in converting a json configuration file into a context. Simply pass the file path to the adapter's ProcessConfigurationFile method.

The levo project is a useful example of how the above methods can be used to instrument this library. It also provides example files, include an example json configuration.

License

This library is licensed under the Apache License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0

Code-Generator Context Structure

type Context struct {
	ProjectName      string
	PackageName      string
	TemplaterVersion string
	Schema           Schema
	Templates        []TemplateInfo
	Mappings         []TemplatesForModels
	Language         string
	Zip              bool
}

type Schema struct {
	Project string
	Models  []Model
}

type Model struct {
	Name       string
	Parent     string
	ParentRef  *Model
	Properties []ModelProperty
}

type ModelProperty struct {
	RemoteIdentifier   string
	LocalIdentifier string
	PropertyType   string
}

type TemplateInfo struct {
	Language  string
	Version   string
	Directory string
	FileName  string
	Body      []byte
	Adapter   OutputAdapter
}

type TemplatesForModels struct {
	Models    []*Model
	Templates []*TemplateInfo
}

type GeneratedFile struct {
	FileName  string
	Directory string
	Body      []byte
}

type OutputAdapter interface {
	GenerateFiles(templateInfo TemplateInfo, templateData TemplateData) ([]GeneratedFile, error)
}

type GeneratedFile struct {
	FileName string
	Body     string
}

func (context *Context) AddModelWithName(name string) (*Model, error)
//Initalize a new Model with a specific name. Add that Model to the Context

func (context *Context) AddModel(model Model) (*Model, error)
//Add a Model object to the Context

func (context *Context) AddTemplateDirectory(templateDirPath string) error
//Parse a directory and all it's subdirectories. Update the Context with a new Template
//for every file found.

func (context *Context) AddTemplateFilePath(filePath string) (TemplateInfo, error)
//Add the file at filePath to the Context as a Template

func (context *Context) AddTemplate(fileName string, body []byte, version string, directory string, adapter OutputAdapter) (*TemplateInfo, error)
//Initalize a template with the information provided and add that TemplateInfo to the Context
//fileName          : Exactly what it seems like
//body              : The contents of the template file
//version           : The template version. Used to ensure that levolib is able to process the template.
//directory         : The path to the template file (can be a relative path). This directory will be
//                    used to determine where the output of the template goes.
//adapter           : This library comes with a GoTemplateAdapter for processing go templates.
//                    Other adapters can be created. For example, adapters for Mako templates or
//                    xslt templates. 

func (context *Context) AddTemplatesForModelsMapping(templateFileNames []string, modelNames []string) error
//Specify which models should be used to fill a template (or a set of templates).

func (context *Context) ModelForName(name string) (*Model, error)
//Simple getter method

func (context *Context) TemplateForFileName(fileName string) (*TemplateInfo, error)
//Simple getter method

Code-Generatior Structure

func BeginContext(packageName string, language string, version string) (Context, error)
//Get an initialized context object.

func ProcessMappings(context Context) ([]GeneratedFile, error)
//Create source code files using the contents of this context

func GetJSONConfigurationAdapter() JSONConfigAdapter
//An adapter for converting JSON configuration files into Contexts. The adapter provides
//**ProcessConfigurationFile** and **ProcessConfigurationString**, each of which returns a Context
//based on the JSON configuration information they are fed

func GetJSONSchemaAdapter() JSONSchemaAdapter
//An adapter for converting JSON schema files into Contexts. The adapter provides
//**ProcessSchemaFile** and **ProcessSchemaString**, each of which returns a Context
//based on the JSON schema information they are fed

Documentation

Overview

Copyright (C) 2014 Pivotal Software, Inc.

All rights reserved. This program and the accompanying materials are made available under the terms of the under the Apache License, Version 2.0 (the "License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (C) 2014 Pivotal Software, Inc.

All rights reserved. This program and the accompanying materials are made available under the terms of the under the Apache License, Version 2.0 (the "License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (C) 2014 Pivotal Software, Inc.

All rights reserved. This program and the accompanying materials are made available under the terms of the under the Apache License, Version 2.0 (the "License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (C) 2014 Pivotal Software, Inc.

All rights reserved. This program and the accompanying materials are made available under the terms of the under the Apache License, Version 2.0 (the "License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (C) 2014 Pivotal Software, Inc.

All rights reserved. This program and the accompanying materials are made available under the terms of the under the Apache License, Version 2.0 (the "License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const LibraryVersion string = "1.0.0"

Variables

View Source
var CustomType map[string]map[string]string

Functions

func Camelcase

func Camelcase(input string) string

func Concat

func Concat(first string, second string) string

func CoreDataTypes

func CoreDataTypes() map[string]string

func HasListType

func HasListType(model Model) bool

func IdProp

func IdProp(properties []ModelProperty) string

func IsCustomType

func IsCustomType(customType string, prop ModelProperty) bool

func IsJavaType

func IsJavaType(prop ModelProperty) bool

func IsSqliteType

func IsSqliteType(prop ModelProperty) bool

func JavaTypes

func JavaTypes() map[string]string

func Lower

func Lower(input string) string

func PackageToPath

func PackageToPath(input string) string

func Pluralize

func Pluralize(input string) string

func Prefix

func Prefix(prefix string, original string) string

func RailsTypes

func RailsTypes() map[string]string

func RegisterCustomType

func RegisterCustomType(customType string) string

func SHA256

func SHA256(data string) string

func SetCustomType

func SetCustomType(customType string, key string, value string) string

func Snakecase

func Snakecase(input string) string

func SqliteTypes

func SqliteTypes() map[string]string

func Suffix

func Suffix(suffix string, original string) string

func TestEquality

func TestEquality(args ...interface{}) bool

func TestInequality

func TestInequality(args ...interface{}) bool

func Titlecase

func Titlecase(input string) string

func ToCoreDataType

func ToCoreDataType(input string) string

func ToCustomType

func ToCustomType(customType string, prop ModelProperty) string

func ToJavaType

func ToJavaType(prop ModelProperty) string

func ToRailsType

func ToRailsType(prop ModelProperty) string

func ToSqliteType

func ToSqliteType(prop ModelProperty) string

func Truncate

func Truncate(targetLength int, tooLong string) string

func Upper

func Upper(input string) string

Types

type ConfigurationAdapter

type ConfigurationAdapter interface {
	ProcessConfigurationFile(configFile *os.File) (Context, error)
	ProcessConfigurationString(configString string) (Context, error)
}

type Context

type Context struct {
	ProjectName      string
	PackageName      string
	TemplaterVersion string
	Schema           Schema
	Templates        []TemplateInfo
	Mappings         []TemplatesForModels
	Language         string
	TemplateFeatures map[string]bool
	GoAdapter        GoTemplateAdapter
}

func BeginContext

func BeginContext() Context

func (*Context) AddModel

func (context *Context) AddModel(model Model) (*Model, error)

func (*Context) AddModelWithName

func (context *Context) AddModelWithName(name string) (*Model, error)

func (*Context) AddTemplate

func (context *Context) AddTemplate(fileName string, body []byte, version string, directory string, adapter OutputAdapter) (*TemplateInfo, error)

func (*Context) AddTemplateDirectory

func (context *Context) AddTemplateDirectory(templateDirPath string) ([]TemplateInfo, error)

func (*Context) AddTemplateFeature

func (context *Context) AddTemplateFeature(feature string)

func (*Context) AddTemplateFile

func (context *Context) AddTemplateFile(path string, info os.FileInfo, err error) error

func (*Context) AddTemplateFilePath

func (context *Context) AddTemplateFilePath(filePath string) (TemplateInfo, error)

func (*Context) AddTemplatesForModelsMapping

func (context *Context) AddTemplatesForModelsMapping(templateFileNames []string, modelNames []string) error

func (*Context) FindTemplate

func (context *Context) FindTemplate(fileName string, directory string) (*TemplateInfo, error)

func (*Context) ModelForName

func (context *Context) ModelForName(name string) (*Model, error)

func (*Context) RemoveTemplateFeature

func (context *Context) RemoveTemplateFeature(feature string)

func (*Context) TemplateForFileName

func (context *Context) TemplateForFileName(fileName string) ([]*TemplateInfo, error)

type GeneratedFile

type GeneratedFile struct {
	FileName  string
	Directory string
	Body      []byte
}

func ProcessMappings

func ProcessMappings(context Context) ([]GeneratedFile, error)

type GoTemplateAdapter

type GoTemplateAdapter struct {
	ParsedTemplates *template.Template
}

func (*GoTemplateAdapter) GenerateFiles

func (self *GoTemplateAdapter) GenerateFiles(templateInfo TemplateInfo, templateData TemplateData) ([]GeneratedFile, error)

func (*GoTemplateAdapter) GetFilesFromOutput

func (self *GoTemplateAdapter) GetFilesFromOutput(buffer *bytes.Buffer, directory string) ([]GeneratedFile, error)

func (*GoTemplateAdapter) ParseTemplate

func (self *GoTemplateAdapter) ParseTemplate(templateInfo TemplateInfo) error

type JSONSchemaAdapter

type JSONSchemaAdapter struct{}

func GetJSONSchemaAdapter

func GetJSONSchemaAdapter() JSONSchemaAdapter

func (*JSONSchemaAdapter) ParseModelSchemaString

func (self *JSONSchemaAdapter) ParseModelSchemaString(schemaString []byte) (Schema, error)

func (*JSONSchemaAdapter) ProcessSchemaFile

func (self *JSONSchemaAdapter) ProcessSchemaFile(schemaPath string) (Schema, error)

type Model

type Model struct {
	Name       string
	Parent     string
	ParentRef  *Model
	Properties []ModelProperty
}

func (*Model) AddProperty

func (model *Model) AddProperty(remoteIdentifier string, localIdentifier string, propertyType string) (*ModelProperty, error)

type ModelProperty

type ModelProperty struct {
	RemoteIdentifier string
	LocalIdentifier  string
	PropertyType     string
	IsSetType        bool
}

type OutputAdapter

type OutputAdapter interface {
	ParseTemplate(template TemplateInfo) error
	GenerateFiles(templateInfo TemplateInfo, templateData TemplateData) ([]GeneratedFile, error)
	GetFilesFromOutput(buffer *bytes.Buffer, directory string) ([]GeneratedFile, error)
}

type Schema

type Schema struct {
	Project string
	Models  []Model
}

type SchemaAdapter

type SchemaAdapter interface {
	ProcessSchemaFile(schemaFile string) (Schema, error)
	ProcessSchemaString(schemaString string) (Schema, error)
}

type TemplateData

type TemplateData struct {
	PackageName string
	ProjectName string
	PackagePath string
	Models      []Model
	Features    map[string]bool
}

type TemplateInfo

type TemplateInfo struct {
	Language  string
	Version   string
	Directory string
	FileName  string
	Body      []byte
	Adapter   OutputAdapter
}

type TemplatesForModels

type TemplatesForModels struct {
	Models    []*Model
	Templates []*TemplateInfo
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL