我試圖拿出一個CSV分析器,可以這樣調用:DSL類似的語法在斯卡拉
parser parse "/path/to/csv/file" using parserConfiguration
凡解析器將包含目標案例類到其中的類CSV文件將被解析成:
class CSVParser[A] {
def parse(path: String) = Source.fromFile(fromFilePath).getLines().mkString("\n")
def using(cfg: ParserConfig) = ??? How do I chain this optionally???
}
val parser = CSVParser[SomeCaseClass]
我設法爬起來的地步,我可以打電話:
parser parse "/the/path/to/the/csv/file/"
但我不想要運行解析方法,因爲我想要像上面提到的那樣使用像DSL一樣的應用配置!所以這裏有兩條規則。如果調用者不提供parserConfig,我應該可以使用默認運行,但是如果用戶提供了parserConfig,我想應用配置並運行解析方法。我試着用implicits的組合,但不能讓它們正常工作!
有什麼建議嗎?
編輯:因此,該解決方案看起來像這樣按從「西里爾Corpet」評論:
class CSVReader[A] {
def parse(path: String) = ReaderWithFile[A](path)
case class ReaderWithFile[A](path: String) {
def using(cfg: CSVParserConfig): Seq[A] = {
val lines = Source.fromFile(path).getLines().mkString("\n")
println(lines)
println(cfg)
null
}
}
object ReaderWithFile {
implicit def parser2parsed[A](parser: ReaderWithFile[A]): Seq[A] = parser.using(defaultParserCfg)
}
}
object CSVReader extends App {
def parser[A] = new CSVReader[A]
val sss: Seq[A] = parser parse "/csv-parser/test.csv" // assign this to a val so that the implicit conversion gets applied!! Very important to note!
}
我想我需要的隱含範圍在這裏我所說的解析器解析的位置,但在同時我不想搞亂我上面的結構!
好的!幾乎在那裏,但我怎樣才能把這個組織成適當的結構呢?看看我上面編輯的帖子! – sparkr
你可以看看我編輯過的文章嗎? – sparkr
好的!我終於開始工作了! – sparkr