| 49 | } |
| 50 | |
| 51 | func (parent *Namespace) AddNamespace(namespacePath []string, task Task) { |
| 52 | if len(namespacePath) == 0 { |
| 53 | return |
| 54 | } |
| 55 | |
| 56 | // If there are no child namespaces, then we have found a task and we can |
| 57 | // simply add it to the current namespace |
| 58 | if len(namespacePath) == 1 { |
| 59 | parent.Tasks = append(parent.Tasks, task) |
| 60 | return |
| 61 | } |
| 62 | |
| 63 | // Get the key of the current namespace in the path |
| 64 | namespaceKey := namespacePath[0] |
| 65 | |
| 66 | // Add the namespace to the parent namespaces map using the namespace key |
| 67 | if parent.Namespaces == nil { |
| 68 | parent.Namespaces = make(map[string]*Namespace, 0) |
| 69 | } |
| 70 | |
| 71 | // Search for the current namespace in the parent namespaces map |
| 72 | // If it doesn't exist, create it |
| 73 | namespace, ok := parent.Namespaces[namespaceKey] |
| 74 | if !ok { |
| 75 | namespace = &Namespace{} |
| 76 | parent.Namespaces[namespaceKey] = namespace |
| 77 | } |
| 78 | |
| 79 | // Remove the current namespace key from the namespace path. |
| 80 | childNamespacePath := namespacePath[1:] |
| 81 | |
| 82 | // If there are no child namespaces in the task name, then we have found the |
| 83 | // namespace of the task and we can add it to the current namespace. |
| 84 | // Otherwise, we need to go deeper |
| 85 | namespace.AddNamespace(childNamespacePath, task) |
| 86 | } |