go-poet icon indicating copy to clipboard operation
go-poet copied to clipboard

Type-references from generated code in another package

Open dpolansky opened this issue 8 years ago • 1 comments

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!

dpolansky avatar Feb 20 '17 00:02 dpolansky

It seems like a good idea. Especially as a stop gap to a bigger refactor @bmoylan outlined we would need to consistently resolve all of the issues.

kdeenanauth avatar Feb 21 '17 19:02 kdeenanauth