Skip to content
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

Start moving generation over to Descriptors #497

Merged
merged 16 commits into from
May 1, 2017
Merged
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
37 changes: 37 additions & 0 deletions Sources/PluginLibrary/Array+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Sources/PluginLibrary/Array+Extensions.swift - Additions to Arrays
//
// Copyright (c) 2014 - 2017 Apple Inc. and the project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See LICENSE.txt for license information:
// https://github.com/apple/swift-protobuf/blob/master/LICENSE.txt
//
// -----------------------------------------------------------------------------

import Foundation

extension Array {

/// Like map, but calls the transform with the index and value.
///
/// NOTE: It would seem like doing:
/// return self.enumerated().map {
/// return try transform($0.index, $0.element)
/// }
/// would seem like a simple thing to avoid extension. However as of Xcode 8.3.2
/// (Swift 3.1), building/running 5000000 interation test (macOS) of the differences
/// are rather large -
/// Release build:
/// Using enumerated: 3.694987967
/// Using enumeratedMap: 0.961241992
/// Debug build:
/// Using enumerated: 20.038512905
/// Using enumeratedMap: 8.521299144
func enumeratedMap<T>(_ transform: (Int, Element) throws -> T) rethrows -> [T] {
var i: Int = -1
return try map {
i += 1
return try transform(i, $0)
}
}
}
107 changes: 107 additions & 0 deletions Sources/PluginLibrary/Descriptor+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Sources/PluginLibrary/Descriptor+Extensions.swift - Additions to Descriptor
//
// Copyright (c) 2014 - 2017 Apple Inc. and the project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See LICENSE.txt for license information:
// https://github.com/apple/swift-protobuf/blob/master/LICENSE.txt
//
// -----------------------------------------------------------------------------

import Foundation
import SwiftProtobuf

extension FileDescriptor: ProvidesSourceCodeLocation {
// True if this file should perserve unknown enums within the enum.
public var hasUnknownEnumPreservingSemantics: Bool {
return syntax == .proto3
}

public var sourceCodeInfoLocation: Google_Protobuf_SourceCodeInfo.Location? {
// google/protobuf's descriptor.cc says it should be an empty path.
return sourceCodeInfoLocation(path: IndexPath())
}
}

extension Descriptor: ProvidesLocationPath, ProvidesSourceCodeLocation {
public func getLocationPath(path: inout IndexPath) {
if let containingType = containingType {
containingType.getLocationPath(path: &path)
path.append(Google_Protobuf_DescriptorProto.FieldNumbers.nestedType)
} else {
path.append(Google_Protobuf_FileDescriptorProto.FieldNumbers.messageType)
}
path.append(index)
}
}

extension EnumDescriptor: ProvidesLocationPath, ProvidesSourceCodeLocation {
// True if this enum should perserve unknown enums within the enum.
public var hasUnknownEnumPreservingSemantics: Bool {
return file.hasUnknownEnumPreservingSemantics
}

public func getLocationPath(path: inout IndexPath) {
if let containingType = containingType {
containingType.getLocationPath(path: &path)
path.append(Google_Protobuf_DescriptorProto.FieldNumbers.enumType)
} else {
path.append(Google_Protobuf_FileDescriptorProto.FieldNumbers.enumType)
}
path.append(index)
}
}

extension EnumValueDescriptor: ProvidesLocationPath, ProvidesSourceCodeLocation {
public weak var file: FileDescriptor! { return enumType.file }

public func getLocationPath(path: inout IndexPath) {
enumType.getLocationPath(path: &path)
path.append(Google_Protobuf_EnumDescriptorProto.FieldNumbers.value)
path.append(index)
}
}

extension OneofDescriptor: ProvidesLocationPath, ProvidesSourceCodeLocation {
public weak var file: FileDescriptor! { return containingType.file }

public func getLocationPath(path: inout IndexPath) {
containingType.getLocationPath(path: &path)
path.append(Google_Protobuf_DescriptorProto.FieldNumbers.oneofDecl)
path.append(index)
}
}

extension FieldDescriptor: ProvidesLocationPath, ProvidesSourceCodeLocation {
public func getLocationPath(path: inout IndexPath) {
if isExtension {
if let extensionScope = extensionScope {
extensionScope.getLocationPath(path: &path)
path.append(Google_Protobuf_DescriptorProto.FieldNumbers.extension)
} else {
path.append(Google_Protobuf_FileDescriptorProto.FieldNumbers.extension)
}
} else {
containingType.getLocationPath(path: &path)
path.append(Google_Protobuf_DescriptorProto.FieldNumbers.field)
}
path.append(index)
}
}

extension ServiceDescriptor: ProvidesLocationPath, ProvidesSourceCodeLocation {
public func getLocationPath(path: inout IndexPath) {
path.append(Google_Protobuf_FileDescriptorProto.FieldNumbers.service)
path.append(index)
}
}

extension MethodDescriptor: ProvidesLocationPath, ProvidesSourceCodeLocation {
public weak var file: FileDescriptor! { return service.file }

public func getLocationPath(path: inout IndexPath) {
service.getLocationPath(path: &path)
path.append(Google_Protobuf_ServiceDescriptorProto.FieldNumbers.method)
path.append(index)
}
}
Loading