Skip to content

Commit

Permalink
🔀 Merge pull request #3 from Lucien/Lucien-patch-2
Browse files Browse the repository at this point in the history
🤖 Add swiftlint workflow
  • Loading branch information
Lucien authored Jul 8, 2024
2 parents add009b + 478157d commit f47c30b
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 53 deletions.
10 changes: 2 additions & 8 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
matrix:
include:
- language: swift
build-mode: autobuild
build-mode: manual
# CodeQL supports the following values keywords for 'language': 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'swift'
# Use `c-cpp` to analyze code written in C, C++ or both
# Use 'java-kotlin' to analyze code written in Java, Kotlin or both
Expand Down Expand Up @@ -79,13 +79,7 @@ jobs:
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
- if: matrix.build-mode == 'manual'
shell: bash
run: |
echo 'If you are using a "manual" build mode for one or more of the' \
'languages you are analyzing, replace this with the commands to build' \
'your code, for example:'
echo ' make bootstrap'
echo ' make release'
exit 1
run: swift build -v

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
Expand Down
16 changes: 16 additions & 0 deletions .github/workflows/swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: SwiftLint

on:
pull_request:
paths:
- '.github/workflows/swiftlint.yml'
- '.swiftlint.yml'
- '**/*.swift'

jobs:
SwiftLint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: GitHub Action for SwiftLint
uses: norio-nomura/[email protected]
7 changes: 3 additions & 4 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ opt_in_rules: # some rules are only opt-in
- conditional_returns_on_newline
- empty_count
- explicit_init
- explicit_type_interface
# - explicit_type_interface
- fatal_error_message
- file_header
- first_where
Expand All @@ -83,9 +83,9 @@ opt_in_rules: # some rules are only opt-in
- sorted_imports
- switch_case_on_newline
included: # paths to include during linting. `--path` is ignored if present.
- NadaADeclarar
- Sources
excluded: # paths to ignore during linting. Takes precedence over `included`.
- NadaADeclararTests
- Sources/NadaADeclararTests

# configurable rules can be customized from this configuration file
# binary rules can set their severity level
Expand All @@ -95,7 +95,6 @@ closure_spacing: error
conditional_returns_on_newline: error
empty_count: error
explicit_init: error
explicit_type_interface: error
force_cast: error

attributes:
Expand Down
86 changes: 86 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"pins" : [
{
"identity" : "collectionconcurrencykit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/JohnSundell/CollectionConcurrencyKit.git",
"state" : {
"revision" : "b4f23e24b5a1bff301efc5e70871083ca029ff95",
"version" : "0.2.0"
}
},
{
"identity" : "cryptoswift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/krzyzanowskim/CryptoSwift.git",
"state" : {
"revision" : "c9c3df6ab812de32bae61fc0cd1bf6d45170ebf0",
"version" : "1.8.2"
}
},
{
"identity" : "sourcekitten",
"kind" : "remoteSourceControl",
"location" : "https://github.com/jpsim/SourceKitten.git",
"state" : {
"revision" : "fd4df99170f5e9d7cf9aa8312aa8506e0e7a44e7",
"version" : "0.35.0"
}
},
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser.git",
"state" : {
"revision" : "0fbc8848e389af3bb55c182bc19ca9d5dc2f255b",
"version" : "1.4.0"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "303e5c5c36d6a558407d364878df131c3546fad8",
"version" : "510.0.2"
}
},
{
"identity" : "swiftlint",
"kind" : "remoteSourceControl",
"location" : "https://github.com/realm/SwiftLint.git",
"state" : {
"revision" : "b515723b16eba33f15c4677ee65f3fef2ce8c255",
"version" : "0.55.1"
}
},
{
"identity" : "swiftytexttable",
"kind" : "remoteSourceControl",
"location" : "https://github.com/scottrhoyt/SwiftyTextTable.git",
"state" : {
"revision" : "c6df6cf533d120716bff38f8ff9885e1ce2a4ac3",
"version" : "0.9.0"
}
},
{
"identity" : "swxmlhash",
"kind" : "remoteSourceControl",
"location" : "https://github.com/drmohundro/SWXMLHash.git",
"state" : {
"revision" : "a853604c9e9a83ad9954c7e3d2a565273982471f",
"version" : "7.0.2"
}
},
{
"identity" : "yams",
"kind" : "remoteSourceControl",
"location" : "https://github.com/jpsim/Yams.git",
"state" : {
"revision" : "9234124cff5e22e178988c18d8b95a8ae8007f76",
"version" : "5.1.2"
}
}
],
"version" : 2
}
13 changes: 11 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,29 @@ import PackageDescription

