-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresolver.go
More file actions
78 lines (68 loc) · 1.69 KB
/
Copy pathresolver.go
File metadata and controls
78 lines (68 loc) · 1.69 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package bqin
import (
"fmt"
"net/url"
"strconv"
"strings"
"github.com/kayac/bqin/internal/logger"
)
type Resolver struct {
rules []*Rule
}
func NewResolver(rules []*Rule) *Resolver {
return &Resolver{
rules: rules,
}
}
func (r *Resolver) Resolve(urls []*url.URL) []*Job {
ret := make([]*Job, 0, len(urls))
for _, u := range urls {
logger.Debugf("check url :%s", u.String())
for _, rule := range r.rules {
ok, capture := rule.Match(u)
if !ok {
continue
}
logger.Debugf("match rule: %s", rule.String())
ret = append(ret, newJob(rule, u, capture))
}
}
return ret
}
type Job struct {
*TransportJob
*LoadingJob
}
func newJob(r *Rule, u *url.URL, capture []string) *Job {
temp := &url.URL{
Scheme: "gs",
Host: expandPlaceHolder(r.Option.TemporaryBucket, capture),
Path: u.Path,
}
dest := &LoadingDestination{
ProjectID: expandPlaceHolder(r.BigQuery.ProjectID, capture),
Dataset: expandPlaceHolder(r.BigQuery.Dataset, capture),
Table: expandPlaceHolder(r.BigQuery.Table, capture),
}
loadingJob := NewLoadingJob(dest, temp.String())
loadingJob.GCSRef.Compression = r.Option.getCompression()
loadingJob.GCSRef.AutoDetect = r.Option.getAutoDetect()
loadingJob.GCSRef.SourceFormat = r.Option.getSourceFormat()
return &Job{
TransportJob: &TransportJob{
Source: u,
Destination: temp,
},
LoadingJob: loadingJob,
}
}
func (job *Job) String() string {
return fmt.Sprintf(`%s, and %s`, job.TransportJob, job.LoadingJob)
}
// example: when capture []string{"hoge"}, table_$1 => table_hoge
func expandPlaceHolder(s string, capture []string) string {
for i, v := range capture {
s = strings.Replace(s, "$"+strconv.Itoa(i), v, -1)
}
return s
}