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" |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 return false | 221 return false |
222 } | 222 } |
223 for _, sb := range id.SortBy { | 223 for _, sb := range id.SortBy { |
224 if sb.Property == "" || sb.Property == "__ancestor__" { | 224 if sb.Property == "" || sb.Property == "__ancestor__" { |
225 return false | 225 return false |
226 } | 226 } |
227 } | 227 } |
228 return true | 228 return true |
229 } | 229 } |
230 | 230 |
| 231 // YAMLString returns the YAML representation of this IndexDefinition. |
| 232 // |
| 233 // If the index definition is Builtin() or not Compound(), this will return |
| 234 // an error. |
| 235 func (id *IndexDefinition) YAMLString() (string, error) { |
| 236 if id.Builtin() || !id.Compound() { |
| 237 return "", fmt.Errorf("cannot generate YAML for this IndexDefini
tion") |
| 238 } |
| 239 |
| 240 ret := bytes.Buffer{} |
| 241 |
| 242 first := true |
| 243 ws := func(s string, indent int) { |
| 244 nl := "\n" |
| 245 if first { |
| 246 nl = "" |
| 247 first = false |
| 248 } |
| 249 fmt.Fprintf(&ret, "%s%s%s", nl, strings.Repeat(" ", indent), s) |
| 250 } |
| 251 |
| 252 ws(fmt.Sprintf("- kind: %s", id.Kind), 0) |
| 253 if id.Ancestor { |
| 254 ws("ancestor: yes", 1) |
| 255 } |
| 256 ws("properties:", 1) |
| 257 for _, o := range id.SortBy { |
| 258 ws(fmt.Sprintf("- name: %s", o.Property), 1) |
| 259 if o.Descending { |
| 260 ws("direction: desc", 2) |
| 261 } |
| 262 } |
| 263 return ret.String(), nil |
| 264 } |
| 265 |
231 func (id *IndexDefinition) String() string { | 266 func (id *IndexDefinition) String() string { |
232 » ret := &bytes.Buffer{} | 267 » ret := bytes.Buffer{} |
233 wr := func(r rune) { | 268 wr := func(r rune) { |
234 _, err := ret.WriteRune(r) | 269 _, err := ret.WriteRune(r) |
235 if err != nil { | 270 if err != nil { |
236 panic(err) | 271 panic(err) |
237 } | 272 } |
238 } | 273 } |
239 | 274 |
240 ws := func(s string) { | 275 ws := func(s string) { |
241 _, err := ret.WriteString(s) | 276 _, err := ret.WriteString(s) |
242 if err != nil { | 277 if err != nil { |
(...skipping 13 matching lines...) Expand all Loading... |
256 } | 291 } |
257 for _, sb := range id.SortBy { | 292 for _, sb := range id.SortBy { |
258 wr('/') | 293 wr('/') |
259 if sb.Descending { | 294 if sb.Descending { |
260 wr('-') | 295 wr('-') |
261 } | 296 } |
262 ws(sb.Property) | 297 ws(sb.Property) |
263 } | 298 } |
264 return ret.String() | 299 return ret.String() |
265 } | 300 } |
OLD | NEW |