aboutsummaryrefslogtreecommitdiff
path: root/sx-filter.el
blob: c67d05bc49bfb3b8cdb767d9be427aa90a803ab9 (plain) (blame)
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
116
117
118
119
120
121
;;; sx-filter.el --- Handles retrieval of filters.                         -*- lexical-binding: t; -*-

;; Copyright (C) 2014  Sean Allred

;; Author: Sean Allred <code@seanallred.com>

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;;; Code:


;;; Dependencies

(require 'sx)
(require 'sx-cache)
(require 'sx-request)


;;; Customizations

(defvar sx--filter-alist
  (sx-cache-get 'filter)
  "An alist of known filters.  See `sx-filter-compile'.
Structure:

    (((INCLUDE  EXCLUDE  BASE ) . \"compiled filter \")
     ((INCLUDE2 EXCLUDE2 BASE2) . \"compiled filter2\")
     ...)")


;;; Creation

(defmacro sx-filter-from-nil (included)
  "Creates a filter data structure with INCLUDED fields.
All wrapper fields are included by default."
  ;; @OTODO: it would be neat to have syntax like
  ;; 
  ;; (field-a
  ;;  field-b
  ;;  (object-a subfield)
  ;;  field-c
  ;;  (object-b subfield-a subfield-b))
  ;;
  ;; expand into
  ;;
  ;; (field-a
  ;;  field-b
  ;;  object-a.subfield
  ;;  field-c
  ;;  object-b.subfield-a object-b.subfield-b)
  `(quote ((,@included
            .backoff
            .error_id
            .error_message
            .error_name
            .has_more
            .items
            .page
            .page_size
            .quota_max
            .quota_remaining
            .type)
           nil none)))

;;; @TODO allow BASE to be a precompiled filter name
(defun sx-filter-compile (&optional include exclude base)
  "Compile INCLUDE and EXCLUDE into a filter derived from BASE.
INCLUDE and EXCLUDE must both be lists; BASE should be a string.

Returns the compiled filter as a string."
  (let ((keyword-arguments
         `((include . ,(if include (sx--thing-as-string include)))
           (exclude . ,(if exclude (sx--thing-as-string exclude)))
           (base    . ,(if base base)))))
    (let ((response (elt (sx-request-make
                          "filter/create"
                          keyword-arguments) 0)))
      (sx-assoc-let response
        .filter))))


;;; Storage and Retrieval

(defun sx-filter-get-var (filter-variable)
  "Return the string representation of FILTER-VARIABLE."
  (apply #'sx-filter-get filter-variable))

(defun sx-filter-get (&optional include exclude base)
  "Return the string representation of the given filter.

If the filter data exist in `sx--filter-alist', that value will
be returned.  Otherwise, compile INCLUDE, EXCLUDE, and BASE into
a filter with `sx-filter-compile' and push the association onto
`sx--filter-alist'.  Re-cache the alist with `sx-cache-set' and
return the compiled filter."
  (or (cdr (assoc (list include exclude base) sx--filter-alist))
      (let ((filter (sx-filter-compile include exclude base)))
        (when filter
          (push (cons (list include exclude base) filter) sx--filter-alist)
          (sx-cache-set 'filter sx--filter-alist)
          filter))))

(provide 'sx-filter)
;;; sx-filter.el ends here

;; Local Variables:
;; indent-tabs-mode: nil
;; End: