From 4f31de893471819dbe6e6c1e3b621acef21bf0aa Mon Sep 17 00:00:00 2001 From: Levi Gross Date: Sun, 26 Nov 2017 18:36:47 -0500 Subject: [PATCH 1/4] 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 From 56cec8d3487cb84f64150f513b76bc430fb9ec91 Mon Sep 17 00:00:00 2001 From: Levi Gross Date: Sun, 26 Nov 2017 18:47:04 -0500 Subject: [PATCH 2/4] Ensure decoder is set as well Signed-off-by: Levi Gross --- jwriter/writer.go | 17 +++++++++++++++-- tests/data.go | 4 ++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/jwriter/writer.go b/jwriter/writer.go index e5a5ddf..b9ed7cc 100644 --- a/jwriter/writer.go +++ b/jwriter/writer.go @@ -240,11 +240,25 @@ func (w *Writer) Float32(n float32) { w.Buffer.Buf = strconv.AppendFloat(w.Buffer.Buf, float64(n), 'g', -1, 32) } +func (w *Writer) Float32Str(n float32) { + w.Buffer.EnsureSpace(20) + w.Buffer.Buf = append(w.Buffer.Buf, '"') + w.Buffer.Buf = strconv.AppendFloat(w.Buffer.Buf, float64(n), 'g', -1, 32) + w.Buffer.Buf = append(w.Buffer.Buf, '"') +} + func (w *Writer) Float64(n float64) { w.Buffer.EnsureSpace(20) w.Buffer.Buf = strconv.AppendFloat(w.Buffer.Buf, n, 'g', -1, 64) } +func (w *Writer) Float64Str(n float64) { + w.Buffer.EnsureSpace(20) + w.Buffer.Buf = append(w.Buffer.Buf, '"') + w.Buffer.Buf = strconv.AppendFloat(w.Buffer.Buf, float64(n), 'g', -1, 64) + w.Buffer.Buf = append(w.Buffer.Buf, '"') +} + func (w *Writer) Bool(v bool) { w.Buffer.EnsureSpace(5) if v { @@ -340,12 +354,11 @@ func (w *Writer) base64(in []byte) { return } - w.Buffer.EnsureSpace(((len(in) - 1) / 3 + 1) * 4) + w.Buffer.EnsureSpace(((len(in)-1)/3 + 1) * 4) si := 0 n := (len(in) / 3) * 3 - for si < n { // Convert 3x 8bit source bytes into 4 bytes val := uint(in[si+0])<<16 | uint(in[si+1])<<8 | uint(in[si+2]) diff --git a/tests/data.go b/tests/data.go index a6e294c..5f18a98 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 `json:", string"` - Float64 float64 `json:", string"` + Float32 float32 `json:",string"` + Float64 float64 `json:",string"` Ptr *string PtrNil *string From b5dedd1b9efc7a41be93ea048beaad2ea201712e Mon Sep 17 00:00:00 2001 From: Levi Gross Date: Sun, 26 Nov 2017 18:55:54 -0500 Subject: [PATCH 3/4] We now include and pass tests Signed-off-by: Levi Gross --- gen/decoder.go | 4 ++-- tests/data.go | 13 +++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/gen/decoder.go b/gen/decoder.go index 184b229..f7e415f 100644 --- a/gen/decoder.go +++ b/gen/decoder.go @@ -48,8 +48,8 @@ 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", + reflect.Float32: "in.Float32Str()", + reflect.Float64: "in.Float64Str()", } var customDecoders = map[string]string{ diff --git a/tests/data.go b/tests/data.go index 5f18a98..f6d6653 100644 --- a/tests/data.go +++ b/tests/data.go @@ -38,8 +38,11 @@ type PrimitiveTypes struct { Uint32String uint32 `json:",string"` Uint64String uint64 `json:",string"` - Float32 float32 `json:",string"` - Float64 float64 `json:",string"` + Float32 float32 + Float64 float64 + + Float32String float32 `json:",string"` + Float64String float64 `json:",string"` Ptr *string PtrNil *string @@ -77,6 +80,9 @@ var primitiveTypesValue = PrimitiveTypes{ Float32: 1.5, Float64: math.MaxFloat64, + Float32String: 1.5, + Float64String: math.MaxFloat64, + Ptr: &str, } @@ -110,6 +116,9 @@ var primitiveTypesString = "{" + `"Float32":` + fmt.Sprint(1.5) + `,` + `"Float64":` + fmt.Sprint(math.MaxFloat64) + `,` + + `"Float32String":"` + fmt.Sprint(1.5) + `",` + + `"Float64String":"` + fmt.Sprint(math.MaxFloat64) + `",` + + `"Ptr":"bla",` + `"PtrNil":null` + From 5fb2687db09307a2b6f21ecda85a608c5ea2292a Mon Sep 17 00:00:00 2001 From: Levi Gross Date: Mon, 27 Nov 2017 20:29:13 -0500 Subject: [PATCH 4/4] No need to mark as float Signed-off-by: Levi Gross --- jlexer/lexer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jlexer/lexer.go b/jlexer/lexer.go index b37c5be..18d65cd 100644 --- a/jlexer/lexer.go +++ b/jlexer/lexer.go @@ -1043,7 +1043,7 @@ func (r *Lexer) Float64Str() float64 { Data: string(b), }) } - return float64(n) + return n } func (r *Lexer) Error() error {