source.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package conf
  15. import (
  16. "github.com/lf-edge/ekuiper/internal/conf"
  17. "github.com/lf-edge/ekuiper/pkg/ast"
  18. "strings"
  19. )
  20. func GetSourceConf(sourceType string, options *ast.Options) map[string]interface{} {
  21. confkey := options.CONF_KEY
  22. yamlOps, err := conf.NewConfigOperatorFromSourceYaml(sourceType)
  23. if err != nil {
  24. conf.Log.Warnf("fail to parse yaml for source %s. Return error %v", sourceType, err)
  25. }
  26. props := make(map[string]interface{})
  27. cfg := yamlOps.CopyConfContent()
  28. if len(cfg) == 0 {
  29. conf.Log.Warnf("fail to parse yaml for source %s. Return an empty configuration", sourceType)
  30. } else {
  31. def, ok := cfg["default"]
  32. if !ok {
  33. conf.Log.Warnf("default conf %s is not found", confkey)
  34. } else {
  35. props = def
  36. if c, ok := cfg[strings.ToLower(confkey)]; ok {
  37. for k, v := range c {
  38. props[k] = v
  39. }
  40. }
  41. }
  42. }
  43. f := options.FORMAT
  44. if f == "" {
  45. f = "json"
  46. }
  47. props["format"] = strings.ToLower(f)
  48. props["key"] = strings.ToLower(options.KEY)
  49. conf.Log.Debugf("get conf for %s with conf key %s: %v", sourceType, confkey, printable(props))
  50. return props
  51. }
  52. func printable(m map[string]interface{}) map[string]interface{} {
  53. printableMap := make(map[string]interface{})
  54. for k, v := range m {
  55. if strings.EqualFold(k, "password") {
  56. printableMap[k] = "*"
  57. } else {
  58. if vm, ok := v.(map[string]interface{}); ok {
  59. printableMap[k] = printable(vm)
  60. } else {
  61. printableMap[k] = v
  62. }
  63. }
  64. }
  65. return printableMap
  66. }