Commit 94c42103 authored by Klaus Aehlig's avatar Klaus Aehlig

Add VTypeFloat

...in order not to have to declare floating point
values as VTypeInt and rely on the sloppiness of
the JSON specification to not distinguish between
integers and floating point numbers.
Signed-off-by: default avatarKlaus Aehlig <aehlig@google.com>
Reviewed-by: default avatarPetr Pudlak <pudlak@google.com>
parent 946c0e2b
......@@ -353,6 +353,7 @@ _QFT_NAMES = {
constants.QFT_TEXT: "Text",
constants.QFT_BOOL: "Boolean",
constants.QFT_NUMBER: "Number",
constants.QFT_NUMBER_FLOAT: "Floating-point number",
constants.QFT_UNIT: "Storage size",
constants.QFT_TIMESTAMP: "Timestamp",
constants.QFT_OTHER: "Custom",
......
......@@ -68,8 +68,8 @@ from ganeti import jstore
from ganeti.hypervisor import hv_base
from ganeti.constants import (QFT_UNKNOWN, QFT_TEXT, QFT_BOOL, QFT_NUMBER,
QFT_UNIT, QFT_TIMESTAMP, QFT_OTHER,
RS_NORMAL, RS_UNKNOWN, RS_NODATA,
QFT_NUMBER_FLOAT, QFT_UNIT, QFT_TIMESTAMP,
QFT_OTHER, RS_NORMAL, RS_UNKNOWN, RS_NODATA,
RS_UNAVAIL, RS_OFFLINE)
(NETQ_CONFIG,
......@@ -127,6 +127,7 @@ _VERIFY_FN = {
QFT_TEXT: ht.TString,
QFT_BOOL: ht.TBool,
QFT_NUMBER: ht.TInt,
QFT_NUMBER_FLOAT: ht.TFloat,
QFT_UNIT: ht.TInt,
QFT_TIMESTAMP: ht.TNumber,
QFT_OTHER: lambda _: True,
......@@ -154,6 +155,7 @@ _VTToQFT = {
constants.VTYPE_BOOL: QFT_BOOL,
constants.VTYPE_SIZE: QFT_UNIT,
constants.VTYPE_INT: QFT_NUMBER,
constants.VTYPE_FLOAT: QFT_NUMBER_FLOAT,
}
_SERIAL_NO_DOC = "%s object serial number, incremented on each modification"
......
......@@ -134,6 +134,12 @@ def ForceDictType(target, key_types, allowed_values=None):
except (ValueError, TypeError):
msg = "'%s' (value %s) is not a valid integer" % (key, target[key])
raise errors.TypeEnforcementError(msg)
elif ktype == constants.VTYPE_FLOAT:
try:
target[key] = float(target[key])
except (ValueError, TypeError):
msg = "'%s' (value %s) is not a valid float" % (key, target[key])
raise errors.TypeEnforcementError(msg)
def ValidateServiceName(name):
......
......@@ -1435,6 +1435,9 @@ vtypeBool = VTypeBool
vtypeInt :: VType
vtypeInt = VTypeInt
vtypeFloat :: VType
vtypeFloat = VTypeFloat
vtypeMaybeString :: VType
vtypeMaybeString = VTypeMaybeString
......@@ -3476,6 +3479,9 @@ qftBool = "bool"
qftNumber :: String
qftNumber = "number"
qftNumberFloat :: String
qftNumberFloat = "float"
qftOther :: String
qftOther = "other"
......@@ -3495,6 +3501,7 @@ qftAll :: FrozenSet String
qftAll =
ConstantUtils.mkSet [qftBool,
qftNumber,
qftNumberFloat,
qftOther,
qftText,
qftTimestamp,
......
......@@ -75,6 +75,7 @@ vTypeToQFT VTypeMaybeString = QFTOther
vTypeToQFT VTypeBool = QFTBool
vTypeToQFT VTypeSize = QFTUnit
vTypeToQFT VTypeInt = QFTNumber
vTypeToQFT VTypeFloat = QFTNumberFloat
-- * Result helpers
......
......@@ -99,6 +99,7 @@ $(declareSADT "FieldType"
, ("QFTText", 'C.qftText )
, ("QFTBool", 'C.qftBool )
, ("QFTNumber", 'C.qftNumber )
, ("QFTNumberFloat", 'C.qftNumberFloat )
, ("QFTUnit", 'C.qftUnit )
, ("QFTTimestamp", 'C.qftTimestamp )
, ("QFTOther", 'C.qftOther )
......
......@@ -768,6 +768,7 @@ $(THH.declareLADT ''String "VType"
, ("VTypeBool", "bool")
, ("VTypeSize", "size")
, ("VTypeInt", "int")
, ("VTypeFloat", "float")
])
$(THH.makeJSONInstance ''VType)
......
......@@ -122,6 +122,7 @@ checkResultType fdef (ResultEntry RSNormal (Just v)) =
(QFTText , JSString {}) -> passTest
(QFTBool , JSBool {}) -> passTest
(QFTNumber , JSRational {}) -> passTest
(QFTNumberFloat , JSRational {}) -> passTest
(QFTTimestamp , JSRational {}) -> passTest
(QFTUnit , JSRational {}) -> passTest
(QFTOther , _) -> passTest -- meh, QFT not precise...
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment