Skip to content

Commit 13b09cd

Browse files
authored
[Feature] [3.7] Schema validation options (#274)
1 parent 78f27fa commit 13b09cd

9 files changed

+285
-26
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [master](https://github.com/arangodb/go-driver/tree/master) (N/A)
44
- Use internal coordinator communication for cursors if specified coordinator was not found on endpoint list
55
- Add support for Overwrite Mode (ArangoDB 3.7)
6+
- Add support for Schema Collection options (ArangoDB 3.7)
67

78
## [1.0.0](https://github.com/arangodb/go-driver/tree/1.0.0) (N/A)
89
- Enable proper CHANGELOG and versioning

collection.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ type CollectionProperties struct {
143143
// The following attribute specifies if the new MerkleTree based sync protocol
144144
// can be used on the collection.
145145
SyncByRevision bool `json:"syncByRevision,omitempty"`
146+
// Schema for collection validation
147+
Schema *CollectionSchemaOptions `json:"schema,omitempty"`
146148
}
147149

148150
const (
@@ -171,6 +173,8 @@ type SetCollectionPropertiesOptions struct {
171173
WriteConcern int `json:"writeConcern,omitempty"`
172174
// CacheEnabled set cacheEnabled option in collection properties
173175
CacheEnabled *bool `json:"cacheEnabled,omitempty"`
176+
// Schema for collection validation
177+
Schema *CollectionSchemaOptions `json:"schema,omitempty"`
174178
}
175179

176180
// CollectionStatus indicates the status of a collection.

collection_impl.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,9 @@ type collectionPropertiesInternal struct {
286286
ShardingStrategy ShardingStrategy `json:"shardingStrategy,omitempty"`
287287
DistributeShardsLike string `json:"distributeShardsLike,omitempty"`
288288
// Available from 3.7 arangod version.
289-
UsesRevisionsAsDocumentIds bool `json:"usesRevisionsAsDocumentIds,omitempty"`
290-
SyncByRevision bool `json:"syncByRevision,omitempty"`
289+
UsesRevisionsAsDocumentIds bool `json:"usesRevisionsAsDocumentIds,omitempty"`
290+
SyncByRevision bool `json:"syncByRevision,omitempty"`
291+
Schema *CollectionSchemaOptions `json:"schema,omitempty"`
291292
}
292293

293294
func (p *collectionPropertiesInternal) asExternal() CollectionProperties {
@@ -308,6 +309,7 @@ func (p *collectionPropertiesInternal) asExternal() CollectionProperties {
308309
DistributeShardsLike: p.DistributeShardsLike,
309310
UsesRevisionsAsDocumentIds: p.UsesRevisionsAsDocumentIds,
310311
SyncByRevision: p.SyncByRevision,
312+
Schema: p.Schema,
311313
}
312314
}
313315

@@ -326,6 +328,7 @@ func (p *CollectionProperties) asInternal() collectionPropertiesInternal {
326328
WriteConcern: p.WriteConcern,
327329
SmartJoinAttribute: p.SmartJoinAttribute,
328330
ShardingStrategy: p.ShardingStrategy,
331+
Schema: p.Schema,
329332
}
330333
}
331334

@@ -371,7 +374,8 @@ type setCollectionPropertiesOptionsInternal struct {
371374
// Deprecated: use 'WriteConcern' instead
372375
MinReplicationFactor int `json:"minReplicationFactor,omitempty"`
373376
// Available from 3.6 arangod version.
374-
WriteConcern int `json:"writeConcern,omitempty"`
377+
WriteConcern int `json:"writeConcern,omitempty"`
378+
Schema *CollectionSchemaOptions `json:"schema,omitempty"`
375379
}
376380

377381
func (p *SetCollectionPropertiesOptions) asInternal() setCollectionPropertiesOptionsInternal {
@@ -382,6 +386,7 @@ func (p *SetCollectionPropertiesOptions) asInternal() setCollectionPropertiesOpt
382386
ReplicationFactor: replicationFactor(p.ReplicationFactor),
383387
MinReplicationFactor: p.MinReplicationFactor,
384388
WriteConcern: p.WriteConcern,
389+
Schema: p.Schema,
385390
}
386391
}
387392

@@ -392,6 +397,7 @@ func (p *SetCollectionPropertiesOptions) fromInternal(i *setCollectionProperties
392397
p.ReplicationFactor = int(i.ReplicationFactor)
393398
p.MinReplicationFactor = i.MinReplicationFactor
394399
p.WriteConcern = i.WriteConcern
400+
p.Schema = i.Schema
395401
}
396402

397403
// MarshalJSON converts SetCollectionPropertiesOptions into json

database_collections.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ type CreateCollectionOptions struct {
113113
// This attribute specifies the name of the sharding strategy to use for the collection.
114114
// Must be one of ShardingStrategy* values.
115115
ShardingStrategy ShardingStrategy `json:"shardingStrategy,omitempty"`
116+
// Schema for collection validation
117+
Schema *CollectionSchemaOptions `json:"schema,omitempty"`
116118
}
117119

118120
// CollectionType is the type of a collection.

database_collections_impl.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,23 +106,24 @@ type createCollectionOptionsInternal struct {
106106
ReplicationFactor replicationFactor `json:"replicationFactor,omitempty"`
107107
CacheEnabled *bool `json:"cacheEnabled,omitempty"`
108108
// Deprecated: use 'WriteConcern' instead
109-
MinReplicationFactor int `json:"minReplicationFactor,omitempty"`
110-
WriteConcern int `json:"writeConcern,omitempty"`
111-
WaitForSync bool `json:"waitForSync,omitempty"`
112-
DoCompact *bool `json:"doCompact,omitempty"`
113-
IsVolatile bool `json:"isVolatile,omitempty"`
114-
ShardKeys []string `json:"shardKeys,omitempty"`
115-
NumberOfShards int `json:"numberOfShards,omitempty"`
116-
IsSystem bool `json:"isSystem,omitempty"`
117-
Type CollectionType `json:"type,omitempty"`
118-
IndexBuckets int `json:"indexBuckets,omitempty"`
119-
KeyOptions *CollectionKeyOptions `json:"keyOptions,omitempty"`
120-
DistributeShardsLike string `json:"distributeShardsLike,omitempty"`
121-
IsSmart bool `json:"isSmart,omitempty"`
122-
SmartGraphAttribute string `json:"smartGraphAttribute,omitempty"`
123-
Name string `json:"name"`
124-
SmartJoinAttribute string `json:"smartJoinAttribute,omitempty"`
125-
ShardingStrategy ShardingStrategy `json:"shardingStrategy,omitempty"`
109+
MinReplicationFactor int `json:"minReplicationFactor,omitempty"`
110+
WriteConcern int `json:"writeConcern,omitempty"`
111+
WaitForSync bool `json:"waitForSync,omitempty"`
112+
DoCompact *bool `json:"doCompact,omitempty"`
113+
IsVolatile bool `json:"isVolatile,omitempty"`
114+
ShardKeys []string `json:"shardKeys,omitempty"`
115+
NumberOfShards int `json:"numberOfShards,omitempty"`
116+
IsSystem bool `json:"isSystem,omitempty"`
117+
Type CollectionType `json:"type,omitempty"`
118+
IndexBuckets int `json:"indexBuckets,omitempty"`
119+
KeyOptions *CollectionKeyOptions `json:"keyOptions,omitempty"`
120+
DistributeShardsLike string `json:"distributeShardsLike,omitempty"`
121+
IsSmart bool `json:"isSmart,omitempty"`
122+
SmartGraphAttribute string `json:"smartGraphAttribute,omitempty"`
123+
Name string `json:"name"`
124+
SmartJoinAttribute string `json:"smartJoinAttribute,omitempty"`
125+
ShardingStrategy ShardingStrategy `json:"shardingStrategy,omitempty"`
126+
Schema *CollectionSchemaOptions `json:"schema,omitempty"`
126127
}
127128

128129
// CreateCollection creates a new collection with given name and options, and opens a connection to it.
@@ -195,6 +196,7 @@ func (p *createCollectionOptionsInternal) fromExternal(i *CreateCollectionOption
195196
p.SmartGraphAttribute = i.SmartGraphAttribute
196197
p.SmartJoinAttribute = i.SmartJoinAttribute
197198
p.ShardingStrategy = i.ShardingStrategy
199+
p.Schema = i.Schema
198200
}
199201

200202
// // MarshalJSON converts CreateCollectionOptions into json

database_collections_schema.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2020 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Adam Janikowski
21+
//
22+
23+
package driver
24+
25+
import (
26+
"encoding/json"
27+
)
28+
29+
type CollectionSchemaLevel string
30+
31+
const (
32+
CollectionSchemaLevelNone CollectionSchemaLevel = "none"
33+
CollectionSchemaLevelNew CollectionSchemaLevel = "new"
34+
CollectionSchemaLevelModerate CollectionSchemaLevel = "moderate"
35+
CollectionSchemaLevelStrict CollectionSchemaLevel = "strict"
36+
)
37+
38+
type CollectionSchemaOptions struct {
39+
Rule interface{} `json:"rule,omitempty"`
40+
Level CollectionSchemaLevel `json:"level,omitempty"`
41+
Message string `json:"message,omitempty"`
42+
}
43+
44+
func (d *CollectionSchemaOptions) LoadRule(data []byte) error {
45+
var rule interface{}
46+
47+
if err := json.Unmarshal(data, &rule); err != nil {
48+
return err
49+
}
50+
51+
d.Rule = rule
52+
return nil
53+
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ require (
99
github.com/dchest/uniuri v0.0.0-20160212164326-8902c56451e9
1010
github.com/dgrijalva/jwt-go v3.2.0+incompatible
1111
github.com/google/uuid v1.1.1
12-
github.com/kr/pretty v0.1.0 // indirect
12+
github.com/kr/pretty v0.2.0 // indirect
1313
github.com/pkg/errors v0.8.1
1414
github.com/stretchr/testify v1.5.1
15-
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
15+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
1616
gopkg.in/yaml.v2 v2.2.8 // indirect
1717
)

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumC
1212
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
1313
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
1414
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
15-
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
16-
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
15+
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
16+
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
1717
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
1818
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
1919
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
@@ -26,8 +26,8 @@ github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H
2626
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
2727
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2828
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
29-
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
30-
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
29+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
30+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3131
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
3232
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
3333
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=

0 commit comments

Comments
 (0)