From 4f31de893471819dbe6e6c1e3b621acef21bf0aa Mon Sep 17 00:00:00 2001 From: Levi Gross Date: Sun, 26 Nov 2017 18:36:47 -0500 Subject: [PATCH] Add float string functions when requested Signed-off-by: Levi Gross --- gen/decoder.go | 6 ++++-- gen/encoder.go | 2 ++ jlexer/lexer.go | 32 ++++++++++++++++++++++++++++++++ tests/data.go | 4 ++-- 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/gen/decoder.go b/gen/decoder.go index 021933a..184b229 100644 --- a/gen/decoder.go +++ b/gen/decoder.go @@ -48,10 +48,12 @@ var primitiveStringDecoders = map[reflect.Kind]string{ reflect.Uint32: "in.Uint32Str()", reflect.Uint64: "in.Uint64Str()", reflect.Uintptr: "in.UintptrStr()", + reflect.Float32: "in.Float32()Str", + reflect.Float64: "in.Float64()Str", } var customDecoders = map[string]string{ - "json.Number": "in.JsonNumber()", + "json.Number": "in.JsonNumber()", } // genTypeDecoder generates decoding code for the type t, but uses unmarshaler interface if implemented by t. @@ -88,7 +90,7 @@ func (g *Generator) genTypeDecoder(t reflect.Type, out string, tags fieldTags, i func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags fieldTags, indent int) error { ws := strings.Repeat(" ", indent) // Check whether type is primitive, needs to be done after interface check. - if dec := customDecoders[t.String()]; dec != "" { + if dec := customDecoders[t.String()]; dec != "" { fmt.Fprintln(g.out, ws+out+" = "+dec) return nil } else if dec := primitiveStringDecoders[t.Kind()]; dec != "" && tags.asString { diff --git a/gen/encoder.go b/gen/encoder.go index 48cba15..3a26d72 100644 --- a/gen/encoder.go +++ b/gen/encoder.go @@ -45,6 +45,8 @@ var primitiveStringEncoders = map[reflect.Kind]string{ reflect.Uint32: "out.Uint32Str(uint32(%v))", reflect.Uint64: "out.Uint64Str(uint64(%v))", reflect.Uintptr: "out.UintptrStr(uintptr(%v))", + reflect.Float32: "out.Float32Str(float32(%v))", + reflect.Float64: "out.Float64Str(float64(%v))", } // fieldTags contains parsed version of json struct field tags. diff --git a/jlexer/lexer.go b/jlexer/lexer.go index e5558ae..b37c5be 100644 --- a/jlexer/lexer.go +++ b/jlexer/lexer.go @@ -997,6 +997,22 @@ func (r *Lexer) Float32() float32 { return float32(n) } +func (r *Lexer) Float32Str() float32 { + s, b := r.unsafeString() + if !r.Ok() { + return 0 + } + n, err := strconv.ParseFloat(s, 32) + if err != nil { + r.addNonfatalError(&LexerError{ + Offset: r.start, + Reason: err.Error(), + Data: string(b), + }) + } + return float32(n) +} + func (r *Lexer) Float64() float64 { s := r.number() if !r.Ok() { @@ -1014,6 +1030,22 @@ func (r *Lexer) Float64() float64 { return n } +func (r *Lexer) Float64Str() float64 { + s, b := r.unsafeString() + if !r.Ok() { + return 0 + } + n, err := strconv.ParseFloat(s, 64) + if err != nil { + r.addNonfatalError(&LexerError{ + Offset: r.start, + Reason: err.Error(), + Data: string(b), + }) + } + return float64(n) +} + func (r *Lexer) Error() error { return r.fatalError } diff --git a/tests/data.go b/tests/data.go index 145f093..a6e294c 100644 --- a/tests/data.go +++ b/tests/data.go @@ -38,8 +38,8 @@ type PrimitiveTypes struct { Uint32String uint32 `json:",string"` Uint64String uint64 `json:",string"` - Float32 float32 - Float64 float64 + Float32 float32 `json:", string"` + Float64 float64 `json:", string"` Ptr *string PtrNil *string