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
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/haveapi/go_client/utils.rb', line 15 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
92 93 94 |
# File 'lib/haveapi/go_client/utils.rb', line 92 def go_comment_text(v) v.to_s.gsub(/[\r\n\t]+/, ' ').gsub(/[[:cntrl:]]+/, ' ').strip end |
#go_json_tag(v) ⇒ String
79 80 81 |
# File 'lib/haveapi/go_client/utils.rb', line 79 def go_json_tag(v) go_string_literal("json:#{JSON.generate(v.to_s)}") end |
#go_module_path(v) ⇒ String
61 62 63 64 65 66 67 68 69 |
# File 'lib/haveapi/go_client/utils.rb', line 61 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
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/haveapi/go_client/utils.rb', line 46 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
86 87 88 |
# File 'lib/haveapi/go_client/utils.rb', line 86 def go_query_key(namespace, name) go_string_literal("#{namespace}[#{name}]") end |
#go_string_literal(v) ⇒ String
73 74 75 |
# File 'lib/haveapi/go_client/utils.rb', line 73 def go_string_literal(v) JSON.generate(v.to_s) end |
#safe_file_component(v) ⇒ String
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/haveapi/go_client/utils.rb', line 32 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 |