Properties data query with (for/each)

What I’m trying to do

I’m going to create a config file for each switch in my network.
Therefore I have created a properties section with a description for each switchport.
Like this:

  P1: FW Uplink
  P2: Server A
  P8: Distribution Switch C
  P10: Server C
  P11: Server D

The goal would be to go through each line in port_description and generate a separate output line. Something like a for/each command.
Like this:
configure ports 1 description FW Uplink
configure ports 2 description Server A
configure ports 8 description Distribution Switch C
configure ports 10 description Server C
configure ports 11 description Server D

Things I have tried

The current config looks like this:

configure ports 1 description `= this.port_description.P1`
configure ports 2 description `= this.port_description.P2`
configure ports 8 description `= this.port_description.P8`
configure ports 10 description `= this.port_description.P10`
configure ports 11 description `= this.port_description.P11`

It is working but there is no sort of automation.
I have to create manually each line together with a manual port number.

I also tried this with dataview but the output does not work well when you want to do a copy/paste to your device.

It would be great if someone could point me to the right direction.

Thank you

1 Like

OK, so here’s what I came up with (though it requires a " ```dataviewjs" codeblock):

let ports = dv.current().port_description;
Object.keys(ports).forEach(key =>
	dv.el("p", `configure ${key.replace("P", "port ")} description: ${ports[key]}`)

Result in both Live Preview & Reading mode:


What do you mean by this? How does it not work well with copy/paste? And which kind of query did you do?

Regarding solutions on how to automate this using dataview, you’ve got the option of using dataviewjs as suggested by @woofy31 (and variants thereof). Using plain dataview queries (aka DQL queries), it’s very hard to loop on various keys of a property, so in most cases you’re better of with either of the following solutions:

  • using a compound object, which doesn’t look good in the properties editor
  • constructing strings which are easily splittable for later usage
  • leaving the properties all alone, and using lists with multiple fields to simulate the compound objects

Lets tour these options one by one

Compound objects

Your original markup is already compound objects, so they don’t really look that nice in the properties editor:

They can however be slightly rewritten to better allow for querying, but which would still look kind of bad in the property editor:

- port: 1
  desc: FW Uplink
- port: 12
  desc: Server A
- port: 8
  desc: Switch C

This would allow for a query on the compound object, and then extract the bits and pieces like in this query:

LIST WITHOUT ID "Port " + C.port + ": " + C.desc
FLATTEN compound as C
WHERE file = this.file
SORT C.port

String construction and split

Another option is to make a pure list in the property editor, but one list allowing for easy splits by using a unique character, like in:

- 1 / FW Uplink
- 12 / Server A
- 8 / Switch C

With the query:
LIST WITHOUT ID "Port " + C[0] + ": " + C[1]
FLATTEN using_string as item
FLATTEN list(split(item, "/")) as C
WHERE file = this.file

This variant would allow for actual editing within the properties editor, both removal and editing of the description. This look like this in my view when right clicking the last item:

Using lists

Lastly if you use inline fields in lists, it could look like this:

- Port (port:: 1): (desc:: FW Uplink)
- Port (port:: 12):  (desc:: Server A)
- Port (port:: 8): (desc:: Switch C)

Which in any case could be copy-pasted directly as it has the wanted output, but it would still allow querying from other places using a query like:

TABLE WITHOUT ID item.port as "Number", item.desc as "Description"
FLATTEN file.lists as item
WHERE item.port
SORT item.port

Which produces a output like:

All of the above queries could be changed to/from LIST or TABLE, but you get the gist of it. The latter variant using the lists in the body, could possibly be the one allowing for better copy/paste (though that’s a little dependending on from where and how you copy/paste).

1 Like

to both of you, Thank you very much.
The combination of Compound and Flatten in dataview did the trick.
This keeps everything nice and clean.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.