haveapi-go-client 0.27.3 → 0.28.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/haveapi-go-client.gemspec +1 -1
- data/lib/haveapi/go_client/action.rb +7 -3
- data/lib/haveapi/go_client/erb_template.rb +3 -0
- data/lib/haveapi/go_client/generator.rb +5 -2
- data/lib/haveapi/go_client/resource.rb +7 -2
- data/lib/haveapi/go_client/utils.rb +101 -1
- data/lib/haveapi/go_client/version.rb +1 -1
- data/spec/integration/generator_spec.rb +654 -2
- data/template/action.go.erb +74 -73
- data/template/authentication/oauth2.go.erb +20 -8
- data/template/authentication/token.go.erb +10 -10
- data/template/client.go.erb +41 -2
- data/template/request.go.erb +106 -8
- data/template/resource.go.erb +3 -3
- metadata +3 -4
- data/shell.nix +0 -23
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2311cffaf323ff4da73072897c6b07b9d47645d0768a5a897995e7380ae8dd51
|
|
4
|
+
data.tar.gz: 9a51f18818d70dbf88c0b6b5501b88b17bb14f86ff34e0110f25c948823830d6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f821f3309a480081de9b71f9fadc8d82793271deae5f4de6b1e6f2de114a9137c69f33f5daa2763772120a10f8ea1e07f64657599785f98b20d3767671a42e59
|
|
7
|
+
data.tar.gz: ad1a5e7a656d0d2bdf0f79497a2ca53873e8679027fc659d2e59fb22364d2c74eecde36a7f5cea7dddc1da09a87461c24846380ce882ea50054dcf9e5db2e0e0
|
data/haveapi-go-client.gemspec
CHANGED
|
@@ -15,6 +15,10 @@ module HaveAPI::GoClient
|
|
|
15
15
|
# @return [Array<String>]
|
|
16
16
|
attr_reader :aliases
|
|
17
17
|
|
|
18
|
+
# Safe name for generated filenames
|
|
19
|
+
# @return [String]
|
|
20
|
+
attr_reader :file_name
|
|
21
|
+
|
|
18
22
|
# Full action name, including resource
|
|
19
23
|
# @return [String]
|
|
20
24
|
attr_reader :full_dot_name
|
|
@@ -58,6 +62,7 @@ module HaveAPI::GoClient
|
|
|
58
62
|
@resource = resource
|
|
59
63
|
@name = name.to_s
|
|
60
64
|
@prefix = prefix
|
|
65
|
+
@file_name = safe_file_component(@name)
|
|
61
66
|
@aliases = desc[:aliases]
|
|
62
67
|
@full_dot_name = "#{resource.full_dot_name}##{@name.capitalize}"
|
|
63
68
|
@go_name = camelize(name)
|
|
@@ -75,9 +80,8 @@ module HaveAPI::GoClient
|
|
|
75
80
|
|
|
76
81
|
# Return action name with all aliases, camelized
|
|
77
82
|
# @return [Array<String>]
|
|
78
|
-
def all_names
|
|
79
|
-
|
|
80
|
-
aliases.each { |v| yield(camelize(v)) }
|
|
83
|
+
def all_names(&)
|
|
84
|
+
([go_name] + aliases.map { |v| camelize(v) }).uniq.each(&)
|
|
81
85
|
end
|
|
82
86
|
|
|
83
87
|
# @return [Boolean]
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
require 'fileutils'
|
|
2
2
|
require 'haveapi/client'
|
|
3
|
+
require 'haveapi/go_client/utils'
|
|
3
4
|
|
|
4
5
|
module HaveAPI::GoClient
|
|
5
6
|
class Generator
|
|
7
|
+
include Utils
|
|
8
|
+
|
|
6
9
|
# Destination directory
|
|
7
10
|
# @return [String]
|
|
8
11
|
attr_reader :dst
|
|
@@ -23,8 +26,8 @@ module HaveAPI::GoClient
|
|
|
23
26
|
# @option opts [String] :package
|
|
24
27
|
def initialize(url, dst, opts)
|
|
25
28
|
@dst = dst
|
|
26
|
-
@module = opts[:module]
|
|
27
|
-
@package = opts[:package]
|
|
29
|
+
@module = opts[:module] && go_module_path(opts[:module])
|
|
30
|
+
@package = go_package_name(opts[:package])
|
|
28
31
|
|
|
29
32
|
conn = HaveAPI::Client::Communicator.new(url)
|
|
30
33
|
if opts[:basic_user] && opts[:basic_password]
|
|
@@ -16,6 +16,10 @@ module HaveAPI::GoClient
|
|
|
16
16
|
# @return [String]
|
|
17
17
|
attr_reader :full_name
|
|
18
18
|
|
|
19
|
+
# Safe full name for generated filenames
|
|
20
|
+
# @return [String]
|
|
21
|
+
attr_reader :file_name
|
|
22
|
+
|
|
19
23
|
# Full name with dots
|
|
20
24
|
# @return [String]
|
|
21
25
|
attr_reader :full_dot_name
|
|
@@ -41,6 +45,7 @@ module HaveAPI::GoClient
|
|
|
41
45
|
@name = name.to_s
|
|
42
46
|
@prefix = prefix
|
|
43
47
|
@full_name = resource_path.map(&:name).join('_')
|
|
48
|
+
@file_name = resource_path.map { |r| safe_file_component(r.name) }.join('_')
|
|
44
49
|
@full_dot_name = resource_path.map(&:name).map(&:capitalize).join('.')
|
|
45
50
|
@go_name = camelize(name)
|
|
46
51
|
@go_type = full_go_type
|
|
@@ -89,7 +94,7 @@ module HaveAPI::GoClient
|
|
|
89
94
|
package: gen.package,
|
|
90
95
|
resource: self
|
|
91
96
|
},
|
|
92
|
-
File.join(gen.dst, prefix_underscore("resource_#{
|
|
97
|
+
File.join(gen.dst, prefix_underscore("resource_#{file_name}.go"))
|
|
93
98
|
)
|
|
94
99
|
|
|
95
100
|
resources.each { |r| r.generate(gen) }
|
|
@@ -101,7 +106,7 @@ module HaveAPI::GoClient
|
|
|
101
106
|
package: gen.package,
|
|
102
107
|
action: a
|
|
103
108
|
},
|
|
104
|
-
File.join(gen.dst, prefix_underscore("resource_#{
|
|
109
|
+
File.join(gen.dst, prefix_underscore("resource_#{file_name}_action_#{a.file_name}.go"))
|
|
105
110
|
)
|
|
106
111
|
end
|
|
107
112
|
end
|
|
@@ -1,10 +1,110 @@
|
|
|
1
|
+
require 'digest/sha1'
|
|
2
|
+
require 'json'
|
|
3
|
+
|
|
1
4
|
module HaveAPI::GoClient
|
|
2
5
|
module Utils
|
|
6
|
+
GO_KEYWORDS = %w[
|
|
7
|
+
break case chan const continue default defer else fallthrough for func go
|
|
8
|
+
goto if import interface map package range return select struct switch type
|
|
9
|
+
var
|
|
10
|
+
].freeze
|
|
11
|
+
|
|
3
12
|
# Remove underscores and capitalize names
|
|
4
13
|
# @param v [String]
|
|
5
14
|
# @return [String]
|
|
6
15
|
def camelize(v)
|
|
7
|
-
v.to_s
|
|
16
|
+
raw = v.to_s
|
|
17
|
+
current = raw.split('_').map(&:capitalize).join
|
|
18
|
+
sanitized = raw.gsub(/[^A-Za-z0-9_]/, '_')
|
|
19
|
+
candidate = sanitized.split('_').reject(&:empty?).map(&:capitalize).join
|
|
20
|
+
candidate = 'X' if candidate.empty?
|
|
21
|
+
candidate = "X#{candidate}" unless go_identifier?(candidate)
|
|
22
|
+
|
|
23
|
+
if candidate != current || go_keyword?(candidate)
|
|
24
|
+
"#{candidate}_#{identifier_hash(raw)}"
|
|
25
|
+
else
|
|
26
|
+
candidate
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# @param v [String]
|
|
31
|
+
# @return [String]
|
|
32
|
+
def safe_file_component(v)
|
|
33
|
+
raw = v.to_s
|
|
34
|
+
candidate = raw.gsub(/[^A-Za-z0-9_-]/, '_')
|
|
35
|
+
candidate = 'x' if candidate.empty?
|
|
36
|
+
|
|
37
|
+
if candidate != raw || %w[. ..].include?(candidate)
|
|
38
|
+
"#{candidate}_#{identifier_hash(raw)}"
|
|
39
|
+
else
|
|
40
|
+
candidate
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# @param v [String]
|
|
45
|
+
# @return [String]
|
|
46
|
+
def go_package_name(v)
|
|
47
|
+
raw = v.to_s
|
|
48
|
+
candidate = raw.gsub(/[^A-Za-z0-9_]/, '_')
|
|
49
|
+
candidate = 'pkg' if candidate.empty? || candidate == '_'
|
|
50
|
+
candidate = "pkg_#{candidate}" unless go_identifier?(candidate)
|
|
51
|
+
|
|
52
|
+
if candidate != raw || go_keyword?(candidate)
|
|
53
|
+
"#{candidate}_#{identifier_hash(raw)}"
|
|
54
|
+
else
|
|
55
|
+
candidate
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# @param v [String]
|
|
60
|
+
# @return [String]
|
|
61
|
+
def go_module_path(v)
|
|
62
|
+
raw = v.to_s
|
|
63
|
+
|
|
64
|
+
if raw.empty? || raw.match?(/[\s[:cntrl:]]/)
|
|
65
|
+
raise ArgumentError, "invalid Go module path '#{raw}'"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
raw
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# @param v [String]
|
|
72
|
+
# @return [String]
|
|
73
|
+
def go_string_literal(v)
|
|
74
|
+
JSON.generate(v.to_s)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# @param v [String]
|
|
78
|
+
# @return [String]
|
|
79
|
+
def go_json_tag(v)
|
|
80
|
+
go_string_literal("json:#{JSON.generate(v.to_s)}")
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# @param namespace [String]
|
|
84
|
+
# @param name [String]
|
|
85
|
+
# @return [String]
|
|
86
|
+
def go_query_key(namespace, name)
|
|
87
|
+
go_string_literal("#{namespace}[#{name}]")
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# @param v [String]
|
|
91
|
+
# @return [String]
|
|
92
|
+
def go_comment_text(v)
|
|
93
|
+
v.to_s.gsub(/[\r\n\t]+/, ' ').gsub(/[[:cntrl:]]+/, ' ').strip
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
protected
|
|
97
|
+
|
|
98
|
+
def go_identifier?(v)
|
|
99
|
+
v.match?(/\A[A-Za-z_][A-Za-z0-9_]*\z/)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def go_keyword?(v)
|
|
103
|
+
GO_KEYWORDS.include?(v)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def identifier_hash(v)
|
|
107
|
+
Digest::SHA1.hexdigest(v.to_s)[0, 8]
|
|
8
108
|
end
|
|
9
109
|
end
|
|
10
110
|
end
|