| 389 | } |
| 390 | |
| 391 | func (branch TreeBranch) walkBranch(in TreeBranch, path []string, commentsStack [][]string, onLeaves func(in interface{}, path []string, commentsStack [][]string) (interface{}, error)) (TreeBranch, error) { |
| 392 | // Because append returns a new slice, the original stack is not changed. |
| 393 | commentsStack = append(commentsStack, []string{}) |
| 394 | for i, item := range in { |
| 395 | if c, ok := item.Key.(Comment); ok { |
| 396 | // If key is a comment, we add it to the slice of active comments. |
| 397 | // This allows us to also encrypt comments themselves by enabling encryption in a prior comment. |
| 398 | commentsStack[len(commentsStack)-1] = append(commentsStack[len(commentsStack)-1], c.Value) |
| 399 | enc, err := branch.walkValue(item.Key, path, commentsStack, onLeaves) |
| 400 | if err != nil { |
| 401 | return nil, err |
| 402 | } |
| 403 | if encComment, ok := enc.(Comment); ok { |
| 404 | in[i].Key = encComment |
| 405 | continue |
| 406 | } else if comment, ok := enc.(string); ok { |
| 407 | in[i].Key = Comment{Value: comment, Inline: c.Inline} |
| 408 | continue |
| 409 | } else { |
| 410 | return nil, fmt.Errorf("walkValue of Comment should be either Comment or string, was %T", enc) |
| 411 | } |
| 412 | } |
| 413 | c, valueIsComment := item.Value.(Comment) |
| 414 | if valueIsComment { |
| 415 | // If value is a comment, we add it to the slice of active comments. |
| 416 | // This allows us to also encrypt comments themselves by enabling encryption in a prior comment. |
| 417 | commentsStack[len(commentsStack)-1] = append(commentsStack[len(commentsStack)-1], c.Value) |
| 418 | } |
| 419 | key, ok := item.Key.(string) |
| 420 | if !ok { |
| 421 | return nil, fmt.Errorf( |
| 422 | "Tree contains a non-string key (type %T): %s. Only string keys are supported", item.Key, item.Key) |
| 423 | } |
| 424 | newV, err := branch.walkValue(item.Value, append(path, key), commentsStack, onLeaves) |
| 425 | if err != nil { |
| 426 | return nil, err |
| 427 | } |
| 428 | in[i].Value = newV |
| 429 | if !valueIsComment { |
| 430 | // If value is not a comment, we clear the slice of active comments. |
| 431 | commentsStack[len(commentsStack)-1] = []string{} |
| 432 | } |
| 433 | } |
| 434 | return in, nil |
| 435 | } |
| 436 | |
| 437 | func (tree Tree) shouldBeEncrypted(path []string, commentsStack [][]string, isComment bool) bool { |
| 438 | encrypted := true |