-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathimports.go.tmpl
112 lines (96 loc) · 2.93 KB
/
imports.go.tmpl
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
{{- define "imports" -}}
{{- $types := .Types -}}
{{- $opts := .Opts -}}
{{- /* Map of import paths. */ -}}
{{- $stdlibImports := dict -}}
{{- set $stdlibImports "context" "" -}}
{{- set $stdlibImports "errors" "" -}}
{{- set $stdlibImports "fmt" "" -}}
{{- set $stdlibImports "net/http" "" -}}
{{- if eq $opts.json "stdlib" -}}
{{- set $stdlibImports "encoding/json" "" -}}
{{- end -}}
{{- if $opts.errorStackTrace }}
{{- set $stdlibImports "runtime" "" -}}
{{- end -}}
{{- if $opts.client }}
{{- set $stdlibImports "bytes" "" -}}
{{- set $stdlibImports "io" "" -}}
{{- set $stdlibImports "net/url" "" -}}
{{- end -}}
{{- if $opts.server }}
{{- set $stdlibImports "strings" "" -}}
{{- set $stdlibImports "io" "" -}}
{{- if $opts.fixEmptyArrays }}
{{- set $stdlibImports "reflect" "" -}}
{{- end -}}
{{- end -}}
{{- if $opts.streaming -}}
{{- if $opts.server }}
{{- set $stdlibImports "sync" "" -}}
{{- set $stdlibImports "time" "" -}}
{{- end -}}
{{- if $opts.client }}
{{- set $stdlibImports "bufio" "" -}}
{{- end -}}
{{- end -}}
{{- /* Import "time" if there's at least one timestamp. */ -}}
{{ if $opts.types -}}
{{ if eq $opts.importTypesFrom "" -}}
{{- range $_, $type := $types -}}
{{- range $_, $field := $type.Fields -}}
{{- if $field.Type -}}
{{- if eq $field.Type.Expr "timestamp" -}}
{{- set $stdlibImports "time" "" -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
import (
{{- /* Print stdlib imports. */ -}}
{{- range $import, $rename := $stdlibImports }}
{{if ne $rename ""}}{{$rename}} {{end}}"{{$import}}"
{{- end -}}
{{- /* Print custom type imports. */ -}}
{{- $imports := dict -}}
{{- if eq $opts.json "stdlib" -}}
{{- /* Already imported in the stdlib section. */ -}}
{{- else if eq $opts.json "jsoniter" -}}
{{- set $imports "github.com/json-iterator/go" "jsoniter" -}}
{{- else -}}
{{- stderrPrintf "unsupported -json=%s" $opts.json -}}
{{- exit 1 -}}
{{- end -}}
{{- if ne $opts.importTypesFrom "" }}
{{- set $imports $opts.importTypesFrom "" -}}
{{- else -}}
{{- range $_, $type := $types -}}
{{- /* TODO: We might need to loop through method args too. */ -}}
{{- range $meta := $type.Meta -}}
{{- if exists $meta "go.type.import" -}}
{{- set $imports (get $meta "go.type.import") "" -}}
{{- end -}}
{{- end -}}
{{- if $opts.types }}
{{- range $_, $field := $type.Fields -}}
{{- range $meta := $field.Meta -}}
{{- if exists $meta "go.type.import" -}}
{{- set $imports (get $meta "go.type.import") "" -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end }}
{{- end -}}
{{- end }}
{{ range $import, $rename := $imports }}
{{- if not (exists $stdlibImports $import)}}
{{if ne $rename ""}}{{$rename}} {{end}}"{{$import}}"
{{- end }}
{{- end }}
)
{{- if eq $opts.json "jsoniter" }}
var json = jsoniter.ConfigCompatibleWithStandardLibrary
{{- end -}}
{{- end -}}