Git Product home page Git Product logo

go-poet's People

Contributors

bmoylan avatar dpolansky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

go-poet's Issues

Support multiline comments

We currently do the equivalent of "// " + comment. We should prepend // at the beginning of every line in the string, not just the first one.

bad validation when an ImportPath has points

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!

Importing from the same package as a generated file

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.

Type-references from generated code in another package

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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.