orlandos-nl / MongoKitten

Native MongoDB driver for Swift, written in Swift
MIT License
716 stars 101 forks source link

MongoKitten and Swift Playground #184

Closed kzahedi closed 5 years ago

kzahedi commented 5 years ago

Hi,

I'm trying to use MongoKitten in a Swift Playground for Data Science purposes. Unfortunately, I cannot get it to work, because it will not find the CNIOOpenSSL module.

Does anyone know a solution to this problem?

Best, Keyan

Joannis commented 5 years ago

Hello 👋 This could happen because your version of macOS doesn't have the openSSL or libreSSL headers installed by default. If that's not the case it might be that your Xcodeproject was created without pkg-config installed, it will likely not find the C library (libssl) without it.

Joannis commented 5 years ago

Just so I can help better; what steps did you use to create the playground?

kzahedi commented 5 years ago

Thank you very much for your fast reply. Here are my steps:

  1. I used Carthage to download and install MongoKitten
  2. Called GenerateXcodeproj.rb in ~/Carthage/Checkouts/MongoKitten to create the project file
  3. Created a new workspace
  4. Added a playground file to my new workspace (code below)
  5. Added MongoKitten.xcodeproj to my workspace
  6. Compiled MongoKitten-Package for "My Mac"

My Swift Playground is:

import Cocoa import MongoKitten

var str = "Hello, playground"

And I receive the following error message

error: missing required module 'CNIOOpenSSL'

I have attached a screenshot below.

The failed compile message for MongoKitten contains 999+ compile errors. I have screenshotted a few of them in the second screenshot below. I had installed sourcery via homebrew.

Thanks, Keyan

screenshot 2019-02-15 at 17 47 06 screenshot 2019-02-15 at 17 28 24
Joannis commented 5 years ago

The 120 characters error is not an error in our project or tooling. I'm suspecting your xcworkspace is running some script over the code for linting with a configuration we do not support. How did you link the playground against MongoKitten?

kzahedi commented 5 years ago

I am new to swift and playground. Basically, I followed the instructions I found in a WWDC talk. I opened a new workspace and included the MongoKitten.xcodeproj using the "Add file to " option. This works fine with other 3rd party libraries, such as Yams (yaml parser).

kzahedi commented 5 years ago

I don't recall installing swiftlint via brew, but somehow, it was installed. Uninstalling it removed all the compile errors for MongoKitten, but the include problem remains. Trying the run the three lines of code shown above still results in:

error: missing required module 'CNIOOpenSSL'

Recompiling CNIOOpenSSL did not resolve the issue.

MasterSwift commented 5 years ago

try using https://github.com/JohnSundell/TestDrive

Joannis commented 5 years ago

I didn't try the above, but I'm not sure how to get it working otherwise. If you clone MongoKitten itself using git or by copying the zip from the releases tab here on github. You can run swift build. Does this work?

Does it work if you create the xcodeproject then with swift package generate-xcodeproj?

kzahedi commented 5 years ago

try using https://github.com/JohnSundell/TestDrive

Thanks for the tip. Unfortunately, it does not work for MongoKitten:

testdrive https://github.com/OpenKitten/MongoKitten.git 📦 Cloning https://github.com/OpenKitten/MongoKitten.git... 🚢 Resolving latest version... 📋 Checking out 5.1.1... 💥 Xcode project missing at 'https://github.com/OpenKitten/MongoKitten.git'

Joannis commented 5 years ago

Hmm... 🙁 None of those tools all support SPM it seems. You can try to create a small package (without Playground) for now.

mkdir NotAPlayground
cd NotAPlayground
swift package init --type=exeuctable
open Package.swift

Update the Package.swift

swift package generate-xcodeproj
open NotAPlayground.xcodeproj
kzahedi commented 5 years ago

That works fine but causes other problems. One that is related to MongoKitten is the question of how to wait for the find expression to complete before moving on in the code.

Joannis commented 5 years ago

SwiftNIO provides the wait() API on all promises. Don't use it within an eventloop. So you'll want to use Database.synchronousConnect(...) and then wait() for all operations.

kzahedi commented 5 years ago

Thanks. That worked. Now I need to find out, how the second library that I rely on works in an executable setting :)

It would still be great to see MongoKitten work in a playground setting. I am trying to move from Python to Swift for my data science stuff and ML stuff.

Joannis commented 5 years ago

@kzahedi if you're interested, we're working on MongoKitten mobile which supports Cocoapods as well as MongoDB's embedded database and NIOTransportServices for macOS and iOS.

I'm closing this since it's solved, but feel free to ask me other things here or in slack 👍