Skip to content

Commit

Permalink
Merge pull request #33 from contentful/hotfix/localizationContext-for…
Browse files Browse the repository at this point in the history
…-seed-db

Deserialize space and inject localization context to seed db
  • Loading branch information
loudmouth committed Sep 22, 2017
2 parents 76a988a + 90fc3c6 commit 34a031e
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .env
@@ -1 +1 @@
CONTENTFUL_PERSISTENCE_VERSION=0.6.1
CONTENTFUL_PERSISTENCE_VERSION=0.6.2
2 changes: 1 addition & 1 deletion .envrc
@@ -1 +1 @@
export CONTENTFUL_PERSISTENCE_VERSION=0.6.1
export CONTENTFUL_PERSISTENCE_VERSION=0.6.2
10 changes: 9 additions & 1 deletion CHANGELOG.md
Expand Up @@ -15,12 +15,20 @@ This project adheres to [Semantic Versioning](http://semver.org/) starting from

#### 0.x Releases

- `0.6.x` Releases - [0.6.0](#060) | [0.6.1](#061)
- `0.6.x` Releases - [0.6.0](#060) | [0.6.1](#061) | [0.6.2](#062)
- `0.5.x` Releases - [0.5.0](#050)
- `0.4.x` Releases - [0.4.0](#040)

---

## [`0.6.2`](https://github.com/contentful/contentful-persistence.swift/releases/tag/0.6.2)
Released on 2017-09-20

#### Fixed
- Bug that caused `SynchronizationManager` to fail seeding a CoreData database from bundled content because of lack of localization context.

---

## [`0.6.1`](https://github.com/contentful/contentful-persistence.swift/releases/tag/0.6.1)
Released on 2017-09-08

Expand Down
2 changes: 1 addition & 1 deletion Config.xcconfig
@@ -1 +1 @@
CONTENTFUL_PERSISTENCE_VERSION=0.6.1
CONTENTFUL_PERSISTENCE_VERSION=0.6.2
18 changes: 16 additions & 2 deletions Sources/ContentfulPersistence/SynchronizationManager+SeedDB.swift
Expand Up @@ -27,11 +27,25 @@ public extension SynchronizationManager {
var fileIndex = 0
let filePaths = bundle.paths(forResourcesOfType: "json", inDirectory: directory)

let spaceJSONFilePath = filePaths.filter({ URL(string: $0)?.deletingPathExtension().lastPathComponent == "space" }).first
let firstFilePath = filePaths.filter({ URL(string: $0)?.deletingPathExtension().lastPathComponent == String(fileIndex) }).first
guard let initialSyncJSONFilePath = firstFilePath else {

guard let initialSyncJSONFilePath = firstFilePath, let spaceFilePath = spaceJSONFilePath else {
throw DatabaseSeedingError.invalidFilePath
}

// Get the space first to give the client context.
guard let data = FileManager.default.contents(atPath: spaceFilePath) else {
let error = SDKError.invalidURL(string: spaceFilePath)
throw error
}
guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else {
let error = SDKError.unparseableJSON(data: data, errorMessage: "Foundation.JSONSerialization failed")
throw error
}

let map = Map(mappingType: .fromJSON, JSON: json)
let space = try Space(map: map)

var filePath: String? = initialSyncJSONFilePath
while let path = filePath {
Expand All @@ -45,7 +59,7 @@ public extension SynchronizationManager {
throw error
}

let map = Map(mappingType: .fromJSON, JSON: json)
let map = Map(mappingType: .fromJSON, JSON: json, context: space.localizationContext)
let syncSpace = try SyncSpace(map: map)
self.update(with: syncSpace)

Expand Down
@@ -0,0 +1,21 @@
{
"sys": {
"type": "Space",
"id": "smf0sqiu0c5s"
},
"name": "Complex-Sync-Test-Space",
"locales": [
{
"code": "en-US",
"default": true,
"name": "U.S. English",
"fallbackCode": null
},
{
"code": "es-MX",
"default": false,
"name": "Spanish (Mexico)",
"fallbackCode": "en-US"
}
]
}
21 changes: 21 additions & 0 deletions Tests/ContentfulPersistenceTests/PreseedJSONFiles/space.json
@@ -0,0 +1,21 @@
{
"sys": {
"type": "Space",
"id": "smf0sqiu0c5s"
},
"name": "Complex-Sync-Test-Space",
"locales": [
{
"code": "en-US",
"default": true,
"name": "U.S. English",
"fallbackCode": null
},
{
"code": "es-MX",
"default": false,
"name": "Spanish (Mexico)",
"fallbackCode": "en-US"
}
]
}

0 comments on commit 34a031e

Please sign in to comment.