Skip to content

Commit 2880aae

Browse files
committed
gopls/internal/protocol: Avoid omitempty for integer fields
The existing code adds the json tag 'omitempty' to optional protocol fields, but for some integer-valued fields 0 and empty have different semantics. This CL changes the behavior so that optional integer-valued fields are not omitted if they are zero. FIxes golang.go/go#71489 Change-Id: I1f2cd6c6b0d6b7495adb1d8bc0b404ee9ea895f5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/649455 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent 8807101 commit 2880aae

File tree

3 files changed

+41
-29
lines changed

3 files changed

+41
-29
lines changed

gopls/internal/protocol/generate/generate.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,39 +54,44 @@ func generateDoc(out *bytes.Buffer, doc string) {
5454

5555
// decide if a property is optional, and if it needs a *
5656
// return ",omitempty" if it is optional, and "*" if it needs a pointer
57-
func propStar(name string, t NameType, gotype string) (string, string) {
58-
var opt, star string
57+
func propStar(name string, t NameType, gotype string) (omitempty, indirect bool) {
5958
if t.Optional {
60-
star = "*"
61-
opt = ",omitempty"
59+
switch gotype {
60+
case "uint32", "int32":
61+
// in FoldingRange.endLine, 0 and empty have different semantics
62+
// There seem to be no other cases.
63+
default:
64+
indirect = true
65+
omitempty = true
66+
}
6267
}
6368
if strings.HasPrefix(gotype, "[]") || strings.HasPrefix(gotype, "map[") {
64-
star = "" // passed by reference, so no need for *
69+
indirect = false // passed by reference, so no need for *
6570
} else {
6671
switch gotype {
67-
case "bool", "uint32", "int32", "string", "interface{}", "any":
68-
star = "" // gopls compatibility if t.Optional
72+
case "bool", "string", "interface{}", "any":
73+
indirect = false // gopls compatibility if t.Optional
6974
}
7075
}
71-
ostar, oopt := star, opt
76+
oind, oomit := indirect, omitempty
7277
if newStar, ok := goplsStar[prop{name, t.Name}]; ok {
7378
switch newStar {
7479
case nothing:
75-
star, opt = "", ""
80+
indirect, omitempty = false, false
7681
case wantStar:
77-
star, opt = "*", ""
82+
indirect, omitempty = false, false
7883
case wantOpt:
79-
star, opt = "", ",omitempty"
84+
indirect, omitempty = false, true
8085
case wantOptStar:
81-
star, opt = "*", ",omitempty"
86+
indirect, omitempty = true, true
8287
}
83-
if star == ostar && opt == oopt { // no change
84-
log.Printf("goplsStar[ {%q, %q} ](%d) useless %s/%s %s/%s", name, t.Name, t.Line, ostar, star, oopt, opt)
88+
if indirect == oind && omitempty == oomit { // no change
89+
log.Printf("goplsStar[ {%q, %q} ](%d) useless %v/%v %v/%v", name, t.Name, t.Line, oind, indirect, oomit, omitempty)
8590
}
8691
usedGoplsStar[prop{name, t.Name}] = true
8792
}
8893

89-
return opt, star
94+
return
9095
}
9196

9297
func goName(s string) string {

gopls/internal/protocol/generate/output.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,17 @@ func genProps(out *bytes.Buffer, props []NameType, name string) {
273273
tp = newNm
274274
}
275275
// it's a pointer if it is optional, or for gopls compatibility
276-
opt, star := propStar(name, p, tp)
277-
json := fmt.Sprintf(" `json:\"%s%s\"`", p.Name, opt)
276+
omit, star := propStar(name, p, tp)
277+
json := fmt.Sprintf(" `json:\"%s\"`", p.Name)
278+
if omit {
279+
json = fmt.Sprintf(" `json:\"%s,omitempty\"`", p.Name)
280+
}
278281
generateDoc(out, p.Documentation)
279-
fmt.Fprintf(out, "\t%s %s%s %s\n", goName(p.Name), star, tp, json)
282+
if star {
283+
fmt.Fprintf(out, "\t%s *%s %s\n", goName(p.Name), tp, json)
284+
} else {
285+
fmt.Fprintf(out, "\t%s %s %s\n", goName(p.Name), tp, json)
286+
}
280287
}
281288
}
282289

gopls/internal/protocol/tsprotocol.go

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)