merge.go 794 Bytes
Newer Older
songlinfeng's avatar
songlinfeng committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
# Copyright (c) 2024, HCUOpt CORPORATION.  All rights reserved.
**/

package lookup

import (
	"errors"
)

type first []Locator

// First returns a locator that returns the first non-empty match
func First(locators ...Locator) Locator {
	var f first
	for _, l := range locators {
		if l == nil {
			continue
		}
		f = append(f, l)
	}
	return f
}

// Locate returns the results for the first locator that returns a non-empty non-error result.
func (f first) Locate(pattern string) ([]string, error) {
	var allErrors []error
	for _, l := range f {
		if l == nil {
			continue
		}
		candidates, err := l.Locate(pattern)
		if err != nil {
			allErrors = append(allErrors, err)
			continue
		}
		if len(candidates) > 0 {
			return candidates, nil
		}
	}
	return nil, errors.Join(allErrors...)
}