nodeinfo.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package interpreter
  11. import (
  12. "sort"
  13. "devt.de/krotik/common/stringutil"
  14. "devt.de/krotik/eliasdb/graph"
  15. "devt.de/krotik/eliasdb/graph/data"
  16. )
  17. /*
  18. NodeInfo interface. NodeInfo objects are used by the EQL interpreter to format
  19. search results.
  20. */
  21. type NodeInfo interface {
  22. /*
  23. SummaryAttributes returns the attributes which should be shown
  24. in a list view for a given node kind.
  25. */
  26. SummaryAttributes(kind string) []string
  27. /*
  28. Return the display string for a given attribute.
  29. */
  30. AttributeDisplayString(kind string, attr string) string
  31. /*
  32. Check if a given string can be a valid node attribute.
  33. */
  34. IsValidAttr(attr string) bool
  35. }
  36. /*
  37. defaultNodeInfo data structure
  38. */
  39. type defaultNodeInfo struct {
  40. gm *graph.Manager
  41. }
  42. /*
  43. NewDefaultNodeInfo creates a new default NodeInfo instance. The default NodeInfo
  44. provides the most generic rendering information to the interpreter.
  45. */
  46. func NewDefaultNodeInfo(gm *graph.Manager) NodeInfo {
  47. return &defaultNodeInfo{gm}
  48. }
  49. /*
  50. SummaryAttributes returns the attributes which should be shown
  51. in a list view for a given node kind.
  52. */
  53. func (ni *defaultNodeInfo) SummaryAttributes(kind string) []string {
  54. if kind == "" {
  55. return []string{data.NodeKey, data.NodeKind, data.NodeName}
  56. }
  57. attrs := ni.gm.NodeAttrs(kind)
  58. ret := make([]string, 0, len(attrs))
  59. for _, attr := range attrs {
  60. if attr == data.NodeKey || attr == data.NodeKind {
  61. continue
  62. }
  63. ret = append(ret, attr)
  64. }
  65. sort.StringSlice(ret).Sort()
  66. // Prepend the key attribute
  67. ret = append([]string{data.NodeKey}, ret...)
  68. return ret
  69. }
  70. /*
  71. Return the display string for a given attribute.
  72. */
  73. func (ni *defaultNodeInfo) AttributeDisplayString(kind string, attr string) string {
  74. if (attr == data.NodeKey || attr == data.NodeKind || attr == data.NodeName) && kind != "" {
  75. return stringutil.CreateDisplayString(kind) + " " +
  76. stringutil.CreateDisplayString(attr)
  77. }
  78. return stringutil.CreateDisplayString(attr)
  79. }
  80. /*
  81. Check if a given string can be a valid node attribute.
  82. */
  83. func (ni *defaultNodeInfo) IsValidAttr(attr string) bool {
  84. return ni.gm.IsValidAttr(attr)
  85. }