from.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package help
  2. import "reflect"
  3. // Iterator is an alias for function to iterate over data.
  4. type Iterator func() (item interface{}, ok bool)
  5. // Query is the type returned from query functions. It can be iterated manually
  6. // as shown in the example.
  7. type Query struct {
  8. Iterate func() Iterator
  9. }
  10. // KeyValue is a type that is used to iterate over a map (if query is created
  11. // from a map). This type is also used by ToMap() method to output result of a
  12. // query into a map.
  13. type KeyValue struct {
  14. Key interface{}
  15. Value interface{}
  16. }
  17. // Iterable is an interface that has to be implemented by a custom collection in
  18. // order to work with linq.
  19. type Iterable interface {
  20. Iterate() Iterator
  21. }
  22. // From initializes a linq query with passed slice, array or map as the source.
  23. // String, channel or struct implementing Iterable interface can be used as an
  24. // input. In this case From delegates it to FromString, FromChannel and
  25. // FromIterable internally.
  26. func From(source interface{}) Query {
  27. src := reflect.ValueOf(source)
  28. switch src.Kind() {
  29. case reflect.Slice, reflect.Array:
  30. len := src.Len()
  31. return Query{
  32. Iterate: func() Iterator {
  33. index := 0
  34. return func() (item interface{}, ok bool) {
  35. ok = index < len
  36. if ok {
  37. item = src.Index(index).Interface()
  38. index++
  39. }
  40. return
  41. }
  42. },
  43. }
  44. case reflect.Map:
  45. len := src.Len()
  46. return Query{
  47. Iterate: func() Iterator {
  48. index := 0
  49. keys := src.MapKeys()
  50. return func() (item interface{}, ok bool) {
  51. ok = index < len
  52. if ok {
  53. key := keys[index]
  54. item = KeyValue{
  55. Key: key.Interface(),
  56. Value: src.MapIndex(key).Interface(),
  57. }
  58. index++
  59. }
  60. return
  61. }
  62. },
  63. }
  64. case reflect.String:
  65. return FromString(source.(string))
  66. case reflect.Chan:
  67. if _, ok := source.(chan interface{}); ok {
  68. return FromChannel(source.(chan interface{}))
  69. } else {
  70. return FromChannelT(source)
  71. }
  72. default:
  73. return FromIterable(source.(Iterable))
  74. }
  75. }
  76. // FromChannel initializes a linq query with passed channel, linq iterates over
  77. // channel until it is closed.
  78. func FromChannel(source <-chan interface{}) Query {
  79. return Query{
  80. Iterate: func() Iterator {
  81. return func() (item interface{}, ok bool) {
  82. item, ok = <-source
  83. return
  84. }
  85. },
  86. }
  87. }
  88. // FromChannelT is the typed version of FromChannel.
  89. //
  90. // - source is of type "chan TSource"
  91. //
  92. // NOTE: FromChannel has better performance than FromChannelT.
  93. func FromChannelT(source interface{}) Query {
  94. src := reflect.ValueOf(source)
  95. return Query{
  96. Iterate: func() Iterator {
  97. return func() (interface{}, bool) {
  98. value, ok := src.Recv()
  99. return value.Interface(), ok
  100. }
  101. },
  102. }
  103. }
  104. // FromString initializes a linq query with passed string, linq iterates over
  105. // runes of string.
  106. func FromString(source string) Query {
  107. runes := []rune(source)
  108. len := len(runes)
  109. return Query{
  110. Iterate: func() Iterator {
  111. index := 0
  112. return func() (item interface{}, ok bool) {
  113. ok = index < len
  114. if ok {
  115. item = runes[index]
  116. index++
  117. }
  118. return
  119. }
  120. },
  121. }
  122. }
  123. // FromIterable initializes a linq query with custom collection passed. This
  124. // collection has to implement Iterable interface, linq iterates over items,
  125. // that has to implement Comparable interface or be basic types.
  126. func FromIterable(source Iterable) Query {
  127. return Query{
  128. Iterate: source.Iterate,
  129. }
  130. }
  131. // Range generates a sequence of integral numbers within a specified range.
  132. func Range(start, count int) Query {
  133. return Query{
  134. Iterate: func() Iterator {
  135. index := 0
  136. current := start
  137. return func() (item interface{}, ok bool) {
  138. if index >= count {
  139. return nil, false
  140. }
  141. item, ok = current, true
  142. index++
  143. current++
  144. return
  145. }
  146. },
  147. }
  148. }
  149. // Repeat generates a sequence that contains one repeated value.
  150. func Repeat(value interface{}, count int) Query {
  151. return Query{
  152. Iterate: func() Iterator {
  153. index := 0
  154. return func() (item interface{}, ok bool) {
  155. if index >= count {
  156. return nil, false
  157. }
  158. item, ok = value, true
  159. index++
  160. return
  161. }
  162. },
  163. }
  164. }