Coder Social home page Coder Social logo

Comments (6)

natdm avatar natdm commented on April 28, 2024 1

That sounds great. A mention is fine with me! If you are using it and have an issue, please create an issue in Typewriter.

from ponzu.

natdm avatar natdm commented on April 28, 2024

To be clear, this is parsing types that was already generated by ponzu? Not being generated at the same time?

from ponzu.

nilslice avatar nilslice commented on April 28, 2024

Right - the thought is that once you've set your content types, you then generate the client models.

If possible to do cleanly, I think the generated client models should be mostly left alone from a file editing standpoint. This way you can rerun the model generator over and over after Ponzu content types are updated, just like typewriter.

from ponzu.

natdm avatar natdm commented on April 28, 2024

I think the first thing I'd like to try is to look at Typewriter and see what needs to be done for that engine to be used by Ponzu, and what changes need to be made there. I don't predict too many.

Currently this is how Typewriter works.

It parses all types and creates a new model for each one:

// PackageType is a package-level type. Any package type will
// be templated with a full type creation statement and possibly a comment
type PackageType struct {
	Name    string    // Name of the type
	Comment string    // Any comment that type may have
	Type    Templater // The templater that draws the code, switching it up per language
	Tag     string    // any json/tw tags
}
// Templater interface is able to write a template to a writer, based on a Language
type Templater interface {
	Template(w io.Writer, lang Language) error
}

And then they all get parsed in alphabetical order, through a template (or more, depending on the type).

The templates look like this:

var tsTemplates = map[templateKey]string{
	header: `// Automatically generated by typewriter. Do not edit.
// http://www.github.com/natdm/typewriter

`,
	arrayOpen:  `Array<`,
	arrayClose: `>`,
	basic:      `{{if .Pointer}}?{{end}}{{updateTSType .Type}}`,
	comment:    `{{tsComment .Comment}}`,
	declaration: `
{{tsTypeComment .Comment}}type {{.Name}} = `,
	fieldClose: `, `,
	fieldName: `
	{{.Name}}: `,
	mapClose:    ` }`,
	mapKey:      `{ [key: `,
	mapValue:    `]: `,
	structClose: `}`,
	structOpen:  `{`,
}

What languages do you think you want these Go types parsed to, in order of priority? And the more 100% complete examples you can give, the better. Some issues I bump in to are things like embedded types (where I now just get the fields from the embedded type and insert them in the Struct, since the goal was JS/TS/Flow/Elm and they don't really do embedded types) and types in other packages. eg: type x map[string]item.Item. I then have to look at where the 'item' package is, get that type put it in the models to be generated, then use that instead (work in progress). So...

type x map[string]item.Item

turns in to

// replaced dot with underscore to help avoid other collisions with a possible 'Item' type already created
export type item_Item = {
    ...
}
export type x = { [key: string]: item_Item }

from ponzu.

nilslice avatar nilslice commented on April 28, 2024

@natdm -

Apologies for the delay here.. I have a fast-approaching deadline for a project at the moment. In fact, I'm building a semi-complex javascript UI over Ponzu data, so I'm getting some helpful experience on what would be useful here.

It turns out, I don't feel that doing as much code generation on the front end will be particularly useful after all. The only part I think we could reliably generate that would be useful is that which is done by typewriter.. keep the client models in sync as the types change.

The other HTTP stuff is too preference-based to really be beneficial, i.e. people use jQuery Ajax, or superagent, request, etc... and several other libraries across the mobile landscape.

In my own project, I only have 4 data types, and relative to the rest of the project, there is very little code written to manage the HTTP interaction with Ponzu's content API. I think it might be better to let this be an external decision for the time being, instead of something built directly into Ponzu. Maybe we could mention the typewriter project in the wiki or something so others are more inclined to write and share templates for other languages.

What do you think?


As for your comments -- I think the engine is already there for JS (and maybe even HTML) code gen. The embedded type issue isn't typically a problem for Ponzu content types, since the only type we really embed is an item.Item & the find-and-insert approach works fine.

from ponzu.

nilslice avatar nilslice commented on April 28, 2024

Ok, I will close this issue for now, but add some label so it doesn't get lost. I want to revisit this later and see how we can continue to make the dev experience better.

from ponzu.

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.