|
| 1 | +//////////////////////////////////////////////////////////////////////////// |
| 2 | +// Program: jsonfiddle |
| 3 | +// Purpose: JSON Fiddling |
| 4 | +// Authors: Tong Sun (c) 2017-2023, All rights reserved |
| 5 | +//////////////////////////////////////////////////////////////////////////// |
| 6 | + |
| 7 | +package main |
| 8 | + |
| 9 | +import ( |
| 10 | + "fmt" |
| 11 | + "os" |
| 12 | + |
| 13 | + "github.com/go-easygen/go-flags/clis" |
| 14 | +) |
| 15 | + |
| 16 | +// *** Sub-command: j2s *** |
| 17 | + |
| 18 | +//////////////////////////////////////////////////////////////////////////// |
| 19 | +// Constant and data type/structure definitions |
| 20 | + |
| 21 | +// The J2sCommand type defines all the configurable options from cli. |
| 22 | +type J2sCommand struct { |
| 23 | + FmtType string `short:"f" long:"fmt" description:"the structural format of the input data (json/yaml)" default:"json"` |
| 24 | + Filei string `short:"i" long:"input" description:"the source of the input JSON (mandatory)" required:"true"` |
| 25 | + Fileo string `short:"o" long:"output" description:"the output, default to stdout" default:"-"` |
| 26 | + Name string `long:"name" description:"the name of the root struct (default: as input file name)"` |
| 27 | + Pkg string `long:"pkg" description:"the name of the package for the generated code" default:"main"` |
| 28 | + SubStruct bool `long:"subStruct" description:"create types for sub-structs"` |
| 29 | +} |
| 30 | + |
| 31 | +var j2sCommand J2sCommand |
| 32 | + |
| 33 | +func init() { |
| 34 | + parser.AddCommand("j2s", |
| 35 | + "JSON to struct", |
| 36 | + "JSON convert to Go struct", |
| 37 | + &j2sCommand) |
| 38 | +} |
| 39 | + |
| 40 | +func (x *J2sCommand) Execute(args []string) error { |
| 41 | + fmt.Fprintf(os.Stderr, "JSON to struct\n") |
| 42 | + // fmt.Fprintf(os.Stderr, "Copyright (C) 2017-2023, Tong Sun\n\n") |
| 43 | + clis.Setup("jsonfiddle::j2s", opts.Verbose) |
| 44 | + clis.Verbose(1, "Doing J2s, with %+v, %+v", opts, args) |
| 45 | +// fmt.Println(x.FmtType, x.Filei, x.Fileo, x.Name, x.Pkg, x.SubStruct) |
| 46 | + return x.Exec(args) |
| 47 | +} |
| 48 | + |
| 49 | +// Exec implements the business logic of command `j2s` |
| 50 | +// func (x *J2sCommand) Exec(args []string) error { |
| 51 | +// // err := ... |
| 52 | +// // clis.WarnOn("J2s, Exec", err) |
| 53 | +// // or, |
| 54 | +// // clis.AbortOn("J2s, Exec", err) |
| 55 | +// return nil |
| 56 | +// } |
0 commit comments