Skip to content

Commit 74c640a

Browse files
xaionaro@dx.centerxaionaro@dx.center
authored andcommitted
fix: correct uint16 flag casting in cligen using flagReturnType
The cobra flag getter for uint16 returns int32 (no native uint16 flag). Added flagReturnType() to determine the actual Go type returned by each flag getter, and cast to the param type when they differ. Verified on device: thermal, trace, egl info all work correctly.
1 parent 6b9d31e commit 74c640a

4 files changed

Lines changed: 75 additions & 45 deletions

File tree

cmd/ndkcli/android_gen.go

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

cmd/ndkcli/font_gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/ndkcli/input_gen.go

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

tools/cmd/cligen/main.go

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,33 @@ func resolveType(goType string, aliases map[string]string) string {
324324
return goType
325325
}
326326

327+
// flagReturnType returns the Go type that the cobra flag getter returns.
328+
func flagReturnType(resolved string) string {
329+
switch resolved {
330+
case "string":
331+
return "string"
332+
case "int32":
333+
return "int32"
334+
case "int64":
335+
return "int64"
336+
case "int":
337+
return "int"
338+
case "uint16":
339+
return "int32" // promoted — cobra has no uint16 flag
340+
case "uint32":
341+
return "uint32"
342+
case "uint64":
343+
return "uint64"
344+
case "float32":
345+
return "float32"
346+
case "float64":
347+
return "float64"
348+
case "bool":
349+
return "bool"
350+
}
351+
return resolved
352+
}
353+
327354
func isPrimitive(t string) bool {
328355
switch t {
329356
case "string", "int32", "int64", "int", "uint16", "uint32", "uint64",
@@ -672,19 +699,22 @@ func genParamCode(
672699
cmdVar, regMethod, flagName, defVal, p.name))
673700
}
674701

702+
// The flag getter may return a different type than the param
703+
// (e.g., uint16 → GetInt32 returns int32). Determine if we
704+
// need a cast from the flag type to the param type.
705+
flagType := flagReturnType(resolved)
675706
fmt.Fprintf(&body, "\t\t%s, _ := cmd.Flags().%s(%q)\n", varName, getter, flagName)
676707

677-
// If the original type differs from resolved, cast.
678-
if p.goType != resolved && !strings.HasPrefix(p.goType, "*") {
679-
if isPrimitive(p.goType) {
680-
// Builtin type (uint16, int, etc.) — direct cast.
681-
callArgs = append(callArgs, p.goType+"("+varName+")")
682-
} else {
683-
// Package-defined type — qualified cast.
684-
callArgs = append(callArgs, pkgRef+"."+p.goType+"("+varName+")")
685-
}
686-
} else {
708+
switch {
709+
case p.goType == flagType:
710+
// Types match exactly — no cast.
687711
callArgs = append(callArgs, varName)
712+
case isPrimitive(p.goType):
713+
// Builtin type (uint16, int, etc.) — direct cast.
714+
callArgs = append(callArgs, p.goType+"("+varName+")")
715+
default:
716+
// Package-defined type — qualified cast.
717+
callArgs = append(callArgs, pkgRef+"."+p.goType+"("+varName+")")
688718
}
689719
}
690720

0 commit comments

Comments
 (0)