From 7d17eff190178a2a2c1283171450e073057f1780 Mon Sep 17 00:00:00 2001 From: Bruno Andreghetti Dantas Date: Mon, 5 Oct 2020 15:07:46 -0300 Subject: [PATCH] Add integer primitive data type --- assets/assetProp.go | 2 +- assets/dataType.go | 26 ++++++++++++++++++++++++++ transactions/argument.go | 2 +- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/assets/assetProp.go b/assets/assetProp.go index 3cb8b4f..b2b1934 100644 --- a/assets/assetProp.go +++ b/assets/assetProp.go @@ -13,7 +13,7 @@ type AssetProp struct { ReadOnly bool `json:"readOnly"` /* DataType can assume the following values: - Primary types: "string", "number", "boolean", "datetime" + Primary types: "string", "number", "integer", "boolean", "datetime" Special types: : the specific asset type key (reference) as defined by in the assets packages []: an array of elements specified by as any of the above valid types diff --git a/assets/dataType.go b/assets/dataType.go index 3f7841f..a68d25d 100644 --- a/assets/dataType.go +++ b/assets/dataType.go @@ -85,6 +85,32 @@ var dataTypeMap = map[string]DataType{ return strconv.FormatUint(math.Float64bits(dataVal), 16), dataVal, nil }, }, + "integer": { + AcceptedFormats: []string{"number"}, + Parse: func(data interface{}) (string, interface{}, error) { + dataVal, ok := data.(float64) + if !ok { + propValStr, okStr := data.(string) + if !okStr { + return "", nil, errors.NewCCError("asset property should be an integer", 400) + } + var err error + dataVal, err = strconv.ParseFloat(propValStr, 64) + if err != nil { + return "", nil, errors.WrapErrorWithStatus(err, fmt.Sprintf("asset property should be an integer"), 400) + } + } + + retVal := math.Trunc(dataVal) + + if dataVal != retVal { + return "", nil, errors.NewCCError("asset property should be an integer", 400) + } + + // Float IEEE 754 hexadecimal representation + return fmt.Sprintf("%d", int64(retVal)), int64(retVal), nil + }, + }, "boolean": { AcceptedFormats: []string{"boolean"}, Parse: func(data interface{}) (string, interface{}, error) { diff --git a/transactions/argument.go b/transactions/argument.go index 5aaa7e9..7a3bd70 100644 --- a/transactions/argument.go +++ b/transactions/argument.go @@ -5,7 +5,7 @@ type Argument struct { Description string `json:"description"` /* DataType can assume the following values: - Primary types: "string", "number", "boolean", "datetime" + Primary types: "string", "number", "integer", "boolean", "datetime" Special types: @asset: any asset type defined in the assets package @key: key properties for any asset type defined in the assets package