Suggestion: `Value.fromDatatypeValue` should be allowed to throw

Open
#778 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
30/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
swift
Domain
database

Research direction

Start with the Value protocol's fromDatatypeValue and datatypeValue entry points, using the NSPredicate serialization example in the issue as the motivating case. Determine the compatibility impact of allowing both conversions to throw and identify all affected callers; done requires a decided API design that handles serialization failures without forced unwrapping.

Written by the indexing model from the issue text.

Description

enhancement

I am implementing my own serialization methods for a few objects, e.g. NSPredicate:

extension NSPredicate: Value {
	@nonobjc public static let declaredDatatype = Blob.declaredDatatype

	public static func fromDatatypeValue(_ datatypeValue: Blob) -> NSPredicate {
		return NSKeyedUnarchiver.unarchiveObject(with: Data.fromDatatypeValue(datatypeValue)) as! NSPredicate
	}

	public var datatypeValue: Blob {
		return NSKeyedArchiver.archivedData(withRootObject: self).datatypeValue
	}
}

However, that as! NSPredicate bugs me. I would rather not have my code crash in the unexpected case that I can't unwrap an NSPredicate there. Would it be possible to augment the method signature of fromDatatypeValue with a throws clause to handle such errors? Similarly, there might be cases where serialization could fail as well, so while at it I'd suggest converting datatypeValue into a throwing function too.

Dominant language
Swift
Stars
10.2k
Forks
1.6k
Avg merge
6d 2h
Merged PRs (30d)
2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from stephencelis/SQLite.swift

All issues in stephencelis/SQLite.swift

Similar issues

More Swift issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.