Easygen is now coding itself

Easygen is now coding itself

This an appendix to the mini series on how to provide options for Go applications, reporting the updates to easygen, a universal code/text generator that is easy to use — easygen is now coding itself!

generic template testing tool

But first, I’ll reiterate what’s put into the readme file, easygen can now be used as an generic Google template testing tool with the -ts commandline option. For example,

echo 'Name: some-init-method' > /tmp/var.yaml

$ easygen -ts '{{.Name}} {{6 | minus1}} {{minus1 6}} {{ck2lc .Name}} {{ck2uc .Name}}' /tmp/var
some-init-method 5 5 someInitMethod SomeInitMethod

echo '{FirstName: John, LastName: Doe}' > /tmp/name.yaml

$ easygen -ts "{{.FirstName}} {{ck2ss .LastName}}'s full name is "'{{len (printf "%s%s" .FirstName .LastName)}} letters long.' /tmp/name
John DOE's full name is 7 letters long.

More examples are available under the “Tips” section of the readme file.

code gen

Now onto the exciting code auto-generation. The full source and example code has now been committed to git, https://github.com/suntong001/easygen/tree/master/test. Let’s take a look at the generated code first:

The code at https://github.com/suntong001/easygen/blob/master/easygenapi/config.go is a direct generation from the above output:

easygen test/commandlineFlag | gofmt > easygenapi/config.go

I.e., easygen is currently using the code generated by itself now!

This is the output of the Usage function:

$ easygen

Usage:
 easygen [flags] YamlFileName

  -debug=0: debugging level
  -html=false: treat the template file as html instead of text
  -tf="": .tmpl template file name (default: same as .yaml file)
  -ts="": template string (in text)

YamlFileName: The name for the .yaml data and .tmpl template file
        Only the name part, without extension. Can include the path as well.

Now let’s dive deep and take a look at the driving data:

$ head -33 test/commandlineFlag.yaml 
# program name, name for the executable
ProgramName: easygen

# package name
# - For standalone program that does not belong to any package, e.g., 
#   https://github.com/suntong001/easygen/blob/7791e4f0e5605543d27da1671a21376cdb9dcf2a/easygen/easygen.go
#   just ignore the first line, the `package` output, and copy the rest
# - If you don't mind using a separated file to handle commandline paramters,
#   then name the package as "main". see the spin-out "TF-minus1.go" file under
#   https://github.com/suntong001/easygen/tree/d1ab0b5fe80ddac57fe9ef51f6ccb3ab998cd5ee
# - If you are using it in a pacakge, look no further than
#   https://github.com/suntong001/easygen/blob/master/easygenapi/config.go
#   which was a direct dump: easygen test/commandlineFlag > easygenapi/config.go
#
PackageName: easygenapi

# Name of the structure to hold the values for/from commandline
StructName: Options
# The actual variable that hold the commandline paramter values
StructVar: Opts

Options:
  - Name: HTML
    Type: bool
    Flag: html
    Value: false
    Usage: treat the template file as html instead of text

  - Name: TemplateStr
    Type: string
    Flag: ts
    Value: '""'
    Usage: "template string (in text)"

You can see from the comment that there are three different use cases that you can make use of this commandline flag code auto-generation.

Just copy out the test/commandlineFlag.* files, and adapt to your own situation. If you think the default commandlineFlag template is good enough for you, then you can even just only define your own flags, without duplicating a template file, like this:

easygen -tf $GOPATH/src/github.com/suntong001/easygen/test/commandlineFlag /path/to/yourFlagDefFile

And you’ll be all set. There are still lots of features I’m planning to add to easygen. so stay tuned.

Thanks for reading thus far. Happy hacking.

Advertisements

2 thoughts on “Easygen is now coding itself

  1. Pingback: Easygen for mock-up | SF-Xpt's Blog

  2. Pingback: Showcasing the power of easygen with ffcvt | SF-Xpt's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s