Skip to content

Commit

Permalink
generate multiline doc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
banjun committed Apr 10, 2017
1 parent 21c5fcb commit 87f01c5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Examples/01. Simplest API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct TextBodyParameters: BodyParameters {

// MARK: - Transitions

///

struct GET__message: Request {
typealias Response = Responses
let baseURL: URL
Expand Down
20 changes: 10 additions & 10 deletions Examples/02. Resource and Actions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ struct TextBodyParameters: BodyParameters {
// MARK: - Transitions

/// Here we define an action using the `GET` [HTTP request method](http://www.w3schools.com/tags/ref_httpmethods.asp) for our resource `/message`.

As with every good action it should return a
[response](http://www.w3.org/TR/di-gloss/#def-http-response). A response always
bears a status code. Code 200 is great as it means all is green. Responding
with some data can be a great idea as well so let's add a plain text message to
our response.
///
/// As with every good action it should return a
/// [response](http://www.w3.org/TR/di-gloss/#def-http-response). A response always
/// bears a status code. Code 200 is great as it means all is green. Responding
/// with some data can be a great idea as well so let's add a plain text message to
/// our response.
struct GET__message: Request {
typealias Response = Responses
let baseURL: URL
Expand Down Expand Up @@ -99,10 +99,10 @@ struct GET__message: Request {
}

/// OK, let's add another action. This time to put new data to our resource
(essentially an update action). We will need to send something in a
[request](http://www.w3.org/TR/di-gloss/#def-http-request) and then send a
response back confirming the posting was a success (_HTTP Status Code 204 ~
Resource updated successfully, no content is returned_).
/// (essentially an update action). We will need to send something in a
/// [request](http://www.w3.org/TR/di-gloss/#def-http-request) and then send a
/// response back confirming the posting was a success (_HTTP Status Code 204 ~
/// Resource updated successfully, no content is returned_).
struct PUT__message: Request {
typealias Response = Responses
let baseURL: URL
Expand Down
21 changes: 14 additions & 7 deletions Sources/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ fileprivate extension String {
let cs = CharacterSet(charactersIn: " _/{?,}-")
return components(separatedBy: cs).joined(separator: "_")
}

func docCommentPrefixed() -> String {
return components(separatedBy: .newlines).map {"/// " + $0}.joined(separator: "\n")
}
}

extension APIBlueprintDataStructure: SwiftConvertible {
func swift(_ name: String? = nil) throws -> SwiftCode {
let localDSTemplate = Template(templateString: ["struct {{ name }} { {% for v in vars %}",
" /// {{ v.doc }}",
" {{ v.doc }}",
" var {{ v.name }}: {{ v.type }}{% endfor %}",
"}\n"].joined(separator: "\n"))
let globalDSTemplate = Template(templateString: ["extension {{ fqn }}: Decodable {",
Expand Down Expand Up @@ -90,7 +94,7 @@ extension APIBlueprintTransition: SwiftConvertible {
.flatMap {$0.transaction.responses}
}

let trTemplate = Template(templateString: ["/// {{ copy }}",
let trTemplate = Template(templateString: ["{{ copy }}",
"struct {{ name }}: Request {",
" typealias Response = {{ response }}",
" let baseURL: URL",
Expand All @@ -100,7 +104,7 @@ extension APIBlueprintTransition: SwiftConvertible {
" static let pathTemplate: URITemplate = \"{{ path }}\"",
" var pathVars: PathVars",
" struct PathVars {",
"{% endif %} /// {{ v.doc }}",
"{% endif %} {{ v.doc }}",
" var {{ v.name }}: {{ v.type }}",
"{% if forloop.last %} }",
"{% endif %}{% empty %}",
Expand All @@ -118,7 +122,7 @@ extension APIBlueprintTransition: SwiftConvertible {
" var headerFields: [String: String] {return headerVars.context as? [String: String] ?? [:]}",
" var headerVars: HeaderVars",
" struct HeaderVars {",
"{% for v in headerVars %} /// {{ v.doc }}",
"{% for v in headerVars %} {{ v.doc }}",
" var {{ v.name }}: {{ v.type }}",
"{% endfor %}",
" }",
Expand Down Expand Up @@ -228,7 +232,7 @@ extension APIBlueprintTransition: SwiftConvertible {
["key": k,
"name": k.lowercased().swiftIdentifierized(),
"type": "String",
"doc": v]
"doc": v.docCommentPrefixed()]
}
context["headerVars"] = headerVars
globalExtensionCode += try globalPathVarsTemplate.render([
Expand All @@ -255,7 +259,7 @@ extension APIBlueprintTransition: SwiftConvertible {
context["paramContentType"] = requestContentType
}
}
context["copy"] = copy?.text
context["copy"] = copy?.text.docCommentPrefixed()

return try (local: trTemplate.render(context), global: globalExtensionCode)
}
Expand All @@ -282,5 +286,8 @@ extension APIBlueprintMember {
}
return name + (required ? "" : "?")
}
var swiftDoc: String {return [meta?.description, content.displayValue.map {" ex. " + $0}].flatMap {$0}.joined(separator: " ")}
var swiftDoc: String {return [meta?.description, content.displayValue.map {" ex. " + $0}]
.flatMap {$0}
.joined(separator: " ")
.docCommentPrefixed()}
}

0 comments on commit 87f01c5

Please sign in to comment.