Empty impls for package text
This commit adds empty structs that implement no-op asserter methods for interfaces in package text. Those implementations have "Text" prefixed to their names. The added implementations stay in the same place as cchat's.
This commit is contained in:
parent
9a64b50703
commit
59778af1dd
|
@ -4,6 +4,8 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/dave/jennifer/jen"
|
||||
"github.com/diamondburned/cchat/cmd/internal/cchat-generator/genutils"
|
||||
|
@ -18,49 +20,55 @@ func init() {
|
|||
|
||||
var comment = repository.Comment{Raw: `
|
||||
Package empty provides no-op asserter method implementations of interfaces
|
||||
in cchat.
|
||||
in cchat's root and text packages.
|
||||
`}
|
||||
|
||||
func main() {
|
||||
pk, ok := repository.Main[repository.RootPath]
|
||||
if !ok {
|
||||
log.Fatalf("Failed to find main namespace %q\n", repository.RootPath)
|
||||
}
|
||||
|
||||
gen := jen.NewFile("empty")
|
||||
gen.HeaderComment("DO NOT EDIT: THIS FILE IS GENERATED!")
|
||||
gen.PackageComment(comment.GoString(1))
|
||||
|
||||
for _, iface := range pk.Interfaces {
|
||||
// Skip structs without asserter methods.
|
||||
if !hasAsserter(iface) {
|
||||
continue
|
||||
}
|
||||
for pkgpath, pk := range repository.Main {
|
||||
gen.ImportName(pkgpath, path.Base(pkgpath))
|
||||
|
||||
gen.Commentf("%[1]s provides no-op asserters for cchat.%[1]s.", iface.Name)
|
||||
gen.Type().Id(iface.Name).Struct()
|
||||
gen.Line()
|
||||
|
||||
for _, method := range iface.Methods {
|
||||
am, ok := method.(repository.AsserterMethod)
|
||||
if !ok {
|
||||
for _, iface := range pk.Interfaces {
|
||||
// Skip structs without asserter methods.
|
||||
if !hasAsserter(iface) {
|
||||
continue
|
||||
}
|
||||
|
||||
name := fmt.Sprintf("As%s", am.ChildType)
|
||||
gen.Comment(fmt.Sprintf("%s returns nil.", name))
|
||||
var ifaceName string
|
||||
if pkgpath == repository.RootPath {
|
||||
ifaceName = iface.Name
|
||||
} else {
|
||||
ifaceName = strings.Title(repository.TrimRoot(pkgpath)) + iface.Name
|
||||
}
|
||||
|
||||
stmt := jen.Func()
|
||||
stmt.Parens(jen.Id(iface.Name))
|
||||
stmt.Id(fmt.Sprintf("As%s", am.ChildType))
|
||||
stmt.Params()
|
||||
stmt.Add(genutils.GenerateExternType(am))
|
||||
stmt.Values(jen.Return(jen.Nil()))
|
||||
gen.Commentf("%[1]s provides no-op asserters for cchat.%[1]s.", ifaceName)
|
||||
gen.Type().Id(ifaceName).Struct()
|
||||
gen.Line()
|
||||
|
||||
gen.Add(stmt)
|
||||
for _, method := range iface.Methods {
|
||||
am, ok := method.(repository.AsserterMethod)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
name := fmt.Sprintf("As%s", am.ChildType)
|
||||
gen.Comment(fmt.Sprintf("%s returns nil.", name))
|
||||
|
||||
stmt := jen.Func()
|
||||
stmt.Parens(jen.Id(ifaceName))
|
||||
stmt.Id(fmt.Sprintf("As%s", am.ChildType))
|
||||
stmt.Params()
|
||||
stmt.Add(genutils.GenerateExternType(pkgpath, am))
|
||||
stmt.Values(jen.Return(jen.Nil()))
|
||||
|
||||
gen.Add(stmt)
|
||||
}
|
||||
|
||||
gen.Line()
|
||||
}
|
||||
|
||||
gen.Line()
|
||||
}
|
||||
|
||||
f, err := os.Create("empty.go")
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"unicode"
|
||||
|
||||
"github.com/dave/jennifer/jen"
|
||||
"github.com/diamondburned/cchat/repository"
|
||||
)
|
||||
|
||||
type Qualer interface {
|
||||
|
@ -21,11 +20,11 @@ func GenerateType(t Qualer) jen.Code {
|
|||
}
|
||||
|
||||
// GenerateExternType generates a jen.Qual from the given Qualer, except if
|
||||
// the path is empty, RootPath is used instead.
|
||||
func GenerateExternType(t Qualer) jen.Code {
|
||||
// the path is empty, root is used instead.
|
||||
func GenerateExternType(root string, t Qualer) jen.Code {
|
||||
path, name := t.Qual()
|
||||
if path == "" {
|
||||
return jen.Qual(repository.RootPath, name)
|
||||
return jen.Qual(root, name)
|
||||
}
|
||||
return jen.Qual(path, name)
|
||||
}
|
||||
|
|
|
@ -17,6 +17,11 @@ func MakeQual(relPath, name string) string {
|
|||
return fmt.Sprintf("(%s).%s", MakePath(relPath), name)
|
||||
}
|
||||
|
||||
// TrimRoot trims the root path and returns the path relative to root.
|
||||
func TrimRoot(fullPath string) string {
|
||||
return strings.TrimPrefix(strings.TrimPrefix(fullPath, RootPath), "/")
|
||||
}
|
||||
|
||||
// Packages maps Go module paths to packages.
|
||||
type Packages map[string]Package
|
||||
|
||||
|
|
|
@ -1,10 +1,40 @@
|
|||
// DO NOT EDIT: THIS FILE IS GENERATED!
|
||||
|
||||
// Package empty provides no-op asserter method implementations of interfaces in
|
||||
// cchat.
|
||||
// cchat's root and text packages.
|
||||
package empty
|
||||
|
||||
import cchat "github.com/diamondburned/cchat"
|
||||
import (
|
||||
"github.com/diamondburned/cchat"
|
||||
"github.com/diamondburned/cchat/text"
|
||||
)
|
||||
|
||||
// TextSegment provides no-op asserters for cchat.TextSegment.
|
||||
type TextSegment struct{}
|
||||
|
||||
// AsColorer returns nil.
|
||||
func (TextSegment) AsColorer() text.Colorer { return nil }
|
||||
|
||||
// AsLinker returns nil.
|
||||
func (TextSegment) AsLinker() text.Linker { return nil }
|
||||
|
||||
// AsImager returns nil.
|
||||
func (TextSegment) AsImager() text.Imager { return nil }
|
||||
|
||||
// AsAvatarer returns nil.
|
||||
func (TextSegment) AsAvatarer() text.Avatarer { return nil }
|
||||
|
||||
// AsMentioner returns nil.
|
||||
func (TextSegment) AsMentioner() text.Mentioner { return nil }
|
||||
|
||||
// AsAttributor returns nil.
|
||||
func (TextSegment) AsAttributor() text.Attributor { return nil }
|
||||
|
||||
// AsCodeblocker returns nil.
|
||||
func (TextSegment) AsCodeblocker() text.Codeblocker { return nil }
|
||||
|
||||
// AsQuoteblocker returns nil.
|
||||
func (TextSegment) AsQuoteblocker() text.Quoteblocker { return nil }
|
||||
|
||||
// Namer provides no-op asserters for cchat.Namer.
|
||||
type Namer struct{}
|
||||
|
|
Loading…
Reference in New Issue