| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package datastore | 5 package datastore |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bytes" | 8 "bytes" |
| 9 "fmt" | 9 "fmt" |
| 10 "strings" | 10 "strings" |
| 11 ) | 11 ) |
| 12 | 12 |
| 13 // IndexColumn represents a sort order for a single entity field. | 13 // IndexColumn represents a sort order for a single entity field. |
| 14 type IndexColumn struct { | 14 type IndexColumn struct { |
| 15 Property string | 15 Property string |
| 16 Descending bool | 16 Descending bool |
| 17 } | 17 } |
| 18 | 18 |
| 19 // ParseIndexColumn takes a spec in the form of /\s*-?\s*.+\s*/, and | 19 // ParseIndexColumn takes a spec in the form of /\s*-?\s*.+\s*/, and |
| 20 // returns an IndexColumn. Examples are: | 20 // returns an IndexColumn. Examples are: |
| 21 // `- Field `: IndexColumn{Property: "Field", Descending: true} | 21 // `- Field `: IndexColumn{Property: "Field", Descending: true} |
| 22 // `Something`: IndexColumn{Property: "Something", Descending: false} | 22 // `Something`: IndexColumn{Property: "Something", Descending: false} |
| 23 // | 23 // |
| 24 // `+Field` is invalid. `` is invalid. | 24 // `+Field` is invalid. `` is invalid. |
| 25 func ParseIndexColumn(spec string) (IndexColumn, error) { | 25 func ParseIndexColumn(spec string) (IndexColumn, error) { |
| 26 col := IndexColumn{} | 26 col := IndexColumn{} |
| 27 spec = strings.TrimSpace(spec) |
| 27 if strings.HasPrefix(spec, "-") { | 28 if strings.HasPrefix(spec, "-") { |
| 28 col.Descending = true | 29 col.Descending = true |
| 29 col.Property = strings.TrimSpace(spec[1:]) | 30 col.Property = strings.TrimSpace(spec[1:]) |
| 30 } else if strings.HasPrefix(spec, "+") { | 31 } else if strings.HasPrefix(spec, "+") { |
| 31 return col, fmt.Errorf("datastore: invalid order: %q", spec) | 32 return col, fmt.Errorf("datastore: invalid order: %q", spec) |
| 32 } else { | 33 } else { |
| 33 col.Property = strings.TrimSpace(spec) | 34 col.Property = strings.TrimSpace(spec) |
| 34 } | 35 } |
| 35 if col.Property == "" { | 36 if col.Property == "" { |
| 36 » » return col, fmt.Errorf("datastore: empty order") | 37 » » return col, fmt.Errorf("datastore: empty order: %q", spec) |
| 37 } | 38 } |
| 38 return col, nil | 39 return col, nil |
| 39 } | 40 } |
| 40 | 41 |
| 41 func (i IndexColumn) cmp(o IndexColumn) int { | 42 func (i IndexColumn) cmp(o IndexColumn) int { |
| 42 // sort ascending first | 43 // sort ascending first |
| 43 if !i.Descending && o.Descending { | 44 if !i.Descending && o.Descending { |
| 44 return -1 | 45 return -1 |
| 45 } else if i.Descending && !o.Descending { | 46 } else if i.Descending && !o.Descending { |
| 46 return 1 | 47 return 1 |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 } | 256 } |
| 256 for _, sb := range id.SortBy { | 257 for _, sb := range id.SortBy { |
| 257 wr('/') | 258 wr('/') |
| 258 if sb.Descending { | 259 if sb.Descending { |
| 259 wr('-') | 260 wr('-') |
| 260 } | 261 } |
| 261 ws(sb.Property) | 262 ws(sb.Property) |
| 262 } | 263 } |
| 263 return ret.String() | 264 return ret.String() |
| 264 } | 265 } |
| OLD | NEW |