本文整理匯總了Golang中github.com/clipperhouse/gen/typewriter.Type.String方法的典型用法代碼示例。如果您正苦於以下問題:Golang Type.String方法的具體用法?Golang Type.String怎麽用?Golang Type.String使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/clipperhouse/gen/typewriter.Type
的用法示例。
在下文中一共展示了Type.String方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: WriteHeader
func (g *GenWriter) WriteHeader(w io.Writer, t typewriter.Type) {
err := g.ensureValidation(t)
if err != nil {
panic(err)
}
m, exists := g.models[t.String()]
if !exists {
return
}
s := `// See http://clipperhouse.github.io/gen for documentation
`
w.Write([]byte(s))
if includeSortSupport(m.methods) {
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))
}
return
}
示例2: ensureValidation
// genwriter prepares models for later use in the .Validate() method. It must be called prior.
func (g *GenWriter) ensureValidation(t typewriter.Type) error {
if !g.validated[t.String()] {
return fmt.Errorf("Type '%s' has not been previously validated. TypeWriter.Validate() must be called on all types before using them in subsequent methods.", t.String())
}
return nil
}
示例3: Validate
func (c *ContainerWriter) Validate(t typewriter.Type) (bool, error) {
tag, found, err := t.Tags.ByName("containers")
if !found || err != nil {
return false, err
}
// must include at least one item that we recognize
any := false
for _, item := range tag.Items {
if templates.Contains(item) {
// found one, move on
any = true
break
}
}
if !any {
// not an error, but irrelevant
return false, nil
}
c.tagsByType[t.String()] = tag
return true, nil
}
示例4: Validate
func (c ContainerWriter) Validate(t typewriter.Type) (bool, error) {
tag, found, err := t.Tags.ByName("containers")
if found && err == nil {
c.tagsByType[t.String()] = tag
}
return found, err
}
示例5: Imports
func (g *GenWriter) Imports(t typewriter.Type) (result []typewriter.ImportSpec) {
err := g.ensureValidation(t)
if err != nil {
panic(err)
}
m, exists := g.models[t.String()]
if !exists {
return
}
imports := make(map[string]bool)
methodRequiresErrors := map[string]bool{
"First": true,
"Single": true,
"Max": true,
"Min": true,
"MaxBy": true,
"MinBy": true,
"Average": true,
}
methodRequiresSort := map[string]bool{
"Sort": true,
}
for _, s := range m.methods {
if methodRequiresErrors[s] {
imports["errors"] = true
}
if methodRequiresSort[s] {
imports["sort"] = true
}
}
for _, p := range m.projections {
if methodRequiresErrors[p.Method] {
imports["errors"] = true
}
if methodRequiresSort[p.Method] {
imports["sort"] = true
}
}
for s := range imports {
result = append(result, typewriter.ImportSpec{
Path: s,
})
}
return
}
示例6: WriteHeader
func (c *ContainerWriter) WriteHeader(w io.Writer, t typewriter.Type) {
tag := c.tagsByType[t.String()] // validated above
s := `// See http://clipperhouse.github.io/gen for documentation
`
w.Write([]byte(s))
var list, ring, set bool
for _, s := range tag.Items {
if s == "List" {
list = true
}
if s == "Ring" {
ring = true
}
if s == "Set" {
set = true
}
}
if list {
license := `// List is a modification of http://golang.org/pkg/container/list/
`
w.Write([]byte(license))
}
if ring {
license := `// Ring is a modification of http://golang.org/pkg/container/ring/
`
w.Write([]byte(license))
}
if list || ring {
license := `// 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(license))
}
if set {
license := `// Set is a modification of https://github.com/deckarep/golang-set
// The MIT License (MIT)
// Copyright (c) 2013 Ralph Caraveo ([email protected])
`
w.Write([]byte(license))
}
return
}
示例7: WriteBody
func (g *GenWriter) WriteBody(w io.Writer, t typewriter.Type) {
err := g.ensureValidation(t)
if err != nil {
panic(err)
}
m, exists := g.models[t.String()]
if !exists {
return
}
tmpl, _ := standardTemplates.Get("plural")
if err := tmpl.Execute(w, m); err != nil {
panic(err)
}
for _, s := range m.methods {
tmpl, _ := standardTemplates.Get(s) // already validated above
err := tmpl.Execute(w, m)
if err != nil {
panic(err)
}
}
for _, p := range m.projections {
tmpl, _ := projectionTemplates.Get(p.Method) // already validated above
err := tmpl.Execute(w, p)
if err != nil {
panic(err)
}
}
if includeSortInterface(m.methods) {
tmpl, _ := standardTemplates.Get("sortInterface") // already validated above
err := tmpl.Execute(w, m)
if err != nil {
panic(err)
}
}
if includeSortSupport(m.methods) {
tmpl, _ := standardTemplates.Get("sortSupport") // already validated above
err := tmpl.Execute(w, m)
if err != nil {
panic(err)
}
}
}
示例8: WriteBody
func (c *ContainerWriter) WriteBody(w io.Writer, t typewriter.Type) {
tag := c.tagsByType[t.String()] // validated above
for _, s := range tag.Items {
tmpl, err := templates.Get(s) // validate above to avoid err check here?
if err != nil {
continue
}
err = tmpl.Execute(w, t)
if err != nil {
fmt.Println(err)
continue
}
}
return
}
示例9: Validate
// Validates that the tag on the gen type has correctly instructed this
// typewriter to generate code. If the first return value is false, then
// none of the write methods are called.
func (tw *SimpleTypewriter) Validate(twt typewriter.Type) (bool, error) {
tag, found, err := twt.Tags.ByName(plural(tw.name))
if !found || err != nil {
return false, err
}
items := tw.itemsToGenerate(tag.Items)
fmt.Printf("%s: items for %s: %v\n", tw.Name(), twt.String(), items)
if len(items) == 0 {
// not an error, but irrelevant to generation
return false, nil
}
tw.itemsForTypeName[twt.String()] = items
return true, nil
}
示例10: TestValidate
func TestValidate(t *testing.T) {
g := NewGenWriter()
pkg := typewriter.NewPackage("dummy", "SomePackage")
typ := typewriter.Type{
Package: pkg,
Name: "SomeType",
Tags: typewriter.Tags{},
}
if g.validated[typ.String()] {
t.Errorf("type should not show having been validated yet")
}
if err := g.ensureValidation(typ); err == nil {
t.Errorf("ensure validation should return err prior to validation")
}
valid, err := g.Validate(typ)
if !valid || err != nil {
t.Errorf("type should be valid")
}
if !g.validated[typ.String()] {
t.Errorf("type should show having been validated")
}
if err := g.ensureValidation(typ); err != nil {
t.Errorf("ensure validation should not return err after validation")
}
if _, ok := g.models[typ.String()]; !ok {
t.Errorf("type should appear in g.models")
}
if m := g.models[typ.String()]; len(m.methods) == 0 {
t.Errorf("model without tags should have methods")
}
if m := g.models[typ.String()]; len(m.projections) != 0 {
t.Errorf("model without tags should have no projections")
}
typ2 := typewriter.Type{
Package: pkg,
Name: "SomeType2",
Tags: typewriter.Tags{
typewriter.Tag{
Name: "projections",
Items: []string{"int", "string"},
},
},
}
valid2, err2 := g.Validate(typ2)
if !valid2 || err2 != nil {
t.Errorf("type should be valid")
}
if m := g.models[typ2.String()]; len(m.projections) == 0 {
t.Errorf("model with projections tag should have projections")
}
typ3 := typewriter.Type{
Package: pkg,
Name: "SomeType3",
Tags: typewriter.Tags{
typewriter.Tag{
Name: "projections",
Items: []string{"int", "Foo"},
},
},
}
valid3, err3 := g.Validate(typ3)
if valid3 {
t.Errorf("type with unknown projection type should be invalid")
}
if err3 == nil {
t.Errorf("type with unknown projection should return error")
}
if !strings.Contains(err3.Error(), "Foo") {
t.Errorf("type with unknown projection type should mention the unknown projection type; got %v", err3)
}
if !strings.Contains(err3.Error(), typ3.Name) {
t.Errorf("type with unknown projection type should mention the type on which it was declared; got %v", err3)
}
if m := g.models[typ2.String()]; len(m.projections) == 0 {
t.Errorf("model with projections tag should have projections")
}
typ4 := typewriter.Type{
//.........這裏部分代碼省略.........
示例11: evaluateTags
// This business exists because I overload the methods tag to specify both standard and projection methods.
// Kind of a mess, but for the end user, arguably simpler. And arguably not.
func evaluateTags(t typewriter.Type) (standardMethods, projectionMethods []string, err error) {
var nilMethods, nilProjections bool
methods, found, methodsErr := t.Tags.ByName("methods")
if methodsErr != nil {
err = methodsErr
return
}
nilMethods = !found // non-existent methods tag is different than empty
_, found, projectionsErr := t.Tags.ByName("projections")
if projectionsErr != nil {
err = projectionsErr
return
}
nilProjections = !found
if nilMethods || methods.Negated {
// default to all
standardMethods = standardTemplates.GetAllKeys()
if !nilProjections {
projectionMethods = projectionTemplates.GetAllKeys()
}
}
if !nilMethods {
// categorize subsetted methods as standard or projection
std := make([]string, 0)
prj := make([]string, 0)
// collect unknowns for err later
unknown := make([]string, 0)
for _, m := range methods.Items {
isStd := standardTemplates.Contains(m)
if isStd {
std = append(std, m)
}
// only consider projection methods in presence of projected types
isPrj := !nilProjections && projectionTemplates.Contains(m)
if isPrj {
prj = append(prj, m)
}
if !isStd && !isPrj {
unknown = append(unknown, m)
}
}
if methods.Negated {
standardMethods = remove(standardMethods, std...)
projectionMethods = remove(projectionMethods, prj...)
} else {
standardMethods = std
projectionMethods = prj
}
if len(unknown) > 0 {
err = fmt.Errorf("method(s) %v on type %s are unknown", unknown, t.String())
return
}
}
return
}
示例12: Validate
func (g *GenWriter) Validate(t typewriter.Type) (bool, error) {
standardMethods, projectionMethods, err := evaluateTags(t)
if err != nil {
return false, err
}
// filter methods applicable to type
for _, s := range standardMethods {
tmpl, ok := standardTemplates[s]
if !ok {
err = fmt.Errorf("unknown method %v on type %s", s, t.String())
return false, err
}
if !tmpl.ApplicableTo(t) {
standardMethods = remove(standardMethods, s)
}
}
projectionTag, found, err := t.Tags.ByName("projections")
if err != nil {
return false, err
}
m := model{
Type: t,
methods: standardMethods,
}
if found {
for _, s := range projectionTag.Items {
projectionType, err := t.Package.Eval(s)
if err != nil {
return false, fmt.Errorf("unable to identify type %s, projected on %s (%s)", s, t.Name, err)
}
for _, pm := range projectionMethods {
tmpl, ok := projectionTemplates[pm]
if !ok {
return false, fmt.Errorf("unknown projection method %v", pm)
}
if tmpl.ApplicableTo(projectionType) {
m.projections = append(m.projections, Projection{
Method: pm,
Type: s,
Parent: &m,
})
}
}
}
}
g.validated[t.String()] = true
// only add to models if we are going to do something with it
if len(m.methods) > 0 || len(m.projections) > 0 {
g.models[t.String()] = m
}
return true, nil
}
示例13: WriteBody
// Write the actual body.
func (tw *SimpleTypewriter) WriteBody(wrt io.Writer, twt typewriter.Type) {
for _, item := range tw.itemsForTypeName[twt.String()] {
tw.writeTemplateBody(item, wrt, twt)
}
return
}
示例14: WriteHeader
// Write headersForItemType for the generated file. This would include licensing, credits
// or anything else that you wanted to put in there. This can depend upon the
// items selected.
func (tw *SimpleTypewriter) WriteHeader(wrt io.Writer, twt typewriter.Type) {
for _, item := range tw.itemsForTypeName[twt.String()] {
tw.writeHeaderForItem(item, wrt)
}
}