Skip to content

Commit e7a5fc6

Browse files
authored
Merge pull request #39 from theappbusiness/feature/add-array-support
Adds support for parsing arrays
2 parents e0beb64 + 7dcaf1c commit e7a5fc6

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ enableFileSharing : Bool
2727
retryCount : Int
2828
adUnitPrefix : String
2929
analyticsKey : String
30+
arrayOfHashes: [String]
3031
environment : Environment
3132
```
3233

@@ -98,6 +99,7 @@ Please refer to the example project included in the repository for further guida
9899
* `Bool`: Expects Boolean type in plist
99100
* `Double`: Expects floating point type in plist
100101
* `URL`: Expects a string in the plist, which can be converted to a URL (validated at compile time)
102+
* `Array` : Expects an array of values in the plist
101103

102104
# Custom types
103105

@@ -129,3 +131,42 @@ You have to make the type in your plist a string, and input either a number -- e
129131
```
130132
let retryCount: Int? = nil
131133
```
134+
135+
# Arrays
136+
137+
Configen also supports reading arrays from the plist. For example, you can create an array of certificate hashes for pinning purposes and then in the mapping file, declare the configuration variable as something like `arrayOfHashes: [String]` and configen will automatically generate an array for you.
138+
139+
The downside of using arrays in plist is that the order of the array and whether there are any array elements at all are not guaranteed, so keep that in mind when using this functionality.
140+
141+
Example:
142+
143+
## Plist
144+
145+
```
146+
<?xml version="1.0" encoding="UTF-8"?>
147+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
148+
<plist version="1.0">
149+
<dict>
150+
<key>arrayOfHashes</key>
151+
<array>
152+
<string>9BV3692736V893B47V893BY4V94B8V6123984BV6983V6B093</string>
153+
<string>BVQ09PY89V86BY98VY9876BV9786B98687B6976BOP967BP96</string>
154+
<string>PB869869P6B76P9B7869P8B69P697P69769769P7B697PB89B</string>
155+
</array>
156+
</dict>
157+
</plist>
158+
```
159+
160+
## Mapping (.map)
161+
162+
`arrayOfHashes: [String]`
163+
164+
## Config file (generated)
165+
166+
```
167+
static let arrayOfHashes: [String] = [
168+
9BV3692736V893B47V893BY4V94B8V6123984BV6983V6B093,
169+
BVQ09PY89V86BY98VY9876BV9786B98687B6976BOP967BP96,
170+
PB869869P6B76P9B7869P8B69P697P69769769P7B697PB89B
171+
]
172+
```

configen/FileGenerator.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ struct FileGenerator {
116116
fatalError("Found URL without host: \(url) for setting: \(hint.variableName)")
117117
}
118118
line = template.urlImplementation
119+
case let str where str.match(regex: "^(?:\\[)\\w+(?:\\])$"):
120+
line = template.customImplementation
121+
line.replace(token: template.variableNameToken, withString: hint.variableName)
122+
line.replace(token: template.customTypeToken, withString: hint.type)
123+
line.replace(token: template.valueToken, withString: formatRawArrayString(rawValue: value, rawType: str))
124+
return line
119125

120126
default:
121127
guard value is String else {
@@ -131,6 +137,20 @@ struct FileGenerator {
131137
return line
132138
}
133139

140+
private func formatRawArrayString(rawValue: AnyObject, rawType: String) -> String {
141+
var rawTypeCopy = rawType
142+
var rawValueStr = "\(rawValue)"
143+
144+
rawTypeCopy = String(rawTypeCopy.dropFirst()) // drop [
145+
rawTypeCopy = String(rawTypeCopy.dropLast()) // drop ]
146+
147+
rawValueStr = String(rawValueStr.dropFirst()) // drop (
148+
rawValueStr = String(rawValueStr.dropLast()) // drop )
149+
150+
rawValueStr = rawValueStr.trimmingCharacters(in: .whitespacesAndNewlines)
151+
152+
return "[\(rawValueStr)]"
153+
}
134154
}
135155

136156
extension String {
@@ -141,4 +161,13 @@ extension String {
141161
var trimmed: String {
142162
return (self as NSString).trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
143163
}
164+
165+
func match(regex: String) -> Bool {
166+
guard let regex = try? NSRegularExpression(pattern: regex, options: .caseInsensitive) else {
167+
return false
168+
}
169+
170+
let matches = regex.matches(in: self, options: [], range: NSRange(location: 0, length: self.utf16.count))
171+
return !matches.isEmpty
172+
}
144173
}

0 commit comments

Comments
 (0)