Class: HaveAPI::GoClient::Generator

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/haveapi/go_client/generator.rb

Constant Summary

Constants included from Utils

Utils::GO_KEYWORDS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#camelize, #go_comment_text, #go_json_tag, #go_module_path, #go_package_name, #go_query_key, #go_string_literal, #safe_file_component

Constructor Details

#initialize(url, dst, opts) ⇒ Generator

Returns a new instance of Generator.

Parameters:

  • url (String)

    API URL

  • dst (String)

    destination directory

  • opts (Hash)

Options Hash (opts):

  • :version (String)
  • :module (String)
  • :package (String)


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/haveapi/go_client/generator.rb', line 27

def initialize(url, dst, opts)
  @dst = dst
  @module = opts[:module] && go_module_path(opts[:module])
  @package = go_package_name(opts[:package])

  conn = HaveAPI::Client::Communicator.new(url)
  if opts[:basic_user] && opts[:basic_password]
    conn.authenticate(:basic, user: opts[:basic_user], password: opts[:basic_password])
  end
  @api = ApiVersion.new(conn.describe_api(opts[:version]))
end

Instance Attribute Details

#dstString (readonly)

Destination directory

Returns:

  • (String)


11
12
13
# File 'lib/haveapi/go_client/generator.rb', line 11

def dst
  @dst
end

#moduleString (readonly)

Go module name

Returns:

  • (String)


15
16
17
# File 'lib/haveapi/go_client/generator.rb', line 15

def module
  @module
end

#packageString (readonly)

Go package name

Returns:

  • (String)


19
20
21
# File 'lib/haveapi/go_client/generator.rb', line 19

def package
  @package
end

Instance Method Details

#generateObject



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
# File 'lib/haveapi/go_client/generator.rb', line 39

def generate
  FileUtils.mkpath(dst)

  if self.module
    ErbTemplate.render_to_if_changed(
      'go.mod',
      { mod: self.module },
      File.join(dst, 'go.mod')
    )

    @dst = File.join(dst, package)
    FileUtils.mkpath(dst)
  end

  %w[client authentication request response types].each do |v|
    ErbTemplate.render_to_if_changed(
      "#{v}.go",
      {
        package:,
        api:
      },
      File.join(dst, "#{v}.go")
    )
  end

  api.resources.each { |r| r.generate(self) }
  api.auth_methods.each { |v| v.generate(self) }
end

#go_fmtObject



68
69
70
71
72
# File 'lib/haveapi/go_client/generator.rb', line 68

def go_fmt
  return if system('go', 'fmt', chdir: dst)

  raise 'go fmt failed'
end