Skip to content

Commit 4cc01af

Browse files
authored
Add virtual prop support to node/type repr methods (SYN-8919) (#4209)
1 parent 71d9fae commit 4cc01af

File tree

5 files changed

+59
-9
lines changed

5 files changed

+59
-9
lines changed

synapse/lib/node.py

+20-6
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,35 @@
1414

1515
class NodeBase:
1616

17-
def repr(self, name=None, defv=None):
17+
def repr(self, name=None, defv=None, virts=None):
1818

1919
if name is None:
20-
return self.form.type.repr(self.ndef[1])
20+
typeitem = self.form.type
21+
if virts is None:
22+
return typeitem.repr(self.valu())
23+
24+
virtgetr = typeitem.getVirtGetr(virts)
25+
virttype = typeitem.getVirtType(virts)
26+
return virttype.repr(self.valu(virts=virtgetr))
2127

2228
prop = self.form.props.get(name)
2329
if prop is None:
2430
mesg = f'No property named {name}.'
2531
raise s_exc.NoSuchProp(mesg=mesg, form=self.form.name, prop=name)
2632

27-
valu = self.get(name)
28-
if valu is None:
29-
return defv
33+
typeitem = prop.type
34+
35+
if virts is None:
36+
if (valu := self.get(name)) is None:
37+
return defv
38+
return typeitem.repr(valu)
3039

31-
return prop.type.repr(valu)
40+
virtgetr = typeitem.getVirtGetr(virts)
41+
virttype = typeitem.getVirtType(virts)
42+
43+
if (valu := self.get(name, virts=virtgetr)) is None:
44+
return defv
45+
return virttype.repr(valu)
3246

3347
def reprs(self):
3448
'''

synapse/lib/stormtypes.py

+20-3
Original file line numberDiff line numberDiff line change
@@ -1644,10 +1644,17 @@ async def trycast(self, name, valu):
16441644

16451645
@stormfunc(readonly=True)
16461646
async def _repr(self, name, valu):
1647-
name = await toprim(name)
1647+
name = await tostr(name)
16481648
valu = await toprim(valu)
16491649

1650-
return self._reqTypeByName(name).repr(valu)
1650+
parts = name.strip().split('*')
1651+
name = parts[0]
1652+
1653+
typeitem = self._reqTypeByName(name)
1654+
if len(parts) > 1:
1655+
typeitem = typeitem.getVirtType(parts[1:])
1656+
1657+
return typeitem.repr(valu)
16511658

16521659
@stormfunc(readonly=True)
16531660
async def _exit(self, mesg=None, **kwargs):
@@ -6188,7 +6195,17 @@ async def _methNodeNdef(self):
61886195

61896196
@stormfunc(readonly=True)
61906197
async def _methNodeRepr(self, name=None, defv=None):
6191-
return self.valu.repr(name=name, defv=defv)
6198+
name = await toprim(name)
6199+
defv = await toprim(defv)
6200+
virts = None
6201+
6202+
if name is not None:
6203+
parts = name.strip().split('*')
6204+
if len(parts) > 1:
6205+
name = parts[0] or None
6206+
virts = parts[1:]
6207+
6208+
return self.valu.repr(name=name, virts=virts, defv=defv)
61926209

61936210
@stormfunc(readonly=True)
61946211
async def _methNodeIden(self):

synapse/lib/types.py

+9
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ def getVirtType(self, virts):
175175
return virt[0].getVirtType(virts[1:])
176176
return virt[0]
177177

178+
def getVirtGetr(self, virts):
179+
name = virts[0]
180+
if (virt := self.virts.get(name)) is None:
181+
raise s_exc.NoSuchVirt.init(name, self)
182+
183+
if len(virts) > 1:
184+
return (virt[1],) + virt[0].getVirtGetr(virts[1:])
185+
return (virt[1],)
186+
178187
def getRuntPode(self):
179188

180189
ctor = '.'.join([self.__class__.__module__, self.__class__.__qualname__])

synapse/tests/test_lib_stormtypes.py

+7
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,8 @@ async def getseqn(genr, name, key):
955955
self.none(view.parent)
956956
self.none(view.info.get('parent'))
957957

958+
self.eq('0.0.0.1', await core.callStorm('return($lib.repr(inet:server*ip, ([4, 1])))'))
959+
958960
async def test_storm_lib_ps(self):
959961

960962
async with self.getTestCore() as core:
@@ -1224,6 +1226,11 @@ async def test_storm_lib_node(self):
12241226
pode[1].pop('path')
12251227
self.eq(pode, apode)
12261228

1229+
self.eq('1.2.3.4', await core.callStorm('[ inet:server=1.2.3.4:80 ] return($node.repr(*ip))'))
1230+
self.eq('1.2.3.4', await core.callStorm('[ inet:flow=* :dst=1.2.3.4:80 ] return($node.repr(dst*ip))'))
1231+
1232+
self.eq(None, await core.callStorm('[ inet:flow=* ] return($node.repr(dst*ip))'))
1233+
12271234
async def test_storm_lib_dict(self):
12281235
async with self.getTestCore() as core:
12291236
nodes = await core.nodes('$blah = ({"foo": "vertex.link"}) [ inet:fqdn=$blah.foo ]')

synapse/tests/test_lib_types.py

+3
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,9 @@ async def test_ival(self):
900900
with self.raises(s_exc.NoSuchVirt):
901901
ival.getVirtType(['min', 'newp'])
902902

903+
with self.raises(s_exc.NoSuchVirt):
904+
ival.getVirtGetr(['min', 'newp'])
905+
903906
async def test_loc(self):
904907
model = s_datamodel.Model()
905908
loctype = model.types.get('loc')

0 commit comments

Comments
 (0)