| 102 | |
| 103 | == Practical example for using "-type", "-spec" related selectors and properties: == |
| 104 | Let's say we are new to a project with a large code-base and our task is to implement a new interface in which we have to do some operations on a very complex data structure. The source of this complex structure is remote, but we know there are multiple compatible data structures already implemented, although not sure about the specifics, so we would like to investigate and do this without manually going through several thousand lines of code. |
| 105 | |
| 106 | We might start by running the following query: |
| 107 | {{{ |
| 108 | "files.typerefs" |
| 109 | }}} |
| 110 | This gives us all used types in every file. After scrolling to the end of the hundred and something pages of results, we at least know types properly specified, but not much else. |
| 111 | Maybe we should only look at types defined in "general" modules: |
| 112 | {{{ |
| 113 | "mods[name ~ \"gen\"].types" |
| 114 | }}} |
| 115 | Unfortunately these seem to be either irrelevant or too general for us to use. We decide that we should look at types from other modules by filtering results using the "-spec" attributes (and let's assume, we are looking for a structure that has an "append" function implemented). |
| 116 | But first we would like to know if "-spec" is used in our modules. |
| 117 | {{{ |
| 118 | "mods.funs[not (.name in files.specs.name)][loc > 10]" |
| 119 | }}} |
| 120 | The query results are promising, it looks like most of the non-trivial functions have a specification. |
| 121 | We can take a look at returned types of functions named like "append": |
| 122 | {{{ |
| 123 | mods.funs[name ~ \"append\"].returntype |
| 124 | }}} |
| 125 | This query also gives us too many possible results, we are only interested in exported ones: |
| 126 | {{{ |
| 127 | mods.funs[name ~ \"append\"].returntype[exported] |
| 128 | }}} |
| 129 | Turns out most of them were exported. However when we have looked at types from "gen" modules, we may have noticed a type("person") for a structure which is a part of what we have to append to the large structure. |
| 130 | {{{ |
| 131 | mods.funs[name ~ \"append\"][.params.type.(subtype.(params)+)+[name = 'person']].returntype[exported] |
| 132 | }}} |
| 133 | This query only yields return-types of ~"append" functions that have a parameter whose type uses the "person" type. |
| 134 | If we would like to see in which files these types have been used we could run the following: |
| 135 | {{{ |
| 136 | mods.funs[name ~ \"append\"][.params.type.(subtype.(params)+)+[name = 'person']].returntype[exported].references.file |
| 137 | }}} |