let package = Package(
name: "NadaADeclarar",
platforms: [
.macOS(.v12) // Update this line
],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "NadaADeclarar",
targets: ["NadaADeclarar"]),
],
dependencies: [
.package(url: "https://github.com/realm/SwiftLint.git", from: .init(0, 55, 0))
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.target(
name: "NadaADeclarar"),
name: "NadaADeclarar",
plugins: [
.plugin(name: "SwiftLintBuildToolPlugin", package: "SwiftLint")
]),
.testTarget(
name: "NadaADeclararTests",
dependencies: ["NadaADeclarar"],
resources: [.process("Resources")])
resources: [.process("Resources")]),
]
)
4 changes: 2 additions & 2 deletions Sources/NadaADeclarar/CNPJ.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public struct CNPJ: NadaADeclararNumberProtocol, Generatable, NumberParsedInfoIn
- Returns: `true` if the CNPJ represents a headquarters; otherwise, `false`.
*/
public var isHeadquarters: Bool {
return branchNumber == "0001"
branchNumber == "0001"
}

/**
Expand All @@ -55,7 +55,7 @@ public struct CNPJ: NadaADeclararNumberProtocol, Generatable, NumberParsedInfoIn
- Returns: A string representing the branch number.
*/
public var branchNumber: String {
return numberParsedInfo.parts.dropLast().last! // swiftlint:disable:this force_unwrapping
numberParsedInfo.parts.dropLast().last! // swiftlint:disable:this force_unwrapping
}

/**
Expand Down
9 changes: 5 additions & 4 deletions Sources/NadaADeclarar/CPF.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ public struct CPF: NadaADeclararNumberProtocol, Generatable, NumberParsedInfoInt
- allSameDigitsAreValid: A flag indicating whether a CPF with all same digits is valid. Defaults to `false`.
- Returns: `true` if the CPF is valid, `false` otherwise.
*/
func isValid(validationAlgorythm: Validator.ValidationAlgorythm = .simple,
allSameDigitsAreValid: Bool = false) -> Bool {
return validator.isValid(validationAlgorythm: validationAlgorythm,
allSameDigitsAreValid: allSameDigitsAreValid)
func isValid(
validationAlgorythm: Validator.ValidationAlgorythm = .simple,
allSameDigitsAreValid: Bool = false
) -> Bool {
validator.isValid(validationAlgorythm: validationAlgorythm, allSameDigitsAreValid: allSameDigitsAreValid)
}
}
10 changes: 4 additions & 6 deletions Sources/NadaADeclarar/FiscalRegions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,15 @@ extension State: Hashable {
}

public static func == (lhs: State, rhs: State) -> Bool {
return (
lhs.initials == rhs.initials &&
lhs.name == rhs.name &&
lhs.fiscalRegion == rhs.fiscalRegion
)
lhs.initials == rhs.initials &&
lhs.name == rhs.name &&
lhs.fiscalRegion == rhs.fiscalRegion
}
}

