| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"fmt" |
| 10 |
"log" |
| 11 |
"strings" |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
type newType struct { |
| 17 |
name string |
| 18 |
properties Properties |
| 19 |
items []*Type |
| 20 |
line int |
| 21 |
kind string |
| 22 |
typ *Type |
| 23 |
} |
| 24 |
|
| 25 |
func generateDoc(out *bytes.Buffer, doc string) { |
| 26 |
if doc == "" { |
| 27 |
return |
| 28 |
} |
| 29 |
|
| 30 |
if !strings.Contains(doc, "\n") { |
| 31 |
fmt.Fprintf(out, "// %s\n", doc) |
| 32 |
return |
| 33 |
} |
| 34 |
var list bool |
| 35 |
for line := range strings.SplitSeq(doc, "\n") { |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
if len(line) > 0 && line[0] == '-' { |
| 41 |
if !list { |
| 42 |
list = true |
| 43 |
fmt.Fprintf(out, "//\n") |
| 44 |
} |
| 45 |
fmt.Fprintf(out, "// %s\n", line) |
| 46 |
} else { |
| 47 |
if len(line) == 0 { |
| 48 |
list = false |
| 49 |
} |
| 50 |
fmt.Fprintf(out, "// %s\n", line) |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
func propStar(name string, t NameType, gotype string) (omitempty, indirect bool) { |
| 58 |
if t.Optional { |
| 59 |
switch gotype { |
| 60 |
case "uint32", "int32": |
| 61 |
|
| 62 |
|
| 63 |
default: |
| 64 |
indirect = true |
| 65 |
omitempty = true |
| 66 |
} |
| 67 |
} |
| 68 |
if strings.HasPrefix(gotype, "[]") || strings.HasPrefix(gotype, "map[") { |
| 69 |
indirect = false |
| 70 |
} else { |
| 71 |
switch gotype { |
| 72 |
case "bool", "string", "interface{}", "any": |
| 73 |
indirect = false |
| 74 |
} |
| 75 |
} |
| 76 |
oind, oomit := indirect, omitempty |
| 77 |
if newStar, ok := goplsStar[prop{name, t.Name}]; ok { |
| 78 |
switch newStar { |
| 79 |
case nothing: |
| 80 |
indirect, omitempty = false, false |
| 81 |
case wantOpt: |
| 82 |
indirect, omitempty = false, true |
| 83 |
case wantOptStar: |
| 84 |
indirect, omitempty = true, true |
| 85 |
} |
| 86 |
if indirect == oind && omitempty == oomit { |
| 87 |
log.Printf("goplsStar[ {%q, %q} ](%d) useless %v/%v %v/%v", name, t.Name, t.Line, oind, indirect, oomit, omitempty) |
| 88 |
} |
| 89 |
usedGoplsStar[prop{name, t.Name}] = true |
| 90 |
} |
| 91 |
|
| 92 |
return |
| 93 |
} |
| 94 |
|
| 95 |
func goName(s string) string { |
| 96 |
|
| 97 |
if strings.HasSuffix(s, "Id") { |
| 98 |
s = s[:len(s)-len("Id")] + "ID" |
| 99 |
} else if strings.HasSuffix(s, "Uri") { |
| 100 |
s = s[:len(s)-3] + "URI" |
| 101 |
} else if s == "uri" { |
| 102 |
s = "URI" |
| 103 |
} else if s == "id" { |
| 104 |
s = "ID" |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
if news := goplsType[s]; news != "" { |
| 109 |
usedGoplsType[s] = true |
| 110 |
s = news |
| 111 |
} |
| 112 |
|
| 113 |
if strings.HasPrefix(s, "_") { |
| 114 |
s = strings.Replace(s, "_", "X", 1) |
| 115 |
} |
| 116 |
if s != "string" { |
| 117 |
|
| 118 |
s = strings.Title(s) |
| 119 |
} |
| 120 |
return s |
| 121 |
} |