| 1 | # codecgen tool |
| 2 | |
| 3 | Generate is given a list of *.go files to parse, and an output file (fout), |
| 4 | codecgen will create an output file __file.go__ which |
| 5 | contains `codec.Selfer` implementations for the named types found |
| 6 | in the files parsed. |
| 7 | |
| 8 | Using codecgen is very straightforward. |
| 9 | |
| 10 | **Download and install the tool** |
| 11 | |
| 12 | `go get -u github.com/ugorji/go/codec/codecgen` |
| 13 | |
| 14 | **Run the tool on your files** |
| 15 | |
| 16 | The command line format is: |
| 17 | |
| 18 | `codecgen [options] (-o outfile) (infile ...)` |
| 19 | |
| 20 | ```sh |
| 21 | % codecgen -? |
| 22 | Usage of codecgen: |
| 23 | -c="github.com/ugorji/go/codec": codec path |
| 24 | -o="": out file |
| 25 | -d="": random identifier for use in generated code (help reduce changes when files are regenerated) |
| 26 | -nx=false: do not support extensions (use when you know no extensions are used) |
| 27 | -r=".*": regex for type name to match |
| 28 | -nr="": regex for type name to exclude |
| 29 | -rt="": tags for go run |
| 30 | -st="codec,json": struct tag keys to introspect |
| 31 | -t="": build tag to put in file |
| 32 | -u=false: Use unsafe, e.g. to avoid unnecessary allocation on []byte->string |
| 33 | -x=false: keep temp file |
| 34 | |
| 35 | % codecgen -o values_codecgen.go values.go values2.go moretypedefs.go |
| 36 | ``` |
| 37 | |
| 38 | **Limitations** |
| 39 | |
| 40 | codecgen caveats: |
| 41 | |
| 42 | - Canonical option. |
| 43 | If Canonical=true, codecgen'ed code will delegate encoding maps to reflection-based code. |
| 44 | This is due to the runtime work needed to marshal a map in canonical mode. |
| 45 | - CheckCircularRef option. |
| 46 | When encoding a struct, a circular reference can lead to a stack overflow. |
| 47 | If CheckCircularRef=true, codecgen'ed code will delegate encoding structs to reflection-based code. |
| 48 | - MissingFielder implementation. |
| 49 | If a type implements MissingFielder, a Selfer is not generated (with a warning message). |
| 50 | Statically reproducing the runtime work needed to extract the missing fields and marshal them along with the struct fields, |
| 51 | while handling the Canonical=true special case, was onerous to implement. |
| 52 | |
| 53 | **More Information** |
| 54 | |
| 55 | Please see the [blog article](http://ugorji.net/blog/go-codecgen) |
| 56 | for more information on how to use the tool. |