extension State: CustomStringConvertible {
public var description: String {
return "State: \(name) (\(initials)) Fiscal Region: \(fiscalRegion)"
"State: \(name) (\(initials)) Fiscal Region: \(fiscalRegion)"
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/NadaADeclarar/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ extension Generatable {
let basicNumberLength = NumberParsedType.numberLength - NumberParsedType.checkDigitsCount
var randomNumber = ""
for _ in 0 ..< basicNumberLength {
randomNumber += String(arc4random() % 10)
randomNumber += String(Int.random(in: 0...9))
}
return randomNumber
}
Expand Down
16 changes: 8 additions & 8 deletions Sources/NadaADeclarar/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ public struct Parser {
}

var offset = 0
for i in 0..<separators.count {
offset += steps[i] + (i == 0 ? 0 : 1)
let separator = separators[i]
for index in 0..<separators.count {
offset += steps[index] + (index == 0 ? 0 : 1)
let separator = separators[index]

let location = number.index(number.startIndex, offsetBy: offset)
number.insert(separator, at: location)
Expand Down Expand Up @@ -182,15 +182,15 @@ extension Parser.Info: Hashable {
hasher.combine(plainNumber)
hasher.combine(maskedNumber)
let checkDigitsHashValue = checkDigits.reduce(5_381) {
return ($0 << 5) &+ $0 &+ Int($1)
($0 << 5) &+ $0 &+ Int($1)
}
hasher.combine(checkDigitsHashValue)
}

static func == (lhs: Parser.Info, rhs: Parser.Info) -> Bool {
return (lhs.plainNumber == rhs.plainNumber &&
lhs.maskedNumber == rhs.maskedNumber &&
lhs.checkDigits == rhs.checkDigits &&
lhs.parts == rhs.parts)
lhs.plainNumber == rhs.plainNumber &&
lhs.maskedNumber == rhs.maskedNumber &&
lhs.checkDigits == rhs.checkDigits &&
lhs.parts == rhs.parts
}
}
8 changes: 4 additions & 4 deletions Sources/NadaADeclarar/Protocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ protocol NumberParsedInfoInterface: Hashable {

extension NumberParsedInfoInterface {
public var plainNumber: String {
return numberParsedInfo.plainNumber
numberParsedInfo.plainNumber
}

public var maskedNumber: String {
return numberParsedInfo.maskedNumber
numberParsedInfo.maskedNumber
}

public var checkDigits: [Int] {
return numberParsedInfo.checkDigits
numberParsedInfo.checkDigits
}

public func hash(into hasher: inout Hasher) {
hasher.combine(numberParsedInfo)
}

public static func == (lhs: Self, rhs: Self) -> Bool {
return lhs.numberParsedInfo == rhs.numberParsedInfo
lhs.numberParsedInfo == rhs.numberParsedInfo
}
}
26 changes: 13 additions & 13 deletions Sources/NadaADeclarar/Validator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public struct Validator {
let range = Range(uncheckedBounds: (plainNumber.startIndex, upper: upperBound))
let basicNumberLength = plainNumber.count - checkDigitsCount

var v1 = 0
var checkDigit1 = 0
let moduloNumber = 11
var v2 = 0
var checkDigit2 = 0
var index = 0

plainNumber.enumerateSubstrings(in: range,
Expand All @@ -60,17 +60,17 @@ public struct Validator {

if let enumeratedString = enumeratedString {
let number = Int(enumeratedString)! // swiftlint:disable:this force_unwrapping
v1 += number * (basicNumberLength - index)
v2 += number * (basicNumberLength - (index + 1))
checkDigit1 += number * (basicNumberLength - index)
checkDigit2 += number * (basicNumberLength - (index + 1))
index += 1
}
}

v1 = v1 % moduloNumber % (moduloNumber - 1)
v2 += v1 * basicNumberLength
v2 = v2 % moduloNumber % (moduloNumber - 1)
checkDigit1 = checkDigit1 % moduloNumber % (moduloNumber - 1)
checkDigit2 += checkDigit1 * basicNumberLength
checkDigit2 = checkDigit2 % moduloNumber % (moduloNumber - 1)

return numberParsedInfo.checkDigits == [v1, v2]
return numberParsedInfo.checkDigits == [checkDigit1, checkDigit2]
}

/**
Expand Down Expand Up @@ -114,13 +114,13 @@ public struct Validator {
var expectedCheckDigits = [Int]()
var nextBasicNumber = basicNumber

for i in 0..<checkDigitsCount {
for index in 0..<checkDigitsCount {
let sum = calculateWeightsSum(nextBasicNumber)
let remainder = sum % moduloNumber
let v = (remainder < 2) ? 0 : moduloNumber - remainder
assert(v >= 0 && v < 10, "verification number out of range 0 <= v <= 9")
nextBasicNumber.append(String(v))
expectedCheckDigits.insert(v, at: i)
let verificationNumber = (remainder < 2) ? 0 : moduloNumber - remainder
assert(verificationNumber >= 0 && verificationNumber < 10, "verification number out of range 0 <= v <= 9")
nextBasicNumber.append(String(verificationNumber))
expectedCheckDigits.insert(verificationNumber, at: index)
}
return expectedCheckDigits
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/NadaADeclararTests/CPFTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import XCTest
class CPFTests: XCTestCase, ListImporter {

lazy var cpfGroup: Set<CPF>! = {
return CPFTests.generatedNumberList()
CPFTests.generatedNumberList()
}()

func testCPFSet() {
Expand Down

0 comments on commit f47c30b

Please sign in to comment.