Skip to content

Commit

Permalink
Batch/Backup commands use SchemaFlag (#570)
Browse files Browse the repository at this point in the history
This will allow the programs to configure what tables are build with lax
schema. Defaults to strict.

Fixes #564
  • Loading branch information
bolsinga authored Dec 26, 2024
1 parent 74a7fa7 commit 3b93a94
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 29 deletions.
8 changes: 4 additions & 4 deletions Sources/iTunes/BackupCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ public struct BackupCommand: AsyncParsableCommand {
"Reduce Tracks to minimum required fields and music related only. Defaults to false, unless repairing."
) var reduce: Bool = false

/// Database schema constraints. Only applicable with --sql-code or --db.
@Flag(help: "Database schema constraints. Only applicable with --sql-code or --db.")
var schemaConstraints: SchemaConstraints = .strict
/// Lax database schema table constraints. Only applicable with --sql-code or --db.
@Flag(help: "Lax database schema table constraints. Only applicable with --sql-code or --db.")
var laxSchema: [SchemaFlag] = []

/// Optional Output Directory for output file.
@Option(
Expand Down Expand Up @@ -137,7 +137,7 @@ public struct BackupCommand: AsyncParsableCommand {

try await destination.context(outputFile: outputFile).emit(
tracks, branch: "main", tagPrefix: tagPrefix, version: Self.configuration.version,
schemaOptions: schemaConstraints.schemaOptions)
schemaOptions: laxSchema.schemaOptions)
}

public init() {} // This is public and empty to help the compiler.
Expand Down
8 changes: 4 additions & 4 deletions Sources/iTunes/Batch/BatchCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public struct BatchCommand: AsyncParsableCommand {
/// Batch type.
@Flag(help: "Batch type to build.") var batch: Batch = .sql

/// Database schema constraints.
@Flag(help: "Database schema constraints.")
var schemaConstraints: SchemaConstraints = .strict
/// Lax database schema table constraints.
@Flag(help: "Lax database schema table constraints")
var laxSchema: [SchemaFlag] = []

/// Git Directory to read and write data from.
@Option(
Expand Down Expand Up @@ -64,7 +64,7 @@ public struct BatchCommand: AsyncParsableCommand {
directory: gitDirectory, tagPrefix: tagPrefix, fileName: Self.fileName)
try await batch.build(
configuration, outputDirectory: outputDirectory,
schemaOptions: schemaConstraints.schemaOptions)
schemaOptions: laxSchema.schemaOptions)
}

public init() {} // This is public and empty to help the compiler.
Expand Down
17 changes: 0 additions & 17 deletions Sources/iTunes/SchemaConstraints+LaxSchemaOptions.swift

This file was deleted.

36 changes: 36 additions & 0 deletions Sources/iTunes/SchemaFlag.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// SchemaFlag.swift
// itunes_json
//
// Created by Greg Bolsinga on 12/25/24.
//

import ArgumentParser
import Foundation

enum SchemaFlag: String, EnumerableFlag {
case artists, albums, songs, plays
}

extension SchemaFlag {
var schemaOption: SchemaOptions {
switch self {
case .artists:
.laxArtist
case .albums:
.laxAlbum
case .songs:
.laxSong
case .plays:
.laxPlays
}
}
}

extension Collection where Element == SchemaFlag {
var schemaOptions: SchemaOptions {
self.reduce(into: SchemaOptions()) { partialResult, flag in
partialResult.insert(flag.schemaOption)
}
}
}
8 changes: 4 additions & 4 deletions Sources/iTunes/SchemaOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import Foundation
struct SchemaOptions: OptionSet {
let rawValue: UInt

private static let laxArtist = SchemaOptions(rawValue: 1 << 0)
private static let laxAlbum = SchemaOptions(rawValue: 1 << 1)
private static let laxSong = SchemaOptions(rawValue: 1 << 2)
private static let laxPlays = SchemaOptions(rawValue: 1 << 3)
static let laxArtist = SchemaOptions(rawValue: 1 << 0)
static let laxAlbum = SchemaOptions(rawValue: 1 << 1)
static let laxSong = SchemaOptions(rawValue: 1 << 2)
static let laxPlays = SchemaOptions(rawValue: 1 << 3)

static let strictSchema = SchemaOptions()
static let laxSchema: SchemaOptions = [
Expand Down

0 comments on commit 3b93a94

Please sign in to comment.