Skip to content

Commit 530ca18

Browse files
authored
Merge pull request #20 from ulfox/feat/document-manager
[new] document-manager
2 parents 3a9aa44 + 23e2001 commit 530ca18

File tree

9 files changed

+873
-152
lines changed

9 files changed

+873
-152
lines changed

README.md

Lines changed: 208 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,33 @@ Table of Contents
77
- [DB Yaml](#db-yaml)
88
- [Features](#features)
99
- [Usage](#usage)
10-
* [Write to DB](#write-to-db)
11-
* [Query DB](#query-db)
12-
+ [Get First Key](#get-first-key)
13-
+ [Search for Keys](#search-for-keys)
14-
* [Query Path](#query-path)
15-
+ [Query Path with Arrays](#query-path-with-arrays)
16-
- [Without trailing array](#without-trailing-array)
17-
- [With trailing array](#with-trailing-array)
18-
* [Delete Key By Path](#delete-key-by-path)
19-
* [Convert Utils](#convert-utils)
10+
* [Write to DB](#write-to-db)
11+
* [Query DB](#query-db)
12+
+ [Get First Key](#get-first-key)
13+
+ [Search for Keys](#search-for-keys)
14+
* [Query Path](#query-path)
15+
+ [Query Path with Arrays](#query-path-with-arrays)
16+
- [Without trailing array](#without-trailing-array)
17+
- [With trailing array](#with-trailing-array)
18+
* [Delete Key By Path](#delete-key-by-path)
19+
* [Convert Utils](#convert-utils)
2020
+ [Get map of strings from interface](#get-map-of-strings-from-interface)
2121
- [Get map directly from a GetPath object](#get-map-directly-from-a-getpath-object)
22-
- [Get map manually](#get-map-manually)
23-
+ [Get array of string from interface](#get-array-of-string-from-interface)
22+
- [Get map manually](#get-map-manually)
23+
+ [Get array of string from interface](#get-array-of-string-from-interface)
2424
- [Get array directly from a GetPath object](#get-array-directly-from-a-getpath-object)
25-
- [Get array manually](#get-array-manually)
25+
- [Get array manually](#get-array-manually)
26+
* [Document Management](#document-management)
27+
+ [Add a new doc](#add-a-new-doc)
28+
+ [Switch Doc](#switch-doc)
29+
+ [Document names](#document-names)
30+
- [Name documents manually](#name-documents-manually)
31+
- [Name all documents automatically](#name-all-documents-automatically)
32+
- [Switch between docs by name](#switch-between-docs-by-name)
33+
+ [Import Docs](#import-docs)
34+
+ [Global Upsert](#global-upsert)
35+
+ [Global Update](#global-update)
36+
2637

2738
## Features
2839

@@ -111,7 +122,7 @@ Get all they keys (if any). This returns the full path for the key,
111122
not the key values. To get the values check the next section **GetPath**
112123

113124
```go
114-
keys, err := state.Get("key-1")
125+
keys, err := state.FindKeys("key-1")
115126
if err != nil {
116127
logger.Fatalf(err.Error())
117128
}
@@ -181,7 +192,7 @@ key-1:
181192

182193
To get the first index of `key-2`, issue
183194

184-
```
195+
```go
185196
keyPath, err := state.GetPath("key-1.key-2.[0]")
186197
if err != nil {
187198
logger.Fatalf(err.Error())
@@ -331,3 +342,185 @@ vArray := assertData.GetArray()
331342
logger.Info(vArray)
332343
333344
```
345+
346+
### Document Management
347+
348+
DBy creates by default an array of documents called library. That is in fact an array of interfaces
349+
350+
When initiating DBy, document 0 (index 0) is creatd by default and any action is done to that document, unless we switch to a new one
351+
352+
#### Add a new doc
353+
354+
To add a new doc, issue
355+
356+
```go
357+
err = state.AddDoc()
358+
if err != nil {
359+
logger.Fatal(err)
360+
}
361+
362+
```
363+
364+
**Note: Adding a new doc also switches the pointer to that doc. Any action will write/read from the new doc by default**
365+
366+
#### Switch Doc
367+
368+
To switch a different document, we can use **Switch** method that takes as an argument an index
369+
370+
For example to switch to doc 1 (second doc), issue
371+
372+
```go
373+
err = state.Switch(1)
374+
if err != nil {
375+
logger.Fatal(err)
376+
}
377+
```
378+
379+
#### Document names
380+
381+
When we work with more than 1 document, we may want to set names in order to easily switch between docs
382+
383+
We have 2 ways to name our documents
384+
385+
- Add a name to each document manually
386+
- Add a name providing a path that exists in all documents
387+
388+
##### Name documents manually
389+
390+
To name a document manually, we can use the **SetName** method which takes 2 arguments
391+
392+
- name
393+
- doc index
394+
395+
For example to name document with index 0, as myDoc
396+
397+
```go
398+
err := state.SetName("myDoc", 0)
399+
if err != nil {
400+
logger.Fatal(err)
401+
}
402+
```
403+
404+
##### Name all documents automatically
405+
406+
To name all documents automatically we need to ensure that the same path exists in all documents.
407+
408+
The method for updating all documents is called **SetNames** and takes 2 arguments
409+
410+
- Prefix: A path in the documents that will be used for the first name
411+
- Suffix: A path in the documents that will be used for the last name
412+
413+
**Note: Docs that do not have the paths that are queried will not get a name**
414+
415+
This method best works with **Kubernetes** manifests, where all docs have a common set of fields.
416+
417+
For example
418+
419+
```yaml
420+
apiVersion: someApi-0
421+
kind: someKind-0
422+
metadata:
423+
...
424+
name: someName-0
425+
...
426+
---
427+
apiVersion: someApi-1
428+
kind: someKind-1
429+
metadata:
430+
...
431+
name: someName-1
432+
...
433+
---
434+
```
435+
436+
From above we could give a name for all our documents if we use **kind** + **metadata.name** for the name.
437+
438+
```go
439+
err := state.SetNames("kind", "metadata.name")
440+
if err != nil {
441+
logger.Fatal(err)
442+
}
443+
```
444+
445+
###### List all doc names
446+
447+
To get the name of all named docs, issue
448+
449+
```go
450+
for i, j := range state.ListDocs() {
451+
fmt.Println(i, j)
452+
}
453+
```
454+
Example output based on the previous **SetNames** example
455+
456+
```bash
457+
0 service/listener-svc
458+
1 poddisruptionbudget/listener-svc
459+
2 horizontalpodautoscaler/caller-svc
460+
3 deployment/caller-svc
461+
4 service/caller-svc
462+
5 poddisruptionbudget/caller-svc
463+
6 horizontalpodautoscaler/listener-svc
464+
7 deployment/listener-svc
465+
```
466+
467+
##### Switch between docs by name
468+
469+
To switch to a doc by using the doc's name, issue
470+
471+
```go
472+
err = state.SwitchDoc("PodDisruptionBudget/caller-svc")
473+
if err != nil {
474+
logger.Fatal(err)
475+
}
476+
```
477+
478+
#### Import Docs
479+
480+
We can import a set of docs with **ImportDocs** method. For example if we have the following yaml
481+
482+
```yaml
483+
apiVersion: someApi-0
484+
kind: someKind-0
485+
metadata:
486+
...
487+
name: someName-0
488+
...
489+
---
490+
apiVersion: someApi-1
491+
kind: someKind-1
492+
metadata:
493+
...
494+
name: someName-1
495+
...
496+
---
497+
```
498+
499+
We can import it by giving the path of the file
500+
501+
```go
502+
err = state.ImportDocs("file-name.yaml")
503+
if err != nil {
504+
logger.Fatal(err)
505+
}
506+
```
507+
508+
#### Global Upsert
509+
510+
We can use upsert to update or create keys on all documents
511+
512+
```go
513+
err = state.UpsertGlobal(
514+
"some.path",
515+
"v0.3.0",
516+
)
517+
if err != nil {
518+
logger.Fatal(err)
519+
}
520+
521+
```
522+
523+
#### Global Update
524+
525+
Global update works as **GlobalUpsert** but it skips documents that
526+
miss a path rather than creating the path on those docs.

0 commit comments

Comments
 (0)