-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworks_query.go
411 lines (355 loc) · 13.3 KB
/
works_query.go
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package crossrefapi
import (
"encoding"
"fmt"
"net/url"
"reflect"
"strings"
"github.com/google/go-querystring/query"
)
// WorksQueryFields represents the fields that can be queried in the CrossRef API
type WorksQueryFields struct {
Affiliation string `url:"query.affiliation,omitempty"`
Author string `url:"query.author,omitempty"`
Bibliographic string `url:"query.bibliographic,omitempty"`
Chair string `url:"query.chair,omitempty"`
ContainerTitle string `url:"query.container-title,omitempty"`
Contributor string `url:"query.contributor,omitempty"`
Degree string `url:"query.degree,omitempty"`
Description string `url:"query.description,omitempty"`
Editor string `url:"query.editor,omitempty"`
EventAcronym string `url:"query.event-acronym,omitempty"`
EventLocation string `url:"query.event-location,omitempty"`
EventName string `url:"query.event-name,omitempty"`
EventTheme string `url:"query.event-theme,omitempty"`
FunderName string `url:"query.funder-name,omitempty"`
PublisherLocation string `url:"query.publisher-location,omitempty"`
PublisherName string `url:"query.publisher-name,omitempty"`
StandardsBodyAcronym string `url:"query.standards-body-acronym,omitempty"`
StandardsBodyName string `url:"query.standards-body-name,omitempty"`
Title string `url:"query.title,omitempty"`
Translator string `url:"query.translator,omitempty"`
}
type SortOrder string
const (
Asc SortOrder = "asc"
Desc SortOrder = "desc"
)
type SortKey string
const (
Created SortKey = "created"
Deposited SortKey = "deposited"
Indexed SortKey = "indexed"
IsReferencedByCount SortKey = "is-referenced-by-count"
Issued SortKey = "issued"
Published SortKey = "published"
PublishedOnline SortKey = "published-online"
PublishedPrint SortKey = "published-print"
ReferencesCount SortKey = "references-count"
Relevance SortKey = "relevance"
Score SortKey = "score"
LastUpdate SortKey = "updated" // renamed from `Updated` to avoid name collision
)
type QuerySortOptions struct {
Key SortKey `url:"sort,omitempty"`
Order SortOrder `url:"order,omitempty"` // default is "desc"
}
// License represents license-specific filter parameters
type LicenseFilter struct {
URL []string `key:"url,omitempty"`
Version []string `key:"version,omitempty"`
Delay []int `key:"delay,omitempty"`
}
// Relation represents relation-specific filter parameters
type RelationFilter struct {
Type []string `key:"type,omitempty"`
ObjectType []string `key:"object-type,omitempty"`
Object []string `key:"object,omitempty"`
}
type FullTextFilter struct {
Type []string `key:"type,omitempty"`
Application []string `key:"application,omitempty"`
Version []string `key:"version,omitempty"`
}
type AwardFilter struct {
Funder []string `key:"funder,omitempty"`
Number []int `key:"number,omitempty"`
}
type DateParameter struct {
Year int32
Month int32
Day int32
}
func (d DateParameter) String() string {
s := fmt.Sprintf("%d", d.Year)
if d.Month <= 0 {
return s
}
s = fmt.Sprintf("%s-%d", s, d.Month)
if d.Day <= 0 {
return s
}
s = fmt.Sprintf("%s-%d", s, d.Day)
return s
}
func (d DateParameter) MarshalText() ([]byte, error) {
return []byte(d.String()), nil
}
// BoolParameter overrides boolean text marshalling to comply with CrossRef API spec
type BoolParameter bool
func (b BoolParameter) MarshalText() ([]byte, error) {
if b {
return []byte("1"), nil
} else {
return []byte("0"), nil
}
}
// WorksFilter represents the available filter parameters for the /works endpoint
type WorksFilter struct {
AlternativeID []string `key:"alternative-id,omitempty"`
Archive []string `key:"archive,omitempty"`
ArticleNumber []string `key:"article-number,omitempty"`
Assertion []string `key:"assertion,omitempty"`
AssertionGroup []string `key:"assertion-group,omitempty"`
// Award related fields
Award *AwardFilter `key:"award,omitempty"`
CategoryName []string `key:"category-name,omitempty"`
CitationID []string `key:"citation-id,omitempty"`
ClinicalTrialNumber []string `key:"clinical-trial-number,omitempty"`
ContainerTitle []string `key:"container-title,omitempty"`
ContentDomain []string `key:"content-domain,omitempty"`
DOI []string `key:"doi,omitempty"`
// From date fields
FromAcceptedDate []DateParameter `key:"from-accepted-date,omitempty"`
FromApprovedDate []DateParameter `key:"from-approved-date,omitempty"`
FromAwardedDate []DateParameter `key:"from-awarded-date,omitempty"`
FromCreatedDate []DateParameter `key:"from-created-date,omitempty"`
FromDepositDate []DateParameter `key:"from-deposit-date,omitempty"`
FromEventEndDate []DateParameter `key:"from-event-end-date,omitempty"`
FromEventStartDate []DateParameter `key:"from-event-start-date,omitempty"`
FromIndexDate []DateParameter `key:"from-index-date,omitempty"`
FromIssuedDate []DateParameter `key:"from-issued-date,omitempty"`
FromOnlinePubDate []DateParameter `key:"from-online-pub-date,omitempty"`
FromPostedDate []DateParameter `key:"from-posted-date,omitempty"`
FromPrintPubDate []DateParameter `key:"from-print-pub-date,omitempty"`
FromPubDate []DateParameter `key:"from-pub-date,omitempty"`
FromUpdateDate []DateParameter `key:"from-update-date,omitempty"`
// Full text related fields
FullText *FullTextFilter `key:"full-text,omitempty"`
// Other fields
Funder []string `key:"funder,omitempty"`
FunderDoiAssertedBy []string `key:"funder-doi-asserted-by,omitempty"`
GroupTitle []string `key:"group-title,omitempty"`
// Boolean flags
// No point supporting multiple filter values as array here
HasAbstract *BoolParameter `key:"has-abstract"`
HasAffiliation *BoolParameter `key:"has-affiliation"`
HasArchive *BoolParameter `key:"has-archive"`
HasAssertion *BoolParameter `key:"has-assertion"`
HasAuthenticatedOrcid *BoolParameter `key:"has-authenticated-orcid"`
HasAward *BoolParameter `key:"has-award"`
HasClinicalTrialNumber *BoolParameter `key:"has-clinical-trial-number"`
HasContentDomain *BoolParameter `key:"has-content-domain"`
HasDescription *BoolParameter `key:"has-description"`
HasDomainRestriction *BoolParameter `key:"has-domain-restriction"`
HasEvent *BoolParameter `key:"has-event"`
HasFullText *BoolParameter `key:"has-full-text"`
HasFunder *BoolParameter `key:"has-funder"`
HasFunderDoi *BoolParameter `key:"has-funder-doi"`
HasLicense *BoolParameter `key:"has-license"`
HasOrcid *BoolParameter `key:"has-orcid"`
HasReferences *BoolParameter `key:"has-references"`
HasRelation *BoolParameter `key:"has-relation"`
HasRorID *BoolParameter `key:"has-ror-id"`
HasUpdate *BoolParameter `key:"has-update"`
HasUpdatePolicy *BoolParameter `key:"has-update-policy"`
IsUpdate *BoolParameter `key:"is-update"`
// ISBN/ISSN
ISBN []string `key:"isbn,omitempty"`
ISSN []string `key:"issn,omitempty"`
// License fields
License *LicenseFilter `key:"license,omitempty"`
// Award amount
GteAwardAmount int `key:"gte-award-amount,omitempty"`
LteAwardAmount int `key:"lte-award-amount,omitempty"`
// Member and identifiers
Member []string `key:"member,omitempty"`
ORCID []string `key:"orcid,omitempty"`
Prefix []string `key:"prefix,omitempty"`
// Relation fields
Relation *RelationFilter `key:"relation,omitempty"`
// Type fields
RorID []string `key:"ror-id,omitempty"`
Type []string `key:"type,omitempty"`
TypeName []string `key:"type-name,omitempty"`
// Until date fields
UntilAcceptedDate []DateParameter `key:"until-accepted-date,omitempty"`
UntilApprovedDate []DateParameter `key:"until-approved-date,omitempty"`
UntilAwardedDate []DateParameter `key:"until-awarded-date,omitempty"`
UntilCreatedDate []DateParameter `key:"until-created-date,omitempty"`
UntilDepositDate []DateParameter `key:"until-deposit-date,omitempty"`
UntilEventEndDate []DateParameter `key:"until-event-end-date,omitempty"`
UntilEventStartDate []DateParameter `key:"until-event-start-date,omitempty"`
UntilIndexDate []DateParameter `key:"until-index-date,omitempty"`
UntilIssuedDate []DateParameter `key:"until-issued-date,omitempty"`
UntilOnlinePubDate []DateParameter `key:"until-online-pub-date,omitempty"`
UntilPostedDate []DateParameter `key:"until-posted-date,omitempty"`
UntilPrintPubDate []DateParameter `key:"until-print-pub-date,omitempty"`
UntilPubDate []DateParameter `key:"until-pub-date,omitempty"`
UntilUpdateDate []DateParameter `key:"until-update-date,omitempty"`
// Update fields
UpdateType []string `key:"update-type,omitempty"`
Updates []string `key:"updates,omitempty"`
}
func marshalStruct(v interface{}, prefix string) []string {
withName := func(s string) string {
if prefix != "" {
return fmt.Sprintf("%s:%s", prefix, s)
}
return s
}
withPrefix := func(s string) string {
if prefix != "" {
return fmt.Sprintf("%s.%s", prefix, s)
}
return s
}
val := reflect.ValueOf(v)
typ := val.Type()
var result []string
// Handle text marshaller interface
if marshaller, ok := val.Interface().(encoding.TextMarshaler); ok && marshaller != nil {
text, err := marshaller.MarshalText()
if err == nil && len(text) > 0 {
result = append(result, withName(string(text)))
return result
}
}
switch t := v.(type) {
case string:
return []string{withName(t)}
case *string:
if t == nil {
return nil
}
return []string{withName(*t)}
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return []string{withName(fmt.Sprintf("%d", t))}
case *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64:
if t == nil {
return nil
}
return []string{withName(fmt.Sprintf("%d", t))}
default:
// Proceed
}
// If it's a pointer, dereference it
if val.Kind() == reflect.Ptr {
if val.IsNil() {
return nil
}
val = val.Elem()
typ = val.Type()
}
// Handle slice
if val.Kind() == reflect.Slice {
for i := 0; i < val.Len(); i++ {
result = append(result, marshalStruct(val.Index(i).Interface(), prefix)...)
}
fmt.Printf("%v\n", result)
return result
}
// Not scalar nor slice: ensure value is Struct
if val.Kind() != reflect.Struct {
return nil
}
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
fieldVal := val.Field(i)
// Get the key tag
tag := field.Tag.Get("key")
if tag == "" {
tag = field.Name
}
tagParts := strings.Split(tag, ",")
name := tagParts[0]
omitempty := len(tagParts) > 1 && tagParts[1] == "omitempty"
newPrefix := withPrefix(name)
// Skip nil pointers and slices
if (field.Type.Kind() == reflect.Ptr || field.Type.Kind() == reflect.Slice) && fieldVal.IsNil() {
continue
}
// Skip if field is zero value and omitempty is set
if omitempty && (fieldVal.Kind() != reflect.Ptr && fieldVal.IsZero()) {
continue
}
// Recursively marshal struct fields
result = append(result, marshalStruct(fieldVal.Interface(), newPrefix)...)
}
return result
}
func (f WorksFilter) Encode() string {
parts := marshalStruct(f, "")
b := []byte(strings.Join(parts, ","))
return string(b)
}
type Pagination struct {
//The number of items returned in a single response (default is 20, and maximum is 1,000).
Rows int64 `url:"rows,omitempty"`
// offset parameter can be used to retrieve items starting from a specific index of the result list
Offset int64 `url:"offset,omitempty"`
// see "Deep-paging" section of CrossRef works API doc
Cursor string `url:"cursor,omitempty"`
}
// WorksQuery represents a query for works in the CrossRef API.
// See https://api.crossref.org/swagger-ui/index.html#/Works/get_works.
type WorksQuery struct {
// General query string
// Note: as stated in CrossRef API docs,
// Fields.Bibliographic SHOULD be preferred
// whenever the search query is a partial or full reference
// (e.g. including authors, date, title, ...)
FreeFormQuery string
// Structured query on specific metadata
Fields *WorksQueryFields
// Results pagination
Pagination *Pagination
// Results filtering
// Filters on different keys are applied with AND semantic.
// Filters on the same key are applied with OR semantic.
Filters *WorksFilter
// Results projection.
// Restrict the API results to a subset of fields.
// See API docs for available fields
Elements []string
}
func (q WorksQuery) Encode() (values url.Values, err error) {
// Named query parameters
values, err = query.Values(q.Fields)
if err != nil {
return values, err
}
// Free form query
if strings.TrimSpace(q.FreeFormQuery) != "" {
values.Add("query", q.FreeFormQuery)
}
// Pagination
if q.Pagination != nil {
pag, err := query.Values(q.Pagination)
if err != nil {
return values, err
}
mergeQueries(&values, pag)
}
// Filters
if q.Filters != nil {
filters := q.Filters.Encode()
values.Add("filter", filters)
}
// Projection
if q.Elements != nil {
projection := strings.Join(q.Elements, ",")
values.Add("select", projection)
}
return values, nil
}