/
CBLView+REST.m
81 lines (64 loc) · 2.91 KB
/
CBLView+REST.m
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
//
// CBLView+REST.m
// CouchbaseLite
//
// Created by Jens Alfke on 4/7/16.
// Copyright © 2016 Couchbase, Inc. All rights reserved.
//
#import "CBLView+REST.h"
#import "CBLView+Internal.h"
#import "CBLDatabase+REST.h"
#import "CBJSONEncoder.h"
@implementation CBLView (REST)
- (CBLStatus) compileFromDesignDoc {
if (!self.isDesignDoc && self.registeredMapBlock) /* Native design doc like view */
return kCBLStatusOK;
// see if there's a design doc with a CouchDB-style view definition we can compile:
NSString* language;
NSDictionary* viewProps = $castIf(NSDictionary, [self.database getDesignDocFunction: self.name
key: @"views"
language: &language]);
if (!viewProps)
return kCBLStatusNotFound;
LogTo(View, @"%@: Attempting to compile %@ from design doc", self.name, language);
if (![CBLView compiler])
return kCBLStatusNotImplemented;
return [self compileFromProperties: viewProps language: language];
}
- (CBLStatus) compileFromProperties: (NSDictionary*)viewProps language: (NSString*)language {
// Version string is based on a digest of the properties:
NSError* error;
NSString* version = CBLHexSHA1Digest([CBJSONEncoder canonicalEncoding: viewProps error: &error]);
if (!version)
Warn(@"View %@ could not generate version string from the view properties: %@", self, error);
if ([version isEqualToString: self.mapVersion])
return kCBLStatusOK; // Same as the current version
if (!language)
language = @"javascript";
NSString* mapSource = viewProps[@"map"];
if (!mapSource)
return kCBLStatusNotFound;
CBLMapBlock mapBlock = [[CBLView compiler] compileMapFunction: mapSource language: language];
if (!mapBlock) {
Warn(@"View %@ could not compile %@ map fn: %@", self.name, language, mapSource);
return kCBLStatusCallbackError;
}
NSString* reduceSource = viewProps[@"reduce"];
CBLReduceBlock reduceBlock = NULL;
if (reduceSource) {
reduceBlock = [[CBLView compiler] compileReduceFunction: reduceSource language: language];
if (!reduceBlock) {
Warn(@"View %@ could not compile %@ map fn: %@", self.name, language, reduceSource);
return kCBLStatusCallbackError;
}
}
[self setMapBlock: mapBlock reduceBlock: reduceBlock version: version];
self.documentType = $castIf(NSString, viewProps[@"documentType"]);
NSDictionary* options = $castIf(NSDictionary, viewProps[@"options"]);
self.collation = ($equal(options[@"collation"], @"raw")) ? kCBLViewCollationRaw
: kCBLViewCollationUnicode;
// Mark as a design doc view:
self.isDesignDoc = YES;
return kCBLStatusOK;
}
@end