Comments (19)
If Group
is meant for internal use only, perhaps another type for creating snippets functionally?
from jennifer.
Is this what Custom/CustomFunc
is for?
from jennifer.
Sorry I don't understand
from jennifer.
I'd like to make a function like, say:
func generateType(typespec *spec.Type) jen.Code {
...
}
that produces some g.Add(...)
-able code to allow me to compose different functions to generate a more complex file.
currently, I'm doing it like this:
func generateType(typespec *spec.Type) jen.Code {
g := new(jen.Group)
g.Commentf("foo").Line()
return g
}
but new(jen.Group)
doesn't have newlines, so I have to add them manually.
I found jen.CustomFunc
, and it seems to work for what I want, but I'm not sure if it's the right way to do it.
func generateType(typespec *spec.Type) jen.Code {
return CustomFunc(Options{
Multi: true,
}, func(g *Group) {
g.Comment("foo")
})
}
Does that make sense?
from jennifer.
Perhaps we could get a method like CustomGroup(options) *Group
that uses the Options
from Custom
/CustomFunc
?
func CustomGroup(options Options) *Group {
return &Group{
close: options.Close,
multi: options.Multi,
name: "custom",
open: options.Open,
separator: options.Separator,
}
}
CustomFunc works but wrapping my entire function wastes an indent.
from jennifer.
Can't you just do this?
func generateType(typespec *spec.Type) jen.Code {
c := jen.Commentf("foo").Line()
return c
}
from jennifer.
Yes, but my actual generators are a lot larger than that and consist of tens of different structures. Writing them all as one statement would be very unreadable. I'd essentially like a function that creates groups with options, that way I can use them as builders.
from jennifer.
How about returning a []jen.Code
?
from jennifer.
Basically a Group
contains extra information about the separators between items, the braces a the start and end... that sort of thing... so I don't think Group
is the right this for this usage...
from jennifer.
I used to use []jen.Code
, but then I had to write a bunch of logic for handling newlines etc. Perhaps a new construct for this usage would be best then?
Similar to *Group
in that it has helpers for append(g, ...)
, but with only basic options like separator
to join all the items with jen.Line()
.
from jennifer.
Or just implying it's multiline, sort of like a file.
returning []jen.Code
doesn't work so well because of the way it would get added back onto the parent.
results in
so I have to write like
to interleave the newlines
from jennifer.
How about:
code := []jen.Code{...}
for _, c := range code {
g.Add(c)
}
from jennifer.
That doesn't add newlines, and there's still no append helpers for []jen.Code
though, and all the extra code does start to add up. I don't see why there shouldn't be a simpler way to achieve this pattern, using []jen.Code
with a workaround here and a workaround there doesn't make so much sense if the library could include all of the boilerplate.
I'm sure I'm not the only one who will ever want to build code like this, so a type like Builder
or something that wraps all of this would be good.
// update:, no it doesn't add newlines
code := []Code{Comment("1"), Comment("2"), Comment("3")}
for _, c := range code {
g.Add(c)
}
==
// 1// 2// 3
from jennifer.
Where are you getting g
from in that code?
from jennifer.
it's a &Group
, I'd have to repeatedly do the same pattern which is the "all the extra code does start to add up" I mentioned.
from jennifer.
This example test passes:
func ExampleFoo() {
block := BlockFunc(func(group *Group) {
code := []Code{Comment("1"), Comment("2"), Comment("3")}
for _, c := range code {
group.Add(c)
}
})
fmt.Printf("%#v", block)
// Output:
// {
// // 1
// // 2
// // 3
// }
}
from jennifer.
Block
has {}
though, literally block without {}
would be perfect. Block
works because block's group
is configured to put newlines inbetween entries. I would like to be able to create my own Group
-like things that are also have entries separated by newlines. Exactly like Group
in DX, except created in isolation.
from jennifer.
CustomFunc
does it, but it puts the Group
it makes into a closure instead of returning it. Perhaps the group creation bit can be factored out and put in package scope?
from jennifer.
Yeah the solution is to use []Code, and only add to a Group when it's actually being added to code.
(BTW I'm not going to add anything to Jennifer unless there's a bug to fix... the API is far too cluttered already to add more convenience methods)
from jennifer.
Related Issues (20)
- Better ways to manage import names and aliases HOT 5
- Get *jen.Statement from reflect.Type HOT 1
- Plain struct field tag HOT 1
- [go1.19, go1.20] Indented Multiline Struct Comments have tab characters HOT 3
- example with multiple return types HOT 5
- Support `Array` for `Value` besides `Dict`? HOT 2
- Dict sorts keys by string, breaking existing/natural ordering HOT 2
- Please read: Issues for improving the readability or formatting of outputted code.
- Creating a function receiver HOT 2
- Feature: Should add a abstruct layer to make more Declarative? HOT 3
- How to generate struct parameter in function qual HOT 2
- Callback with errors?
- Support multiline string literal (backtick) HOT 2
- Feature: ability to provide custom imports sorter function
- Feature Request: Do not render [] if .Types() is called with no arguments HOT 1
- Non-int untyped numeric literal HOT 2
- How to generate complex type field definitions in Struct HOT 3
- Custom types on interface HOT 3
- How to Gennerate var()? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jennifer.