llvm.org GIT mirror llvm / 7443038
[LIT] Add JSONMetricValue type to wrap types supported by the json encoder. Summary: The following types can be encoded and decoded by the json library: `dict`, `list`, `tuple`, `str`, `unicode`, `int`, `long`, `float`, `bool`, `NoneType`. `JSONMetricValue` can be constructed with any of these types, and used as part of Test.Result. This patch also adds a toMetricValue function that converts a value into a MetricValue. Reviewers: ddunbar, EricWF Reviewed By: EricWF Subscribers: cfe-commits, llvm-commits Differential Revision: http://reviews.llvm.org/D6576 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@224628 91177308-0d34-0410-b5e6-96231b3b80d8 Eric Fiselier 4 years ago
1 changed file(s) with 36 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 import os
11 from xml.sax.saxutils import escape
2 from json import JSONEncoder
23
34 # Test result codes.
45
7172
7273 def todata(self):
7374 return self.value
75
76 class JSONMetricValue(MetricValue):
77 """
78 JSONMetricValue is used for types that are representable in the output
79 but that are otherwise uninterpreted.
80 """
81 def __init__(self, value):
82 # Ensure the value is a serializable by trying to encode it.
83 # WARNING: The value may change before it is encoded again, and may
84 # not be encodable after the change.
85 try:
86 e = JSONEncoder()
87 e.encode(value)
88 except TypeError:
89 raise
90 self.value = value
91
92 def format(self):
93 return str(self.value)
94
95 def todata(self):
96 return self.value
97
98 def toMetricValue(value):
99 if isinstance(value, MetricValue):
100 return value
101 elif isinstance(value, int) or isinstance(value, long):
102 return IntMetricValue(value)
103 elif isinstance(value, float):
104 return RealMetricValue(value)
105 else:
106 # Try to create a JSONMetricValue and let the constructor throw
107 # if value is not a valid type.
108 return JSONMetricValue(value)
109
74110
75111 # Test results.
76112