예제 #1
0
파일: setwriter.go 프로젝트: rickb777/set
func (sw *SetWriter) Write(w io.Writer, t typewriter.Type) error {
	tag, found := t.FindTag(sw.Name())

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

	license := `// Set is a modification of https://github.com/deckarep/golang-set
// The MIT License (MIT)
// Copyright (c) 2013 Ralph Caraveo ([email protected])
`

	if _, err := w.Write([]byte(license)); 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
}
예제 #2
0
파일: model.go 프로젝트: rickb777/golist
func newModel(typ typewriter.Type, flags flags) model {
	p := ""
	d := "*"
	a := ""
	if typ.Pointer {
		p = "*"
		d = ""
		a = "&"
	}
	return model{
		Type:  typ,
		TName: typ.Name,
		PName: typ.String(),
		Ptr:   p,
		Deref: d,
		Addr:  a,
		Has:   flags,
	}
}
예제 #3
0
func (sw *StringerWriter) Write(w io.Writer, t typewriter.Type) error {
	_, found := t.FindTag(sw.Name())

	if !found {
		return nil
	}

	if err := sw.g.parsePackageDir("./"); err != nil {
		return err
	}

	if err := sw.g.generate(t.Name); err != nil {
		return err
	}

	if _, err := io.Copy(w, &sw.g.buf); err != nil {
		return err
	}

	return nil
}
예제 #4
0
func (xw *xWriter) Write(w io.Writer, typ typewriter.Type) error {
	//	fmt.Printf("ListWriter.Write %s %+v\n", typ.String(), typ.Tags)
	tag, found := typ.FindTag(xw.Name())

	if !found {
		return nil
	}

	flags := newFlags().setFlag(xw.name)
	for _, v := range tag.Values {
		flags = flags.setFlag(v.Name)
	}

	// enable collection for the core template
	flags.Collection = true

	// start with the core template
	model := newModel(typ, flags)
	//	fmt.Printf("writeBasicTemplate\n  %+v\n", m)
	if err := writeTextTemplate(w, xw.coreTemplate, model); err != nil {
		return err
	}

	// disable collection for the secondary templates
	flags.Collection = false

	for _, v := range tag.Values {
		err := xw.writeForTag(w, typ, v, flags)
		if err != nil {
			return err
		}
	}

	fmt.Fprintf(w, "// %s flags: %+v\n", xw.name, flags)
	return nil
}
예제 #5
0
func (sw *SliceWriter) Write(w io.Writer, typ typewriter.Type) error {
	tag, found := typ.FindTag(sw.Name())

	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
}