Skip to content

Commit 1ec3912

Browse files
author
Andy C
committed
[j8 refactor] Rename options
Prepare to add options for NON_DATA_IS_NULL and NON_DATA_IS_ERROR
1 parent b5aaf1f commit 1ec3912

File tree

2 files changed

+16
-25
lines changed

2 files changed

+16
-25
lines changed

data_lang/j8.py

+14-23
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,16 @@ def Utf8Encode(code):
136136

137137

138138
SHOW_CYCLES = 1 << 1 # show as [...] or {...} or (...), with object ID
139-
SHOW_NON_DATA = 1 << 2 # non-data objects like Eggex can be <Eggex 0xff>
140-
LOSSY_JSON = 1 << 3 # JSON may lose data about strings
141-
INF_NAN_ARE_NULL = 1 << 4 # for JSON
139+
NON_DATA_PRETTY_PRINT = 1 << 2 # non-data objects like Eggex can be <Eggex 0xff>
140+
LOSSY_JSON_STRINGS = 1 << 3 # JSON may lose data about strings
141+
INF_NAN_ARE_NULL = 1 << 4 # another lossy json issue
142+
143+
# TODO:
144+
# - default is NON_DATA_PRETTY_PRINT
145+
# - opt into either NON_DATA_IS_ERROR or NON_DATA_IS_NULL
142146

143147
# Hack until we fully translate
144-
assert pyj8.LOSSY_JSON == LOSSY_JSON
148+
assert pyj8.LOSSY_JSON_STRINGS == LOSSY_JSON_STRINGS
145149

146150

147151
def _Print(val, buf, indent, options=0):
@@ -170,35 +174,22 @@ def PrintJsonMessage(val, buf, indent):
170174
Caller must handle error.Encode()
171175
Doesn't decay to b'' strings - will use Unicode replacement char.
172176
"""
173-
_Print(val, buf, indent, options=LOSSY_JSON | INF_NAN_ARE_NULL)
177+
_Print(val, buf, indent, options=LOSSY_JSON_STRINGS | INF_NAN_ARE_NULL)
174178

175179

176180
def PrintLine(val, f):
177181
# type: (value_t, mylib.Writer) -> None
178-
""" For pp line (x) """
182+
""" For pp test_ (x) """
179183

180184
# error.Encode should be impossible - we show cycles and non-data
181185
buf = mylib.BufWriter()
182186

183-
_Print(val, buf, -1, options=SHOW_CYCLES | SHOW_NON_DATA)
187+
_Print(val, buf, -1, options=SHOW_CYCLES | NON_DATA_PRETTY_PRINT)
184188

185189
f.write(buf.getvalue())
186190
f.write('\n')
187191

188192

189-
if 0:
190-
191-
def Repr(val):
192-
# type: (value_t) -> str
193-
""" Unused
194-
This is like Python's repr
195-
"""
196-
# error.Encode should be impossible - we show cycles and non-data
197-
buf = mylib.BufWriter()
198-
_Print(val, buf, -1, options=SHOW_CYCLES | SHOW_NON_DATA)
199-
return buf.getvalue()
200-
201-
202193
def EncodeString(s, buf, unquoted_ok=False):
203194
# type: (str, mylib.BufWriter, bool) -> None
204195
""" For pp proc, etc."""
@@ -229,7 +220,7 @@ def MaybeEncodeJsonString(s):
229220
# TODO: add unquoted_ok here?
230221
# /usr/local/foo-bar/x.y/a_b
231222
buf = mylib.BufWriter()
232-
_Print(value.Str(s), buf, -1, options=LOSSY_JSON)
223+
_Print(value.Str(s), buf, -1, options=LOSSY_JSON_STRINGS)
233224
return buf.getvalue()
234225

235226

@@ -575,7 +566,7 @@ def Print(self, val, level=0):
575566
elif case(value_e.Obj):
576567
val = cast(Obj, UP_val)
577568

578-
if not (self.options & SHOW_NON_DATA):
569+
if not (self.options & NON_DATA_PRETTY_PRINT):
579570
raise error.Encode("Can't encode value of type Obj")
580571

581572
# Cycle detection, only for containers that can be in cycles
@@ -609,7 +600,7 @@ def Print(self, val, level=0):
609600

610601
else:
611602
pass # mycpp workaround
612-
if self.options & SHOW_NON_DATA:
603+
if self.options & NON_DATA_PRETTY_PRINT:
613604
# Similar to = operator, ui.DebugPrint()
614605
# TODO: that prints value.Range in a special way
615606
ysh_type = ValType(val)

data_lang/pyj8.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
_ = log
1010

11-
LOSSY_JSON = 1 << 3
11+
LOSSY_JSON_STRINGS = 1 << 3
1212

1313

1414
def WriteString(s, options, buf):
@@ -17,7 +17,7 @@ def WriteString(s, options, buf):
1717
1818
The C++ version is optimized to avoid the intermediate string.
1919
"""
20-
j8_fallback = not (options & LOSSY_JSON)
20+
j8_fallback = not (options & LOSSY_JSON_STRINGS)
2121
#print('j8_fallback %d' % j8_fallback)
2222
buf.write(fastfunc.J8EncodeString(s, j8_fallback))
2323

0 commit comments

Comments
 (0)