Пример #1
0
func (g *ModelGenerator) Write(writer io.Writer, t typewriter.Type) error {
	tag, found := t.FindTag(g)

	if !found {
		return nil
	}

	// Fetch core template
	coreTemplates := templates.Where(func(t *typewriter.Template) bool {
		if t != nil && t.Name == "core" {
			return true
		}

		return false
	})
	if len(coreTemplates) == 0 {
		return errors.New("Unable to find 'core' template")
	}
	coreTemplate, err := coreTemplates[0].Parse()
	if err != nil {
		return errors.New("Unable to parse 'core' template")
	}

	// Fetch relation template
	relationTemplates := templates.Where(func(t *typewriter.Template) bool {
		if t != nil && t.Name == "relation" {
			return true
		}

		return false
	})
	if len(relationTemplates) == 0 {
		return errors.New("Unable to find 'relation' template")
	}
	relationTemplate, err := relationTemplates[0].Parse()
	if err != nil {
		return errors.New("Unable to parse 'relation' template")
	}

	cValues := coreValues{ModelClassName: t.Name}
	if err := coreTemplate.Execute(writer, cValues); err != nil {
		return err
	}

	for _, relation := range tag.Values {
		templateValues := relationValues{}

		templateValues.ModelClassName = t.Name
		templateValues.RelationIdentifierName = fmt.Sprintf("%vIdentifier", strings.Title(relation.Name))
		templateValues.RelationName = relation.Name
		templateValues.RelationClassName = strings.Title(relation.Name)

		if err := relationTemplate.Execute(writer, templateValues); err != nil {
			return err
		}
	}

	return nil
}
func (sw *SequenceGeneratorWriter) Write(w io.Writer, t typewriter.Type) error {
	tag, found := t.FindTag(sw)
	if !found {
		return nil
	}

	if _, err := w.Write([]byte(license)); err != nil {
		return err
	}

	generatorName := t.Name
	for _, v := range tag.Values {
		switch v.Name {
		case "Iterator":
			if len(v.TypeParameters) == 0 {
				return errors.New("No type parameters for Iterate tag")
			}
			typeParameters := v.TypeParameters
			v.TypeParameters = make([]typewriter.Type, 0)

			tmpl, err := templates.ByTagValue(t, v)
			if err != nil {
				return err
			}
			for _, typ := range typeParameters {
				typeName := typ.Pointer.String() + typ.Name
				if typeName == "*bigInt" {
					typeName = "*big.Int"
				} else if typeName == "*bigRat" {
					typeName = "*bigRat"
				}
				typeTitle := typeTitle[typeName]
				if typeTitle == "" {
					return fmt.Errorf("Unexpected type: %s", typeName)
				}
				tmpl.Execute(w, generatorModel{generatorName, typeName, typeTitle})
			}
			return nil
		default:
			return fmt.Errorf("Unrecognized tag: %s", v.Name)
		}
	}

	return nil
}
Пример #3
0
func (tw *writer) Write(w io.Writer, t typewriter.Type) error {
	tag, found := t.FindTag(tw)

	if !found {
		return nil
	}

	header := "// DO NOT MODIFY. Auto-generated code."
	if _, err := w.Write([]byte(header)); err != nil {
		return err
	}

	tmpl, err := templates.ByTag(t, tag)
	if err != nil {
		return err
	}

	if err := tmpl.Execute(w, t); err != nil {
		return err
	}
	return nil
}
Пример #4
0
func (sw *SymTableWriter) Write(w io.Writer, t typewriter.Type) error {
	tag, found := t.FindTag(sw)

	if !found {
		// nothing to be done
		return nil
	}

	log.Printf("Found tag %+v\n", tag)

	license := `// SymTableWriter is a modification of https://github.com/deckarep/golang-set
// The MIT License (MIT)
// Copyright (c) 2015 Yo-An Lin ([email protected])
`
	if _, err := w.Write([]byte(license)); err != nil {
		return err
	}

	log.Printf("Finding template by tag %+v\n", tag)
	tmpl, err := templates.ByTag(t, tag)
	if err != nil {
		return err
	}

	m := model{
		Type:          t,
		Name:          t.Name,
		TypeParameter: tag.Values[0].TypeParameters[0],
		Tag:           tag,
	}

	log.Println("Rendering templates")
	if err := tmpl.Execute(w, m); err != nil {
		return err
	}

	log.Println("Done!")
	return nil
}
Пример #5
0
func (cw *ContainerWriter) Write(w io.Writer, typ typewriter.Type) error {
	containerTag, found := typ.FindTag(cw)

	if !found {
		return nil
	}

	if _, err := w.Write([]byte(license)); err != nil {
		return err
	}

	for _, tagValue := range containerTag.Values {
		if tmpl, err := templates.ByTagValue(typ, tagValue); err != nil {
			return err
		} else if m, err := typeAndTagValueToModel(typ, tagValue); err != nil {
			return err
		} else if err := tmpl.Execute(w, *m); err != nil {
			return err
		}
	}

	return nil
}
Пример #6
0
func (fw *Writer) Write(w io.Writer, typ typewriter.Type) error {
	tag, found := typ.FindTag(fw)

	if !found {
		return nil
	}

	tmpl, err := templates.ByTag(typ, tag)
	if err != nil {
		return err
	}

	m := model{
		Type:      typ,
		SliceName: SliceName(typ),
	}

	if err := tmpl.Execute(w, m); err != nil {
		return err
	}

	return nil
}
Пример #7
0
func (sw *SliceWriter) Write(w io.Writer, typ typewriter.Type) error {
	tag, found := typ.FindTag(sw)

	if !found {
		return nil
	}

	if includeSortImplementation(tag.Values) {
		s := `// Sort implementation is a modification of http://golang.org/pkg/sort/#Sort
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found at http://golang.org/LICENSE.

`
		w.Write([]byte(s))
	}

	// start with the slice template
	tmpl, err := templates.ByTag(typ, tag)

	if err != nil {
		return err
	}

	m := model{
		Type:      typ,
		SliceName: SliceName(typ),
	}

	if err := tmpl.Execute(w, m); err != nil {
		return err
	}

	for _, v := range tag.Values {
		var tp typewriter.Type

		if len(v.TypeParameters) > 0 {
			tp = v.TypeParameters[0]
		}

		m := model{
			Type:          typ,
			SliceName:     SliceName(typ),
			TypeParameter: tp,
			TagValue:      v,
		}

		tmpl, err := templates.ByTagValue(typ, v)

		if err != nil {
			return err
		}

		if err := tmpl.Execute(w, m); err != nil {
			return err
		}
	}

	if includeSortInterface(tag.Values) {
		tmpl, err := sortInterface.Parse()

		if err != nil {
			return err
		}

		if err := tmpl.Execute(w, m); err != nil {
			return err
		}
	}

	if includeSortImplementation(tag.Values) {
		tmpl, err := sortImplementation.Parse()

		if err != nil {
			return err
		}

		if err := tmpl.Execute(w, m); err != nil {
			return err
		}
	}

	return nil
}