-
Notifications
You must be signed in to change notification settings - Fork 19
/
Model.scala
116 lines (89 loc) · 3.54 KB
/
Model.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Copyright 2015 IBM Corporation, Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ibm.couchdb
import java.util.Base64
import org.http4s.headers.`Content-Type`
import scalaz.\/
case class Config(host: String, port: Int, https: Boolean, credentials: Option[(String, String)])
case class CouchDoc[D](
doc: D,
kind: String,
_id: String = "",
_rev: String = "",
_deleted: Boolean = false,
_attachments: Map[String, CouchAttachment] = Map.empty[String, CouchAttachment],
_conflicts: Seq[String] = Seq.empty[String],
_deleted_conflicts: Seq[String] = Seq.empty[String],
_local_seq: Int = 0)
case class CouchDocRev(rev: String)
case class CouchKeyVal[K, V](id: String, key: K, value: V)
case class CouchReducedKeyVal[K, V](key: K, value: V)
case class CouchKeyError[K](key: K, error: String)
case class CouchKeyValWithDoc[K, V, D](id: String, key: K, value: V, doc: CouchDoc[D])
case class CouchKeyVals[K, V](offset: Int, total_rows: Int, rows: Seq[CouchKeyVal[K, V]])
case class CouchReducedKeyVals[K, V](rows: Seq[CouchReducedKeyVal[K, V]])
case class CouchKeyValsIncludesMissing[K, V](
offset: Int,
total_rows: Int,
rows: Seq[\/[CouchKeyError[K], CouchKeyVal[K, V]]])
case class CouchDocs[K, V, D](
offset: Int, total_rows: Int, rows: Seq[CouchKeyValWithDoc[K, V, D]]) {
def getDocs: Seq[CouchDoc[D]] = rows.map(_.doc)
def getDocsData: Seq[D] = rows.map(_.doc.doc)
}
case class CouchDocsIncludesMissing[K, V, D](
offset: Int,
total_rows: Int,
rows: Seq[\/[CouchKeyError[K], CouchKeyValWithDoc[K, V, D]]]) {
def getDocs: Seq[CouchDoc[D]] = rows.flatMap(_.toOption).map(_.doc)
def getDocsData: Seq[D] = rows.flatMap(_.toOption).map(_.doc.doc)
}
case class CouchAttachment(
content_type: String,
revpos: Int = -1,
digest: String = "",
data: String = "",
length: Int = -1,
stub: Boolean = false) {
def toBytes: Array[Byte] = Base64.getDecoder.decode(data)
}
case object CouchAttachment {
def fromBytes(data: Array[Byte], content_type: String = ""): CouchAttachment = {
CouchAttachment(
content_type = content_type,
data = Base64.getEncoder.encodeToString(data))
}
def fromBytes(data: Array[Byte], content_type: `Content-Type`): CouchAttachment = {
CouchAttachment(
content_type = content_type.toString,
data = Base64.getEncoder.encodeToString(data))
}
}
case class CouchView(map: String, reduce: String = "")
case class CouchDesign(
name: String,
_id: String = "",
_rev: String = "",
language: String = "javascript",
validate_doc_update: String = "",
views: Map[String, CouchView] = Map.empty[String, CouchView],
shows: Map[String, String] = Map.empty[String, String],
lists: Map[String, String] = Map.empty[String, String],
_attachments: Map[String, CouchAttachment] = Map.empty[String, CouchAttachment],
signatures: Map[String, String] = Map.empty[String, String])
case class CouchException[D](content: D) extends Throwable {
override def toString: String = "CouchException: " + content
}