Developing SBT plugin with Dispatch 0.11.0 results in Scala compiler's mysterious errors

StackOverflow https://stackoverflow.com/questions/21765963

  •  11-10-2022
  •  | 
  •  

سؤال

I'm new to Scala and Dispatch, and I can't seem to get a basic Post request working.

I'm actually building a sbt plugin that uploads files to a third party service.

Here is my build.sbt file:

sbtPlugin := true

name := "sbt-sweet-plugin"

organization := "com.mattwalters"

version := "0.0.1-SNAPSHOT"

libraryDependencies += "net.databinder.dispatch" %% "dispatch-core" % "0.11.0"

And here is the plugin's SweetPlugin.scala:

package com.mattwalters

import sbt._
import Keys._
import dispatch._

object SweetPlugin extends Plugin {

  import SweetKeys._
  object SweetKeys {

    lazy val sweetApiToken = 
      SettingKey[String]("Required. Find yours at https://example.com/account/#api")
    lazy val someOtherToken = 
      SettingKey[String]("Required. Find yours at https://example.com/some/other/token/")
    lazy val sweetFile = 
      SettingKey[String]("Required. File data")
    lazy val sweetotes = 
      SettingKey[String]("Required. Release notes")

    lazy val sweetUpload = 
      TaskKey[Unit]("sweetUpload", "A task to upload the specified sweet file.")
  }

  override lazy val settings = Seq (
    sweetNotes := "some default notes",
    // define the upload task
    sweetUpload <<= (
      sweetApiToken,
      someOtherToken,
      sweetFile,
      sweetNotes
    ) map { (
      sweetApiToken,
      someOtherToken,
      sweetFile,
      sweetNotes
    ) =>
      // define http stuff here 
      val request = :/("www.example.com") / "some" / "random" / "endpoint" 
      val post = request.POST
      post.addParameter("api_token", sweetApiToken)
      post.addParameter("some_other_token", someOtherToken)
      post.addParameter("file", io.Source.fromFile(sweetFile).mkString)
      post.addParameter("notes", sweetNotes)
      val responseFuture = Http(post OK as.String)
      val response = responseFuture()
      println(response) // see if we can get something at all....
    }
  )
}

The dispatch documentation shows:

import dispatch._, Defaults._

but I get

reference to Defaults is ambiguous;
[error] it is imported twice in the same scope by

removing , Defaults._ makes this error go away.

I also tried the recommendation from this post:

import dispatch._
Import dispatch.Default._

But alas I get:

object Default is not a member of package dispatch
[error] import dispatch.Default._

Also tried the advice from Passing implicit ExecutionContext to contained objects/called methods:

import concurrent._
import concurrent.duration._

But I still get

Cannot find an implicit ExecutionContext, either require one yourself or import ExecutionContext.Implicits.global

Back to square one...

New to scala so any advice at all on the code above is appreciated.

هل كانت مفيدة؟

المحلول

Since the recommended sbt console run works fine, it looks like one of your other imports also has a Defaults module. It's a standard approach in Scala for collecting implicit values to be used as function params (another naming convention is Implicits).

The other problem is that there is a typo/out-of-date problem in the import statement you got from Google Groups - it's Defaults, plural.

In summary - the best solution is to explicitly let Scala know which module you want to use:

import dispatch._
import dispatch.Defaults._

In the general case, only if the library's docs don't say otherwise: the last error message is pretty common to concurrent programming in Scala. To quote the relevant part:

either require one yourself or import ExecutionContext.Implicits.global

So, unless you want to roll your own ExecutionContext, just import the scala.concurrent.ExecutionContext.Implicits.global one via the scala.concurrent.ExecutionContext.Implicits module.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top