ResolveOrderBy resolves order by statement.
(q map[string]interface{}, availFields FieldMap)
| 435 | |
| 436 | // ResolveOrderBy resolves order by statement. |
| 437 | func ResolveOrderBy(q map[string]interface{}, availFields FieldMap) (fields FieldMap, statement string, err error) { |
| 438 | fields = FieldMap{} |
| 439 | |
| 440 | if len(q) == 0 { |
| 441 | // no order by config |
| 442 | return |
| 443 | } |
| 444 | |
| 445 | var colStatements []string |
| 446 | |
| 447 | for k, v := range q { |
| 448 | if !availFields[k] { |
| 449 | err = errors.Errorf("unknown field: %s", k) |
| 450 | return |
| 451 | } |
| 452 | |
| 453 | if !isLiteral(v) { |
| 454 | err = errors.Errorf("invalid sort direction: %v", v) |
| 455 | return |
| 456 | } |
| 457 | |
| 458 | var direction string |
| 459 | |
| 460 | if asInt(v) > 0 { |
| 461 | direction = "ASC" |
| 462 | } else if asInt(v) < 0 { |
| 463 | direction = "DESC" |
| 464 | } else { |
| 465 | err = errors.Errorf("invalid sort direction: %v", v) |
| 466 | return |
| 467 | } |
| 468 | |
| 469 | fields[k] = true |
| 470 | |
| 471 | colStatements = append(colStatements, fmt.Sprintf(`"%s" %s`, k, direction)) |
| 472 | } |
| 473 | |
| 474 | statement = strings.Join(colStatements, ",") |
| 475 | |
| 476 | return |
| 477 | } |
| 478 | |
| 479 | func asInt(v interface{}) int64 { |
| 480 | switch d := v.(type) { |