Skip to content

Commit d54b308

Browse files
committed
[osh/cmd_eval] Use tagswitch
1 parent 0c07304 commit d54b308

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

builtin/assign_osh.py

+28-24
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from display import ui
2121
from frontend import flag_util
2222
from frontend import args
23-
from mycpp.mylib import log
23+
from mycpp.mylib import log, tagswitch
2424
from osh import cmd_eval
2525
from osh import sh_expr_eval
2626
from data_lang import j8_lite
@@ -242,29 +242,33 @@ def _AssignVarForBuiltin(mem, rval, pair, which_scopes, flags, arith_ev,
242242
"Can't convert type %s into BashArray" %
243243
ui.ValType(old_val), pair.blame_word)
244244
elif flag_A:
245-
if old_val.tag() == value_e.Undef:
246-
# Note: We explicitly initialize BashAssoc for Undef.
247-
val = bash_impl.BashAssoc_New()
248-
elif old_val.tag() == value_e.Str:
249-
# Note: We explicitly initialize BashAssoc for Str. When
250-
# applying +=() to Str, we associate an old value to the key
251-
# '0'. OSH disables this when strict_array is turned on.
252-
assoc_val = bash_impl.BashAssoc_New()
253-
if pair.plus_eq:
254-
if mem.exec_opts.strict_array():
255-
e_die("Can't convert Str to BashAssoc (strict_array)",
256-
pair.blame_word)
257-
bash_impl.BashAssoc_SetElement(assoc_val, '0',
258-
cast(value.Str, old_val).s)
259-
val = assoc_val
260-
elif old_val.tag() == value_e.BashAssoc:
261-
# We do not need adjustments for -A.
262-
pass
263-
else:
264-
# Note: BashArray cannot be converted to a BashAssoc
265-
e_die(
266-
"Can't convert type %s into BashAssoc" %
267-
ui.ValType(old_val), pair.blame_word)
245+
with tagswitch(old_val) as case:
246+
if case(value_e.Undef):
247+
# Note: We explicitly initialize BashAssoc for Undef.
248+
val = bash_impl.BashAssoc_New()
249+
elif case(value_e.Str):
250+
# Note: We explicitly initialize BashAssoc for Str. When
251+
# applying +=() to Str, we associate an old value to the
252+
# key '0'. OSH disables this when strict_array is turned
253+
# on.
254+
assoc_val = bash_impl.BashAssoc_New()
255+
if pair.plus_eq:
256+
if mem.exec_opts.strict_array():
257+
e_die(
258+
"Can't convert Str to BashAssoc (strict_array)",
259+
pair.blame_word)
260+
bash_impl.BashAssoc_SetElement(
261+
assoc_val, '0',
262+
cast(value.Str, old_val).s)
263+
val = assoc_val
264+
elif case(value_e.BashAssoc):
265+
# We do not need adjustments for -A.
266+
pass
267+
else:
268+
# Note: BashArray cannot be converted to a BashAssoc
269+
e_die(
270+
"Can't convert type %s into BashAssoc" %
271+
ui.ValType(old_val), pair.blame_word)
268272

269273
val = cmd_eval.ListInitializeTarget(val, initializer, pair.plus_eq,
270274
mem.exec_opts, pair.blame_word)

0 commit comments

Comments
 (0)