dpolansky / go-poet Goto Github PK
View Code? Open in Web Editor NEWA Go package for generating Go code
License: MIT License
A Go package for generating Go code
License: MIT License
We currently do the equivalent of "// " + comment
. We should prepend //
at the beginning of every line in the string, not just the first one.
hello, i have issues when i want to use poet with packages that has points
for example when i import this package in my poet file:
import "github.com/someauthor/somepackage"
and it's use with poet:
.Statement("$T(params)",poet.TypeReferenceFromInstance(somepackage.SomeFunction))
generates:
...
import (
"github"
)
...{
github.com/someauthor/somepackage(params)
...}
which it is obviously invalid.
I think that the problem is in line 207 in types.go (and maybe in others places):
fullyQualifiedPieces := strings.Split(funcPtr.Name(), ".")
witch split the path of a newTypeReferenceFromFunction incorrectly.
thanks!
In the following example, a file is generated inside of package main
while referencing a function, foo
, which is also in package main
.
package main
import (
"fmt"
"github.com/dpolansky/go-poet/poet"
)
func foo() {
}
func main() {
bar := poet.NewFuncSpec("bar").Statement("$T()", poet.TypeReferenceFromInstance(foo))
file := poet.NewFileSpec("main").CodeBlock(bar)
fmt.Println(file)
}
which currently produces
package main
import (
"main"
)
func bar() {
main.foo()
}
instead of the expected output
package main
func bar() {
foo()
}
We should be able to know that foo
is in the same package as the generated file and not import itself. In addition, since we only specify the base name of a package rather than the full path, we do not know the full package path of the generated file, making it ambiguous which packages do not need to be imported.
Consider the following code:
typeRef := TypeReferenceFromInstance(bytes.Buffer{})
actual := typeRef.GetName()
actual
will be assigned bytes.Buffer
as the name, which contains the package qualifier as expected.
However, the TypeReference for generated specs (such as a StructSpec or InterfaceSpec) will not have a package qualifier because we don't know the package it belongs to until it is added to a FileSpec:
strSpec := NewStructSpec("Foo")
// strSpec.GetName() would return "Foo"
file := NewFileSpec("mypackage").CodeBlock(strSpec)
Consider the use case of getting a TypeReference to strSpec from another package. In this example, Foo
belongs to package mypackage
, and it is used in a function myFunc
in package main
:
strSpec := NewStructSpec("Foo")
mypackageFile := NewFileSpec("mypackage").CodeBlock(strSpec)
// we pass in strSpec as a TypeReference to use it as a result parameter
func := NewFuncSpec("myFunc").ResultParameter("", strSpec)
mainFile := NewFileSpec("main").CodeBlock(func)
Currently, the TypeReference for strSpec would only include its unqualified name, 'Foo'. I propose adding a method to a FileSpec that wraps a TypeReference with the File's package name:
strSpec := NewStructSpec("Foo")
mypackageFile := NewFileSpec("mypackage").CodeBlock(strSpec)
wrappedStrSpec := mypackageFile.WrapTypeReference(strSpec)
func := NewFuncSpec("myFunc").ResultParameter("", wrappedStrSpec)
mainFile := NewFileSpec("main").CodeBlock(func)
When wrappedStrSpec.GetName()
is called, it returns mypackage.Foo
.
Any suggestions / alternative approaches for using generated TypeReferences in other packages?
@kdeenanauth Would love to hear your thoughts on this!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.