Module: HaveAPI::GoClient::Utils
- Included in:
- Action, ErbTemplate, Generator, InputOutput, Parameters::Association, Parameters::Base, Resource
- Defined in:
- lib/haveapi/go_client/utils.rb
Constant Summary collapse
- GO_KEYWORDS =
%w[ break case chan const continue default defer else fallthrough for func go goto if import interface map package range return select struct switch type var ].freeze
Instance Method Summary collapse
-
#camelize(v) ⇒ String
Remove underscores and capitalize names.
- #go_comment_text(v) ⇒ String
- #go_json_tag(v) ⇒ String
- #go_module_path(v) ⇒ String
- #go_package_name(v) ⇒ String
- #go_query_key(namespace, name) ⇒ String
- #go_string_literal(v) ⇒ String
- #safe_file_component(v) ⇒ String
Instance Method Details
#camelize(v) ⇒ String
Remove underscores and capitalize names
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/haveapi/go_client/utils.rb', line 57 def camelize(v) raw = v.to_s current = raw.split('_').map(&:capitalize).join sanitized = raw.gsub(/[^A-Za-z0-9_]/, '_') candidate = sanitized.split('_').reject(&:empty?).map(&:capitalize).join candidate = 'X' if candidate.empty? candidate = "X#{candidate}" unless go_identifier?(candidate) if candidate != current || go_keyword?(candidate) "#{candidate}_#{identifier_hash(raw)}" else candidate end end |
#go_comment_text(v) ⇒ String
134 135 136 |
# File 'lib/haveapi/go_client/utils.rb', line 134 def go_comment_text(v) v.to_s.gsub(/[\r\n\t]+/, ' ').gsub(/[[:cntrl:]]+/, ' ').strip end |
#go_json_tag(v) ⇒ String
121 122 123 |
# File 'lib/haveapi/go_client/utils.rb', line 121 def go_json_tag(v) go_string_literal("json:#{JSON.generate(v.to_s)}") end |
#go_module_path(v) ⇒ String
103 104 105 106 107 108 109 110 111 |
# File 'lib/haveapi/go_client/utils.rb', line 103 def go_module_path(v) raw = v.to_s if raw.empty? || raw.match?(/[\s[:cntrl:]]/) raise ArgumentError, "invalid Go module path '#{raw}'" end raw end |
#go_package_name(v) ⇒ String
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/haveapi/go_client/utils.rb', line 88 def go_package_name(v) raw = v.to_s candidate = raw.gsub(/[^A-Za-z0-9_]/, '_') candidate = 'pkg' if candidate.empty? || candidate == '_' candidate = "pkg_#{candidate}" unless go_identifier?(candidate) if candidate != raw || go_keyword?(candidate) "#{candidate}_#{identifier_hash(raw)}" else candidate end end |
#go_query_key(namespace, name) ⇒ String
128 129 130 |
# File 'lib/haveapi/go_client/utils.rb', line 128 def go_query_key(namespace, name) go_string_literal("#{namespace}[#{name}]") end |
#go_string_literal(v) ⇒ String
115 116 117 |
# File 'lib/haveapi/go_client/utils.rb', line 115 def go_string_literal(v) JSON.generate(v.to_s) end |
#safe_file_component(v) ⇒ String
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/haveapi/go_client/utils.rb', line 74 def safe_file_component(v) raw = v.to_s candidate = raw.gsub(/[^A-Za-z0-9_-]/, '_') candidate = 'x' if candidate.empty? if candidate != raw || %w[. ..].include?(candidate) "#{candidate}_#{identifier_hash(raw)}" else candidate end end |