Skip to content

Convert needlessly public methods and properties to internal. #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 108 additions & 9 deletions codegen/lib/graphql_swift_gen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,27 @@ def reformat(code)
Reformatter.new(indent: "\t").reformat(code)
end

def swift_input_type(type, non_null: false)
def swift_input_type(type, non_null: false, wrapped: false)
code = case type.kind
when 'NON_NULL'
return swift_input_type(type.of_type, non_null: true)
return swift_input_type(type.of_type, non_null: true, wrapped: wrapped)
when 'SCALAR'
scalars[type.name].swift_type
when 'LIST'
"[#{swift_input_type(type.of_type, non_null: true)}]"
"[#{swift_input_type(type.of_type, non_null: true, wrapped: wrapped)}]"
when 'INPUT_OBJECT', 'ENUM'
type.name
else
raise NotImplementedError, "Unhandled #{type.kind} input type"
end
code += "?" unless non_null

if wrapped
if !non_null
code = "InputValue<#{code}>"
end
else
code += "?" unless non_null
end
code
end

Expand Down Expand Up @@ -218,9 +225,45 @@ def deserialize_value_code(field_name, expr, type, untyped: true)
raise NotImplementedError, "Unexpected #{type.kind} argument type"
end
end


def generate_input_init(type)
text = "public init("
input_fields = type.required_input_fields + type.optional_input_fields
input_fields.each do |field|
text << escape_reserved_word(field.camelize_name)
text << ": "
text << swift_input_type(field.type, wrapped: true)
text << (field.type.non_null? ? "" : " = .undefined")
text << (field == input_fields.last ? "" : ", ")
end
text << ")"
text << " {\n"
type.input_fields.each do |field|
name = escape_reserved_word(field.camelize_name)
text << "self." + name + " = " + name
text << "\n"
end
text << "}"
end

def remove_linebreaks(text)
text.gsub("\n", " ")
end

def input_field_description(type)
unless type.input_fields.count == 0
text = "/// - parameters:" + ""
type.input_fields.each do |field|
description = (field.description.nil? ? "No description" : remove_linebreaks(field.description))
text << "\n/// - " + field.name + ": " + description
end
text << "\n///"
text
end
end

def swift_arg_defs(field)
defs = ["aliasSuffix: String? = nil"]
defs = ["alias: String? = nil"]
field.args.each do |arg|
arg_def = "#{escape_reserved_word(arg.name)}: #{swift_input_type(arg.type)}"
arg_def << " = nil" unless arg.type.non_null?
Expand All @@ -242,16 +285,72 @@ def generate_append_objects_code(expr, type, non_null: false)
end
return "#{expr}.forEach {\n#{generate_append_objects_code('$0', type.of_type)}\n}" if type.list?

abstract_response = type.object? ? expr : "#{expr} as! GraphQL.AbstractResponse"
abstract_response = type.object? ? expr : "(#{expr} as! GraphQL.AbstractResponse)"
"response.append(#{abstract_response})\n" \
"response.append(contentsOf: #{expr}.childResponseObjectMap())"
"response.append(contentsOf: #{abstract_response}.childResponseObjectMap())"
end

def swift_attributes(deprecatable)
return unless deprecatable.deprecated?
if deprecatable.deprecation_reason
message_argument = ", message:#{deprecatable.deprecation_reason.inspect}"
end
"@available(*, deprecated#{message_argument})"
"@available(*, deprecated#{message_argument})\n"
end

def swift_doc(element, include_args=true)
doc = ''

unless element.description.nil?
description = element.description
description = wrap_text(description, '/// ')
doc << "\n\n" + description
end

if include_args && element.respond_to?(:args)
if element.args.count > 0
doc << "\n///\n"
doc << "/// - parameters:"
element.args.each do |arg|
doc << "\n"
doc << '/// - ' + arg.name + ': ' + (arg.description.nil? ? "No description" : format_arg_list(arg.description, 7))
end
doc << "\n///"
end
end
doc
end

def wrap_text(text, prefix, width=80)
container = ''
line = "" + prefix

parts = text.split(" ")
parts.each do |part|
if line.length + part.length < width
line << part
line << ' '
else
container << line
container << "\n"
line = "" + prefix
line << part
line << ' '
end
end

if line.length > 0
container << line
end
container
end

def format_arg_list(text, spacing)
parts = text.split("\n")
commented = parts.drop(1).map do |part|
"/// " + (" " * spacing) + part
end
commented.unshift(parts.first)
commented.join("\n")
end
end
26 changes: 25 additions & 1 deletion codegen/lib/graphql_swift_gen/templates/ApiSchema.swift.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
// Generated from <%= script_name %>
//
// <%= schema_name %>.swift
// Buy
//
// Created by Shopify.
// Copyright (c) 2017 Shopify Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the inline license necessary? Why not just license the whole repo?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a license for the generated code, which will end up being in another repo. I'm not sure if the license in the generated code is necessary though.

//

open class <%= schema_name %> {
<% [['Query', schema.query_root_name], ['Mutation', schema.mutation_root_name]].each do |operation_type, root_name| %>
Expand Down
Loading