Skip to content

Commit

Permalink
Compatibility for building all targets together in release mode + Use…
Browse files Browse the repository at this point in the history
… `package` instead of `public` (#164)

* use 'package' instead of 'public'

* move 'Fake' into 'PennyTests'

* remove 'package' attributes in 'Fake' folder

* remove import Fake s
  • Loading branch information
MahdiBM authored Jan 30, 2024
1 parent e873262 commit 4f9a257
Show file tree
Hide file tree
Showing 59 changed files with 331 additions and 374 deletions.
12 changes: 6 additions & 6 deletions Lambdas/AutoFaqs/S3AutoFaqsRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SotoS3
import Foundation
import Models

public struct S3AutoFaqsRepository {
package struct S3AutoFaqsRepository {

let s3: S3
let logger: Logger
Expand All @@ -12,12 +12,12 @@ public struct S3AutoFaqsRepository {
let decoder = JSONDecoder()
let encoder = JSONEncoder()

public init(awsClient: AWSClient, logger: Logger) {
package init(awsClient: AWSClient, logger: Logger) {
self.s3 = S3(client: awsClient, region: .euwest1)
self.logger = logger
}

public func insert(expression: String, value: String) async throws -> [String: String] {
package func insert(expression: String, value: String) async throws -> [String: String] {
var all = try await self.getAll()
if all[expression] != value {
all[expression] = value
Expand All @@ -26,15 +26,15 @@ public struct S3AutoFaqsRepository {
return all
}

public func remove(expression: String) async throws -> [String: String] {
package func remove(expression: String) async throws -> [String: String] {
var all = try await self.getAll()
if all.removeValue(forKey: expression) != nil {
try await self.save(items: all)
}
return all
}

public func getAll() async throws -> [String: String] {
package func getAll() async throws -> [String: String] {
let response: S3.GetObjectOutput

do {
Expand All @@ -61,7 +61,7 @@ public struct S3AutoFaqsRepository {
}
}

public func save(items: [String: String]) async throws {
package func save(items: [String: String]) async throws {
let data = try encoder.encode(items)
let putObjectRequest = S3.PutObjectRequest(
acl: .private,
Expand Down
12 changes: 6 additions & 6 deletions Lambdas/AutoPings/S3AutoPingsRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SotoS3
import Foundation
import Models

public struct S3AutoPingsRepository {
package struct S3AutoPingsRepository {

let s3: S3
let logger: Logger
Expand All @@ -12,12 +12,12 @@ public struct S3AutoPingsRepository {
let decoder = JSONDecoder()
let encoder = JSONEncoder()

public init(awsClient: AWSClient, logger: Logger) {
package init(awsClient: AWSClient, logger: Logger) {
self.s3 = S3(client: awsClient, region: .euwest1)
self.logger = logger
}

public func insert(
package func insert(
expressions: [S3AutoPingItems.Expression],
forDiscordID id: UserSnowflake
) async throws -> S3AutoPingItems {
Expand All @@ -29,7 +29,7 @@ public struct S3AutoPingsRepository {
return all
}

public func remove(
package func remove(
expressions: [S3AutoPingItems.Expression],
forDiscordID id: UserSnowflake
) async throws -> S3AutoPingItems {
Expand All @@ -44,7 +44,7 @@ public struct S3AutoPingsRepository {
return all
}

public func getAll() async throws -> S3AutoPingItems {
package func getAll() async throws -> S3AutoPingItems {
let response: S3.GetObjectOutput

do {
Expand All @@ -71,7 +71,7 @@ public struct S3AutoPingsRepository {
}
}

public func save(items: S3AutoPingItems) async throws {
package func save(items: S3AutoPingItems) async throws {
let data = try encoder.encode(items)
let putObjectRequest = S3.PutObjectRequest(
acl: .private,
Expand Down
12 changes: 6 additions & 6 deletions Lambdas/Faqs/S3FaqsRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SotoS3
import Foundation
import Models

public struct S3FaqsRepository {
package struct S3FaqsRepository {

let s3: S3
let logger: Logger
Expand All @@ -12,12 +12,12 @@ public struct S3FaqsRepository {
let decoder = JSONDecoder()
let encoder = JSONEncoder()

public init(awsClient: AWSClient, logger: Logger) {
package init(awsClient: AWSClient, logger: Logger) {
self.s3 = S3(client: awsClient, region: .euwest1)
self.logger = logger
}

public func insert(name: String, value: String) async throws -> [String: String] {
package func insert(name: String, value: String) async throws -> [String: String] {
var all = try await self.getAll()
if all[name] != value {
all[name] = value
Expand All @@ -26,15 +26,15 @@ public struct S3FaqsRepository {
return all
}

public func remove(name: String) async throws -> [String: String] {
package func remove(name: String) async throws -> [String: String] {
var all = try await self.getAll()
if all.removeValue(forKey: name) != nil {
try await self.save(items: all)
}
return all
}

public func getAll() async throws -> [String: String] {
package func getAll() async throws -> [String: String] {
let response: S3.GetObjectOutput

do {
Expand All @@ -61,7 +61,7 @@ public struct S3FaqsRepository {
}
}

public func save(items: [String: String]) async throws {
package func save(items: [String: String]) async throws {
let data = try encoder.encode(items)
let putObjectRequest = S3.PutObjectRequest(
acl: .private,
Expand Down
12 changes: 6 additions & 6 deletions Lambdas/GHOAuth/Models/GHOAuthPayload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ import JWTKit

/// This Payload will get sent along with the OAuth redirect to the GitHub OAuth page,
/// specifically inside the `state` query parameter.
public struct GHOAuthPayload: JWTPayload {
package struct GHOAuthPayload: JWTPayload {
/// Used to verify the user's identity when they come back from GitHub.
public let discordID: UserSnowflake
package let discordID: UserSnowflake
/// The interaction token to respond back to user on Discord with and notify of the result.
public let interactionToken: String
package let interactionToken: String
/// Expiration time of the token.
public let expiration: ExpirationClaim
package let expiration: ExpirationClaim

public init(discordID: UserSnowflake, interactionToken: String) {
package init(discordID: UserSnowflake, interactionToken: String) {
self.discordID = discordID
self.interactionToken = interactionToken
self.expiration = .init(value: Date().addingTimeInterval(10 * 60)) // 10 minutes
}

public func verify(using signer: JWTSigner) throws {
package func verify(using signer: JWTSigner) throws {
try self.expiration.verifyNotExpired()
}
}
14 changes: 7 additions & 7 deletions Lambdas/LambdasShared/+APIGatewayV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ private let jsonEncoder = JSONEncoder()

extension APIGatewayV2Request {

public func decode<D: Decodable>(as type: D.Type = D.self) throws -> D {
package func decode<D: Decodable>(as type: D.Type = D.self) throws -> D {
guard let body = self.body else {
throw APIGatewayErrors.emptyBody(self)
}
let data = Data(body.utf8)
return try jsonDecoder.decode(D.self, from: data)
}

public func decodeWithISO8601<D: Decodable>(as type: D.Type = D.self) throws -> D {
package func decodeWithISO8601<D: Decodable>(as type: D.Type = D.self) throws -> D {
guard let body = self.body else {
throw APIGatewayErrors.emptyBody(self)
}
Expand All @@ -30,7 +30,7 @@ extension APIGatewayV2Request {
}

extension APIGatewayV2Response {
public init(status: HTTPResponseStatus, content: some Encodable) {
package init(status: HTTPResponseStatus, content: some Encodable) {
do {
let data = try jsonEncoder.encode(content)
let string = String(data: data, encoding: .utf8)
Expand All @@ -46,18 +46,18 @@ extension APIGatewayV2Response {
}
}

public struct GatewayFailure: Encodable {
package struct GatewayFailure: Encodable {
var reason: String

public init(reason: String) {
package init(reason: String) {
self.reason = reason
}
}

public enum APIGatewayErrors: Error, CustomStringConvertible {
package enum APIGatewayErrors: Error, CustomStringConvertible {
case emptyBody(APIGatewayV2Request)

public var description: String {
package var description: String {
switch self {
case let .emptyBody(request):
return "emptyBody(\(request))"
Expand Down
6 changes: 3 additions & 3 deletions Lambdas/LambdasShared/SecretsRetriever.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Logging
import Shared
import Foundation

public actor SecretsRetriever {
package actor SecretsRetriever {

enum Errors: Error, CustomStringConvertible {
case secretNotFound(arn: String)
Expand All @@ -24,12 +24,12 @@ public actor SecretsRetriever {

private let queue = SerialProcessor()

public init(awsClient: AWSClient, logger: Logger) {
package init(awsClient: AWSClient, logger: Logger) {
self.secretsManager = SecretsManager(client: awsClient)
self.logger = logger
}

public func getSecret(arnEnvVarKey: String) async throws -> String {
package func getSecret(arnEnvVarKey: String) async throws -> String {
logger.trace("Get secret start", metadata: [
"arnEnvVarKey": .string(arnEnvVarKey)
])
Expand Down
6 changes: 3 additions & 3 deletions Lambdas/LambdasShared/Utilities.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Foundation

public struct NoEnvVarError: Error, CustomStringConvertible {
package struct NoEnvVarError: Error, CustomStringConvertible {
let key: String

public var description: String {
package var description: String {
"NoEnvVarError(key.debugDescription: \(key.debugDescription))"
}
}

public func requireEnvVar(_ key: String) throws -> String {
package func requireEnvVar(_ key: String) throws -> String {
if let value = ProcessInfo.processInfo.environment[key] {
return value
} else {
Expand Down
2 changes: 1 addition & 1 deletion Lambdas/Sponsors/GithubWebhookPayload.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public struct GitHubWebhookPayload: Codable {
package struct GitHubWebhookPayload: Codable {
let action: String
let sponsorship: Sponsorship
let sender: Sender
Expand Down
2 changes: 1 addition & 1 deletion Lambdas/Sponsors/SponsorType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ enum SponsorType: String {
}
}

public static func `for`(sponsorshipAmount: Int) throws -> SponsorType {
package static func `for`(sponsorshipAmount: Int) throws -> SponsorType {
switch sponsorshipAmount {
case 500...9900: return .backer
case 10000...: return .sponsor
Expand Down
14 changes: 7 additions & 7 deletions Lambdas/Users/InternalUsersService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import SotoDynamoDB
import Foundation
import Models

public struct InternalUsersService {
package struct InternalUsersService {
private let userRepo: DynamoUserRepository
private let coinEntryRepo: DynamoCoinEntryRepository
let logger: Logger

public init(awsClient: AWSClient, logger: Logger) {
package init(awsClient: AWSClient, logger: Logger) {
let euWest = Region(awsRegionName: "eu-west-1")
let dynamoDB = DynamoDB(client: awsClient, region: euWest)
self.userRepo = DynamoUserRepository(
Expand All @@ -22,7 +22,7 @@ public struct InternalUsersService {
}

/// `freshUser` must be a fresh user you just got from the db.
public func addCoinEntry(
package func addCoinEntry(
_ coinEntry: CoinEntry,
freshUser user: DynamoDBUser
) async throws -> DynamoDBUser {
Expand All @@ -40,7 +40,7 @@ public struct InternalUsersService {
try await userRepo.getUser(discordID: discordID)
}

public func getOrCreateUser(discordID: UserSnowflake) async throws -> DynamoDBUser {
package func getOrCreateUser(discordID: UserSnowflake) async throws -> DynamoDBUser {
if let existing = try await self.getUser(discordID: discordID) {
return existing
} else {
Expand All @@ -51,18 +51,18 @@ public struct InternalUsersService {
}

/// Returns nil if user does not exist.
public func getUser(githubID: String) async throws -> DynamoDBUser? {
package func getUser(githubID: String) async throws -> DynamoDBUser? {
try await userRepo.getUser(githubID: githubID)
}

public func linkGithubID(discordID: UserSnowflake, githubID: String) async throws {
package func linkGithubID(discordID: UserSnowflake, githubID: String) async throws {
var user = try await self.getOrCreateUser(discordID: discordID)
user.githubID = githubID

try await userRepo.updateUser(user)
}

public func unlinkGithubID(discordID: UserSnowflake) async throws {
package func unlinkGithubID(discordID: UserSnowflake) async throws {
var user = try await self.getOrCreateUser(discordID: discordID)
user.githubID = nil

Expand Down
27 changes: 0 additions & 27 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -258,32 +258,6 @@ let package = Package(
],
swiftSettings: targetsSwiftSettings
),
.target(
name: "Fake",
dependencies: [
.product(name: "SotoDynamoDB", package: "soto"),
.product(name: "SotoS3", package: "soto"),
.product(name: "SotoCore", package: "soto-core"),
.product(name: "DiscordBM", package: "DiscordBM"),
.product(name: "LeafKit", package: "leaf-kit"),
.product(name: "Markdown", package: "swift-markdown"),
.product(name: "SwiftSemver", package: "swift-semver"),
.product(name: "DiscordLogger", package: "DiscordLogger"),
.product(name: "JWTKit", package: "jwt-kit"),
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events"),
.product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"),
.target(name: "GitHubAPI"),
.target(name: "LambdasShared"),
.target(name: "Shared"),
.target(name: "Rendering"),
.target(name: "Models"),
.target(name: "Penny"),
.target(name: "GHHooksLambda"),
],
path: "./Tests/Fake",
swiftSettings: testsSwiftSettings
),
.testTarget(
name: "PennyTests",
dependencies: [
Expand All @@ -304,7 +278,6 @@ let package = Package(
.target(name: "Models"),
.target(name: "Penny"),
.target(name: "GHHooksLambda"),
.target(name: "Fake"),
],
swiftSettings: testsSwiftSettings
),
Expand Down
Loading

0 comments on commit 4f9a257

Please sign in to comment.