1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
|
package data
import (
"bytes"
"github.com/viant/toolbox"
"log"
"strings"
"time"
)
//Map types is an alias type to map[string]interface{} with extended behaviours
type Map map[string]interface{}
//Udf represents a user defined function used to transform data.
type Udf func(interface{}, Map) (interface{}, error)
//Put puts key value into the map.
func (s *Map) Put(key string, value interface{}) {
(*s)[key] = value
}
//Delete removes the supplied keys, it supports key of path expression with dot i.e. request.method
func (s *Map) Delete(keys ...string) {
for _, key := range keys {
if !strings.Contains(key, ".") {
delete(*s, key)
continue
}
keyParts := strings.Split(key, ".")
var temp = *s
for i, part := range keyParts {
if temp == nil {
break
}
isLasPart := i+1 == len(keyParts)
if isLasPart {
delete(temp, part)
} else if temp[part] != nil && toolbox.IsMap(temp[part]) {
subMap := toolbox.AsMap(temp[part])
temp = Map(subMap)
} else {
break
}
}
}
}
//Replace replaces supplied key/path with corresponding value
func (s *Map) Replace(key, val string) {
if !strings.Contains(key, ".") {
(*s)[key] = val
return
}
keyParts := strings.Split(key, ".")
var temp = *s
for i, part := range keyParts {
if temp == nil {
break
}
isLasPart := i+1 == len(keyParts)
if isLasPart {
temp[part] = val
} else if temp[part] != nil && toolbox.IsMap(temp[part]) {
subMap := toolbox.AsMap(temp[part])
temp = Map(subMap)
} else {
break
}
}
}
//Has returns true if the provided key is present
func (s *Map) Has(key string) bool {
_, found := (*s)[key]
return found
}
//Get returns a value for provided key
func (s *Map) Get(key string) interface{} {
if result, found := (*s)[key]; found {
return result
}
return nil
}
/*
GetValue returns value for provided expression.
The expression uses dot (.) to denote nested data structure.
The following expression as supported
1) <-key shift
2) ++key pre increment
3) key++ post increment
4) $key reference access
*/
func (s *Map) GetValue(expr string) (interface{}, bool) {
if expr == "" {
return nil, false
}
if strings.HasPrefix(expr, "{") && strings.HasSuffix(expr, "}") {
expr = expr[1 : len(expr)-1]
}
isShiftOperation := strings.HasPrefix(expr, "<-")
if isShiftOperation {
expr = string(expr[2:])
}
isPostIncrementOperation := strings.HasSuffix(expr, "++")
if isPostIncrementOperation {
expr = string(expr[:len(expr)-2])
}
isPreIncrementOperation := strings.HasPrefix(expr, "++")
if isPreIncrementOperation {
expr = string(expr[2:])
}
isReference := strings.HasPrefix(expr, "$")
if isReference {
expr = string(expr[1:])
expr = s.GetString(expr)
if expr == "" {
return nil, false
}
}
state := *s
if strings.Contains(expr, ".") || strings.HasSuffix(expr, "]") {
fragments := strings.Split(expr, ".")
for i, fragment := range fragments {
var index interface{}
arrayIndexPosition := strings.Index(fragment, "[")
if arrayIndexPosition != -1 {
arrayEndPosition := strings.Index(fragment, "]")
if arrayEndPosition > arrayIndexPosition && arrayEndPosition < len(fragment) {
arrayIndex := string(fragment[arrayIndexPosition+1 : arrayEndPosition])
index = arrayIndex
fragment = string(fragment[:arrayIndexPosition])
}
}
isLast := i+1 == len(fragments)
hasKey := state.Has(fragment)
if !hasKey {
return nil, false
}
var candidate = state.Get(fragment)
if !isLast && candidate == nil {
return nil, false
}
if index != nil {
if intIndex, err := toolbox.ToInt(index); err == nil {
if !toolbox.IsSlice(candidate) {
return nil, false
}
var aSlice = toolbox.AsSlice(candidate)
if intIndex >= len(aSlice) {
return nil, false
}
if intIndex < len(aSlice) {
candidate = aSlice[intIndex]
} else {
candidate = nil
}
} else if textIndex, ok := index.(string); ok {
if !toolbox.IsMap(candidate) {
return nil, false
}
aMap := toolbox.AsMap(candidate)
if candidate, ok = aMap[textIndex]; !ok {
return nil, false
}
} else {
return nil, false
}
if isLast {
return candidate, true
}
}
if isLast {
expr = fragment
continue
}
if toolbox.IsMap(candidate) {
newState := toolbox.AsMap(candidate)
if newState != nil {
state = newState
}
} else {
value, _ := state.GetValue(fragment)
if f, ok := value.(func(key string) interface{}); ok {
return f(fragments[i+1]), true
}
return nil, false
}
}
}
if state.Has(expr) {
var result = state.Get(expr)
if isPostIncrementOperation {
state.Put(expr, toolbox.AsInt(result)+1)
} else if isPreIncrementOperation {
result = toolbox.AsInt(result) + 1
state.Put(expr, result)
} else if isShiftOperation {
aCollection := state.GetCollection(expr)
if len(*aCollection) == 0 {
return nil, false
}
var result = (*aCollection)[0]
var newCollection = (*aCollection)[1:]
state.Put(expr, &newCollection)
return result, true
}
if f, ok := result.(func() interface{}); ok {
return f(), true
}
return result, true
}
return nil, false
}
/*
Set value sets value in the map for the supplied expression.
The expression uses dot (.) to denote nested data structure. For instance the following expression key1.subKey1.attr1
Would take or create map for key1, followied bu takeing or creating antother map for subKey1 to set attr1 key with provided value
The following expression as supported
1) $key reference - the final key is determined from key's content.
2) ->key push expression to append value at the end of the slice
*/
func (s *Map) SetValue(expr string, value interface{}) {
if expr == "" {
return
}
if value == nil {
return
}
if strings.Index(expr, "$") != -1 {
expr = s.ExpandAsText(expr)
}
state := *s
isPushOperation := strings.HasPrefix(expr, "->")
if isPushOperation {
expr = string(expr[2:])
}
if strings.HasPrefix(expr, "{") && strings.HasSuffix(expr, "}") {
expr = expr[1 : len(expr)-1]
}
if strings.Contains(expr, ".") {
fragments := strings.Split(expr, ".")
nodePath := strings.Join(fragments[:len(fragments)-1], ".")
if node, ok := s.GetValue(nodePath); ok && toolbox.IsMap(node) {
if _, writable := node.(map[string]interface{}); !writable {
node = Map(toolbox.AsMap(node))
s.SetValue(nodePath, node)
}
expr = fragments[len(fragments)-1]
state = Map(toolbox.AsMap(node))
} else {
for i, fragment := range fragments {
isLast := i+1 == len(fragments)
if isLast {
expr = fragment
} else {
subState := state.GetMap(fragment)
if subState == nil {
subState = NewMap()
state.Put(fragment, subState)
}
state = subState
}
}
}
}
if isPushOperation {
collection := state.GetCollection(expr)
if collection == nil {
collection = NewCollection()
state.Put(expr, collection)
}
collection.Push(value)
state.Put(expr, collection)
return
}
state.Put(expr, value)
}
//Apply copies all elements of provided map to this map.
func (s *Map) Apply(source map[string]interface{}) {
for k, v := range source {
(*s)[k] = v
}
}
//GetString returns value for provided key as string.
func (s *Map) GetString(key string) string {
if result, found := (*s)[key]; found {
return toolbox.AsString(result)
}
return ""
}
//GetInt returns value for provided key as int.
func (s *Map) GetInt(key string) int {
if result, found := (*s)[key]; found {
return toolbox.AsInt(result)
}
return 0
}
//GetFloat returns value for provided key as float64.
func (s *Map) GetFloat(key string) float64 {
if result, found := (*s)[key]; found {
return toolbox.AsFloat(result)
}
return 0.0
}
//GetBoolean returns value for provided key as boolean.
func (s *Map) GetBoolean(key string) bool {
if result, found := (*s)[key]; found {
return toolbox.AsBoolean(result)
}
return false
}
//GetCollection returns value for provided key as collection pointer.
func (s *Map) GetCollection(key string) *Collection {
if result, found := (*s)[key]; found {
collectionPointer, ok := result.(*Collection)
if ok {
return collectionPointer
}
aSlice, ok := result.([]interface{})
collection := Collection(aSlice)
if ok {
return &collection
}
if !toolbox.IsSlice(result) {
return nil
}
aSlice = toolbox.AsSlice(result)
collection = Collection(aSlice)
return &collection
}
return nil
}
//GetMap returns value for provided key as map.
func (s *Map) GetMap(key string) Map {
if result, found := (*s)[key]; found {
aMap, ok := result.(Map)
if ok {
return aMap
}
aMap, ok = result.(map[string]interface{})
if ok {
return aMap
}
var result = toolbox.AsMap(result)
(*s)[key] = result
return result
}
return nil
}
//Range iterates every key, value pair of this map, calling supplied callback as long it does return true.
func (s *Map) Range(callback func(k string, v interface{}) (bool, error)) error {
for k, v := range *s {
next, err := callback(k, v)
if err != nil {
return err
}
if !next {
break
}
}
return nil
}
//Clones create a clone of this map.
func (s *Map) Clone() Map {
var result = NewMap()
for key, value := range *s {
if aMap, casted := value.(Map); casted {
result[key] = aMap.Clone()
} else {
result[key] = value
}
}
return result
}
//Returns a map that can be encoded by json or other decoders.
//Since a map can store a function, it filters out any non marshalable types.
func (s *Map) AsEncodableMap() map[string]interface{} {
var result = make(map[string]interface{})
for k, v := range *s {
if v == nil {
continue
}
result[k] = asEncodableValue(v)
}
return result
}
//asEncodableValue returns all non func values or func() literal for function.
func asEncodableValue(v interface{}) interface{} {
if v == nil {
return nil
}
value := v
if toolbox.IsFunc(v) {
return "func()"
}
if toolbox.IsMap(v) {
var aMap = Map(toolbox.AsMap(v))
value = aMap.AsEncodableMap()
} else if toolbox.IsSlice(v) {
var targetSlice = make([]interface{}, 0)
var sourceSlice = toolbox.AsSlice(v)
for _, item := range sourceSlice {
targetSlice = append(targetSlice, asEncodableValue(item))
}
value = targetSlice
} else if toolbox.IsString(v) || toolbox.IsInt(v) || toolbox.IsFloat(v) {
value = v
} else {
value = toolbox.AsString(v)
}
return value
}
func hasGenericKeys(aMap map[string]interface{}) bool {
for k := range aMap {
if strings.HasPrefix(k, "$AsInt") || strings.HasPrefix(k, "$AsFloat") || strings.HasPrefix(k, "$AsBool") {
return true
}
}
return false
}
//Expand expands provided value of any type with dollar sign expression/
func (s *Map) Expand(source interface{}) interface{} {
switch value := source.(type) {
case bool, []byte, int, uint, int8, int16, int32, int64, uint8, uint16, uint32, uint64, float32, float64, time.Time:
return source
case *[]byte:
return s.expandExpressions(string(*value))
case *string:
if value == nil {
return ""
}
return s.expandExpressions(*value)
case string:
return s.expandExpressions(value)
case map[string]interface{}:
if hasGenericKeys(value) {
result := make(map[interface{}]interface{})
for k, v := range value {
var key = s.Expand(k)
result[key] = s.Expand(v)
}
return result
}
resultMap := make(map[string]interface{})
for k, v := range value {
var key = s.ExpandAsText(k)
var expanded = s.Expand(v)
if key == "..." {
if expanded != nil && toolbox.IsMap(expanded) {
for key, value := range toolbox.AsMap(expanded) {
resultMap[key] = value
}
continue
}
}
resultMap[key] = expanded
}
return resultMap
case map[interface{}]interface{}:
var resultMap = make(map[interface{}]interface{})
for k, v := range value {
var key = s.Expand(k)
var expanded = s.Expand(v)
if key == "..." {
if expanded != nil && toolbox.IsMap(expanded) {
for key, value := range toolbox.AsMap(expanded) {
resultMap[key] = value
}
continue
}
}
resultMap[key] = expanded
}
return resultMap
case []interface{}:
var resultSlice = make([]interface{}, len(value))
for i, value := range value {
resultSlice[i] = s.Expand(value)
}
return resultSlice
default:
if source == nil {
return nil
}
if toolbox.IsMap(source) {
switch aMap := value.(type) {
case map[string]interface{}:
return s.Expand(aMap)
case map[interface{}]interface{}:
return s.Expand(aMap)
default:
return s.Expand(toolbox.AsMap(value))
}
} else if toolbox.IsSlice(source) {
return s.Expand(toolbox.AsSlice(value))
} else if toolbox.IsStruct(value) {
aMap := toolbox.AsMap(value)
return s.Expand(aMap)
} else if value != nil {
return s.Expand(toolbox.AsString(value))
}
}
return source
}
//ExpandAsText expands all matching expressions starting with dollar sign ($)
func (s *Map) ExpandAsText(text string) string {
result := s.expandExpressions(text)
if toolbox.IsSlice(result) || toolbox.IsMap(result) {
buf := new(bytes.Buffer)
err := toolbox.NewJSONEncoderFactory().Create(buf).Encode(result)
if err == nil {
return buf.String()
}
}
if text, ok := result.(string); ok || result == nil {
return text
}
return toolbox.AsString(result)
}
func (s *Map) evaluateUDF(candidate interface{}, argument interface{}) (interface{}, bool) {
var canExpandAll = true
if toolbox.IsString(argument) {
var expandable = strings.TrimSpace(toolbox.AsString(argument))
Parse(expandable, func(expression string, udf bool, argument interface{}) (interface{}, bool) {
if _, has := s.GetValue(string(expression[1:])); !has {
canExpandAll = false
}
return nil, false
})
}
if !canExpandAll {
return nil, false
}
udf, ok := candidate.(func(interface{}, Map) (interface{}, error))
if !ok {
return nil, false
}
expandedArgument := s.expandArgumentsExpressions(argument)
if toolbox.IsString(expandedArgument) {
expandedText := toolbox.AsString(expandedArgument)
if toolbox.IsStructuredJSON(expandedText) {
evaluated, err := toolbox.JSONToInterface(expandedText)
if err != nil {
return nil, false
}
expandedArgument = evaluated
}
}
evaluated, err := udf(expandedArgument, *s)
if err == nil {
return evaluated, true
}
log.Printf("failed to evaluate %v, %v", candidate, err)
return nil, false
}
func (s *Map) hasCycle(source interface{}, ownerVariable string) bool {
switch value := source.(type) {
case string:
return strings.Contains(value, ownerVariable)
case Map:
for k, v := range value {
if s.hasCycle(k, ownerVariable) || s.hasCycle(v, ownerVariable) {
return true
}
}
case map[string]interface{}:
for k, v := range value {
if s.hasCycle(k, ownerVariable) || s.hasCycle(v, ownerVariable) {
return true
}
}
case []interface{}:
for _, v := range value {
if s.hasCycle(v, ownerVariable) {
return true
}
}
case Collection:
for _, v := range value {
if s.hasCycle(v, ownerVariable) {
return true
}
}
}
return false
}
//expandExpressions will check provided text with any expression starting with dollar sign ($) to substitute it with key in the map if it is present.
//The result can be an expanded text or type of key referenced by the expression.
func (s *Map) expandExpressions(text string) interface{} {
if strings.Index(text, "$") == -1 {
return text
}
var expandVariable = func(expression string, isUDF bool, argument interface{}) (interface{}, bool) {
value, hasExpValue := s.GetValue(string(expression[1:]))
if hasExpValue {
if value != expression && s.hasCycle(value, expression) {
log.Printf("detected data cycle on %v in value: %v", expression, value)
return expression, true
}
if isUDF {
if evaluated, ok := s.evaluateUDF(value, argument); ok {
return evaluated, true
}
} else {
if value != nil && (toolbox.IsMap(value) || toolbox.IsSlice(value)) {
return s.Expand(value), true
}
if text, ok := value.(string); ok {
return text, true
}
if value != nil {
return toolbox.DereferenceValue(value), true
}
return value, true
}
}
if isUDF {
expandedArgument := s.expandArgumentsExpressions(argument)
_, isByteArray := expandedArgument.([]byte)
if !toolbox.IsMap(expandedArgument) && !toolbox.IsSlice(expandedArgument) || isByteArray {
argument = toolbox.AsString(expandedArgument)
}
return expression + "(" + toolbox.AsString(argument) + ")", true
}
return expression, true
}
return Parse(text, expandVariable)
}
//expandExpressions will check provided text with any expression starting with dollar sign ($) to substitute it with key in the map if it is present.
//The result can be an expanded text or type of key referenced by the expression.
func (s *Map) expandArgumentsExpressions(argument interface{}) interface{} {
if argument == nil || !toolbox.IsString(argument) {
return argument
}
argumentLiteral, ok := argument.(string)
if ok {
if toolbox.IsStructuredJSON(argumentLiteral) {
return s.expandExpressions(argumentLiteral)
}
}
var expandVariable = func(expression string, isUDF bool, argument interface{}) (interface{}, bool) {
value, hasExpValue := s.GetValue(string(expression[1:]))
if hasExpValue {
if value != expression && s.hasCycle(value, expression) {
log.Printf("detected data cycle on %v in value: %v", expression, value)
return expression, true
}
if isUDF {
if evaluated, ok := s.evaluateUDF(value, argument); ok {
return evaluated, true
}
} else {
if value != nil && (toolbox.IsMap(value) || toolbox.IsSlice(value)) {
return s.Expand(value), true
}
if text, ok := value.(string); ok {
return text, true
}
if value != nil {
return toolbox.DereferenceValue(value), true
}
return value, true
}
}
if isUDF {
expandedArgument := s.expandArgumentsExpressions(argument)
_, isByteArray := expandedArgument.([]byte)
if !toolbox.IsMap(expandedArgument) && !toolbox.IsSlice(expandedArgument) || isByteArray {
argument = toolbox.AsString(expandedArgument)
}
expression = expression + "(" + toolbox.AsString(argument) + ")"
return expression, true
}
return expression, true
}
tokenizer := toolbox.NewTokenizer(argumentLiteral, invalidToken, eofToken, matchers)
var result = make([]interface{}, 0)
for tokenizer.Index < len(argumentLiteral) {
match, err := toolbox.ExpectTokenOptionallyFollowedBy(tokenizer, whitespace, "expected argument", doubleQuoteEnclosedToken, comaToken, unmatchedToken, eofToken)
if err != nil {
return Parse(argumentLiteral, expandVariable)
}
switch match.Token {
case doubleQuoteEnclosedToken:
result = append(result, strings.Trim(match.Matched, `"`))
case comaToken:
result = append(result, match.Matched)
tokenizer.Index++
case unmatchedToken:
result = append(result, match.Matched)
}
}
for i, arg := range result {
textArg, ok := arg.(string)
if !ok {
continue
}
textArg = strings.Trim(textArg, "'")
result[i] = Parse(textArg, expandVariable)
}
if len(result) == 1 {
return result[0]
}
return result
}
//NewMap creates a new instance of a map.
func NewMap() Map {
return make(map[string]interface{})
}
|