| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "flag" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | func embStructsSubCommand(args []string) { |
| 13 | cmdName := "emb-structs" |
| 14 | cmd := flag.NewFlagSet(cmdName, flag.ExitOnError) |
| 15 | cmd.Usage = func() { |
| 16 | _, _ = fmt.Fprintf(cmd.Output(), `Usage of %s: |
| 17 | |
| 18 | Embed Go structs as compact format in the binary. |
| 19 | JSON file format: |
| 20 | { |
| 21 | "prefix": "html5entities", // prefix for the generated const/var |
| 22 | "types": { // struct field types |
| 23 | "Name": "string", // struct fields |
| 24 | "CodePoints": "[]int", |
| 25 | "Characters": "[]byte" |
| 26 | }, |
| 27 | |
| 28 | // array of struct data |
| 29 | // all data must be a string |
| 30 | "data": [ |
| 31 | {"Name": "AElig", "CodePoints": ["198"], "Characters": ["0xc3", "0x86"]}, |
| 32 | {"Name": "AMP", "CodePoints": ["38"], "Characters": ["0x26"]}, |
| 33 | ] |
| 34 | } |
| 35 | |
| 36 | `, cmdName) |
| 37 | cmd.PrintDefaults() |
| 38 | } |
| 39 | |
| 40 | inputJSONPath := cmd.String("i", "", "source JSON file path(required)") |
| 41 | outputPath := cmd.String("o", "", "output file path(required)") |
| 42 | |
| 43 | if err := cmd.Parse(args); err != nil || |
| 44 | len(*inputJSONPath) == 0 || |
| 45 | len(*outputPath) == 0 { |
| 46 | usage(cmd.Usage, err) |
| 47 | } |
| 48 | var source map[string]any |
| 49 | inputJSONSource, err := os.ReadFile(*inputJSONPath) |
| 50 | if err != nil { |
| 51 | panic(err) |
| 52 | } |
| 53 | if err := json.Unmarshal(inputJSONSource, &source); err != nil { |
| 54 | panic(err) |
| 55 | } |
| 56 | |
| 57 | f, err := os.Create(*outputPath) |
| 58 | if err != nil { |
| 59 | fmt.Printf("Failed to open %s: %v\n", *outputPath, err) |
| 60 | os.Exit(1) |
| 61 | } |
| 62 | defer f.Close() |
| 63 | |
| 64 | abs, _ := filepath.Abs(*outputPath) |
| 65 | pkg := filepath.Base(filepath.Dir(abs)) |
| 66 | |
| 67 | write := func(ft string, v ...any) { |
| 68 | if len(v) == 0 { |
| 69 | _, _ = f.WriteString(ft) |
| 70 | return |
| 71 | } |
| 72 | _, _ = f.WriteString(fmt.Sprintf(ft, v...)) |
| 73 | } |
| 74 | writeln := func(ft string, v ...any) { |
| 75 | write(ft+"\n", v...) |
| 76 | } |
| 77 | |
| 78 | writeln("// Code generated by _tools; DO NOT EDIT.") |
| 79 | writeln("package " + pkg) |
| 80 | |
| 81 | prefix := source["prefix"].(string) |
| 82 | types := source["types"].(map[string]any) |
| 83 | data := source["data"].([]any) |
| 84 | |
| 85 | writeln("const _%sLength = %d", prefix, len(data)) |
| 86 | |
| 87 | for prop, _typ := range types { |
| 88 | typ := _typ.(string) |
| 89 | if typ == "string" { |
| 90 | write("const _%s%s string = \"", prefix, prop) |
| 91 | for _, _d := range data { |
| 92 | d := _d.(map[string]any) |
| 93 | v := d[prop].(string) |
| 94 | write(v) |
| 95 | } |
| 96 | writeln(`"`) |
| 97 | write("const _%s%sIndex = \"", prefix, prop) |
| 98 | for _, _d := range data { |
| 99 | d := _d.(map[string]any) |
| 100 | v := d[prop].(string) |
| 101 | write("\\x%02x", len(v)) |
| 102 | } |
| 103 | writeln(`"`) |
| 104 | continue |
| 105 | } |
| 106 | |
| 107 | if strings.HasPrefix(typ, "[]") { |
| 108 | elmTyp := typ[2:] |
| 109 | write("var _%s%s = [...]%s{", prefix, prop, elmTyp) |
| 110 | for i, _d := range data { |
| 111 | d := _d.(map[string]any) |
| 112 | arr := d[prop].([]any) |
| 113 | for j, a := range arr { |
| 114 | v := a.(string) |
| 115 | write(v) |
| 116 | if i != len(data)-1 || j != len(arr)-1 { |
| 117 | write(", ") |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | writeln("}") |
| 122 | write("var _%s%sIndex = \"", prefix, prop) |
| 123 | for _, _d := range data { |
| 124 | d := _d.(map[string]any) |
| 125 | arr := d[prop].([]any) |
| 126 | write("\\x%02x", len(arr)) |
| 127 | } |
| 128 | writeln(`"`) |
| 129 | continue |
| 130 | } |
| 131 | |
| 132 | write("var _%s%s = [...]%s{", prefix, prop, typ) |
| 133 | for i, _d := range data { |
| 134 | d := _d.(map[string]any) |
| 135 | v := d[prop].(string) |
| 136 | write(v) |
| 137 | if i != len(data)-1 { |
| 138 | write(", ") |
| 139 | } |
| 140 | } |
| 141 | writeln(`}`) |
| 142 | } |
| 143 | } |