in cmd/generate-fastjson/main.go [305:349]
func generateBasicValue(w *bytes.Buffer, expr string, exprType *types.Basic) {
convert := func(t string) {
expr = fmt.Sprintf("%s(%s)", t, expr)
}
var method string
switch k := exprType.Kind(); k {
case types.Bool:
method = "Bool"
case types.Int, types.Int8, types.Int16, types.Int32:
convert("int64")
method = "Int64"
case types.Int64:
method = "Int64"
case types.Uint, types.Uint8, types.Uint16, types.Uint32:
convert("uint64")
method = "Uint64"
case types.Uint64:
method = "Uint64"
case types.Float32:
method = "Float32"
fmt.Fprintf(w, `
if math.IsNaN(float64(%s)) {
return errors.New("json: '%s': unsupported value: NaN")
}
if math.IsInf(float64(%s), 0) {
return errors.New("json: '%s': unsupported value: Inf")
}
`[1:], expr, expr, expr, expr)
case types.Float64:
method = "Float64"
fmt.Fprintf(w, `
if math.IsNaN(%s) {
return errors.New("json: '%s': unsupported value: NaN")
}
if math.IsInf(%s, 0) {
return errors.New("json: '%s': unsupported value: Inf")
}
`[1:], expr, expr, expr, expr)
case types.String:
method = "String"
default:
panic(fmt.Errorf("unhandled basic kind %q", types.Typ[k]))
}
fmt.Fprintf(w, "w.%s(%s)\n", method, expr)
}