-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix int64 handling #19
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ import ( | |
"encoding/json" | ||
"fmt" | ||
"path" | ||
"strconv" | ||
"time" | ||
|
||
"cloud.google.com/go/civil" | ||
|
@@ -36,13 +35,20 @@ func (c *JSONColumn) marshal(t *gspanner.Type, v *structpb.Value) (interface{}, | |
// See: https://godoc.org/google.golang.org/genproto/googleapis/spanner/v1#TypeCode | ||
switch t.Code { | ||
case gspanner.TypeCode_INT64: | ||
// JavaScript's integral part is 53bit. see Number.MAX_SAFE_INTEGER. | ||
return v.GetStringValue(), nil | ||
case gspanner.TypeCode_FLOAT64: | ||
s := v.GetStringValue() | ||
n, err := strconv.ParseInt(s, 10, 64) | ||
if err != nil { | ||
return nil, err | ||
switch s { | ||
case "NaN", "Infinity", "-Infinity": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. この辺はドキュメントに従って書いてあるとおりにコード書いた感じ(実環境での動作確認はしていない |
||
// NaN, Infinity, -Infinity will be null on JSON with JavaScript. | ||
// http://www.ecma-international.org/ecma-262/10.0/index.html#sec-serializejsonproperty | ||
return nil, nil | ||
} | ||
return n, nil | ||
case gspanner.TypeCode_FLOAT64: | ||
// golang: https://golang.org/ref/spec#Numeric_types | ||
// float64 the set of all IEEE-754 64-bit floating-point numbers | ||
// ECMAScript: http://www.ecma-international.org/ecma-262/10.0/index.html#sec-ecmascript-language-types-number-type | ||
// representing the double-precision 64-bit format IEEE 754-2008 values as specified in the IEEE Standard for Binary Floating-Point Arithmetic | ||
return v.GetNumberValue(), nil | ||
case gspanner.TypeCode_STRING: | ||
return v.GetStringValue(), nil | ||
|
@@ -99,7 +105,10 @@ func (c *JSONColumn) schema(o JSONObject, t *gspanner.Type, options ...jsonschem | |
switch t.Code { | ||
default: | ||
return fmt.Errorf("unsupport type: type:%v", t) | ||
case gspanner.TypeCode_INT64, gspanner.TypeCode_FLOAT64: | ||
case gspanner.TypeCode_INT64: | ||
o.Set("type", "string") | ||
o.Set("format", "int64") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stringと区別はしたいはずなので悩みつつこうした。 |
||
case gspanner.TypeCode_FLOAT64: | ||
o.Set("type", "number") | ||
case gspanner.TypeCode_STRING: | ||
o.Set("type", "string") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Goのint64だとJSで処理可能な範囲を超えて数値が勝手に丸められたりするので文字列で送る必要がある。
値によってエンコード方法を変えると辛いと思うので一括で文字列に。