Giter Site home page Giter Site logo

Create groups in my code? about jennifer HOT 19 OPEN

endigma avatar endigma commented on June 10, 2024
Create groups in my code?

from jennifer.

Comments (19)

endigma avatar endigma commented on June 10, 2024

If Group is meant for internal use only, perhaps another type for creating snippets functionally?

from jennifer.

endigma avatar endigma commented on June 10, 2024

Is this what Custom/CustomFunc is for?

from jennifer.

dave avatar dave commented on June 10, 2024

Sorry I don't understand

from jennifer.

endigma avatar endigma commented on June 10, 2024

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.

endigma avatar endigma commented on June 10, 2024

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.

dave avatar dave commented on June 10, 2024

Can't you just do this?

func generateType(typespec *spec.Type) jen.Code {
    c := jen.Commentf("foo").Line()
    return c
}

from jennifer.

endigma avatar endigma commented on June 10, 2024

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.

dave avatar dave commented on June 10, 2024

How about returning a []jen.Code?

from jennifer.

dave avatar dave commented on June 10, 2024

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.

endigma avatar endigma commented on June 10, 2024

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.

endigma avatar endigma commented on June 10, 2024

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.

image
results in
image
so I have to write like

image

to interleave the newlines

from jennifer.

dave avatar dave commented on June 10, 2024

How about:

	code := []jen.Code{...}
	for _, c := range code {
		g.Add(c)
	}

from jennifer.

endigma avatar endigma commented on June 10, 2024

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.

dave avatar dave commented on June 10, 2024

Where are you getting g from in that code?

from jennifer.

endigma avatar endigma commented on June 10, 2024

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.

dave avatar dave commented on June 10, 2024

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.

endigma avatar endigma commented on June 10, 2024

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.

endigma avatar endigma commented on June 10, 2024

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.

dave avatar dave commented on June 10, 2024

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)

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.