|
| 1 | +/* |
| 2 | +Copyright 2024. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package transformers |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "fmt" |
| 23 | + "strings" |
| 24 | + "time" |
| 25 | + |
| 26 | + v1 "github.com/dataflow-operator/dataflow/api/v1" |
| 27 | + "github.com/dataflow-operator/dataflow/internal/types" |
| 28 | + "github.com/tidwall/sjson" |
| 29 | +) |
| 30 | + |
| 31 | +const ( |
| 32 | + insertFieldNowPlaceholder = "${now}" |
| 33 | + insertFieldMetadataPrefix = "${metadata." |
| 34 | + insertFieldMetadataSuffix = "}" |
| 35 | + insertFieldJSONPrefix = "json:" |
| 36 | +) |
| 37 | + |
| 38 | +// InsertFieldTransformer inserts or overwrites JSON fields with literals and placeholders. |
| 39 | +type InsertFieldTransformer struct { |
| 40 | + config *v1.InsertFieldTransformation |
| 41 | +} |
| 42 | + |
| 43 | +// NewInsertFieldTransformer creates a new insertField transformer. |
| 44 | +func NewInsertFieldTransformer(config *v1.InsertFieldTransformation) *InsertFieldTransformer { |
| 45 | + return &InsertFieldTransformer{config: config} |
| 46 | +} |
| 47 | + |
| 48 | +// Transform inserts configured fields into the JSON payload. |
| 49 | +// Non-JSON payloads are passed through unchanged. Metadata is preserved. |
| 50 | +// Missing metadata keys resolve to an empty string. Invalid json: values return an error. |
| 51 | +func (t *InsertFieldTransformer) Transform(ctx context.Context, message *types.Message) ([]*types.Message, error) { |
| 52 | + if _, ok := tryUnmarshalJSON(message); !ok { |
| 53 | + return []*types.Message{message}, nil |
| 54 | + } |
| 55 | + |
| 56 | + jsonStr := string(message.Data) |
| 57 | + for path, rawValue := range t.config.Fields { |
| 58 | + normalized := normalizeFieldPath(strings.TrimSpace(path)) |
| 59 | + if normalized == "" { |
| 60 | + continue |
| 61 | + } |
| 62 | + |
| 63 | + value, asRaw, err := resolveInsertFieldValue(rawValue, message.Metadata) |
| 64 | + if err != nil { |
| 65 | + return nil, fmt.Errorf("insertField %q: %w", path, err) |
| 66 | + } |
| 67 | + |
| 68 | + if asRaw { |
| 69 | + jsonStr, err = sjson.SetRaw(jsonStr, normalized, value.(string)) |
| 70 | + } else { |
| 71 | + jsonStr, err = sjson.Set(jsonStr, normalized, value) |
| 72 | + } |
| 73 | + if err != nil { |
| 74 | + return nil, fmt.Errorf("insertField set %q: %w", path, err) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return []*types.Message{newMessageFrom(message, []byte(jsonStr))}, nil |
| 79 | +} |
| 80 | + |
| 81 | +// resolveInsertFieldValue resolves a configured field value. |
| 82 | +// Returns asRaw=true when the value should be written via sjson.SetRaw (json: prefix). |
| 83 | +func resolveInsertFieldValue(raw string, meta map[string]interface{}) (value interface{}, asRaw bool, err error) { |
| 84 | + switch { |
| 85 | + case raw == insertFieldNowPlaceholder: |
| 86 | + return time.Now().Format(time.RFC3339), false, nil |
| 87 | + case strings.HasPrefix(raw, insertFieldMetadataPrefix) && strings.HasSuffix(raw, insertFieldMetadataSuffix): |
| 88 | + key := raw[len(insertFieldMetadataPrefix) : len(raw)-len(insertFieldMetadataSuffix)] |
| 89 | + return formatInsertFieldMetadata(meta, key), false, nil |
| 90 | + case strings.HasPrefix(raw, insertFieldJSONPrefix): |
| 91 | + jsonRaw := raw[len(insertFieldJSONPrefix):] |
| 92 | + if !json.Valid([]byte(jsonRaw)) { |
| 93 | + return nil, false, fmt.Errorf("invalid json value %q", jsonRaw) |
| 94 | + } |
| 95 | + return jsonRaw, true, nil |
| 96 | + default: |
| 97 | + return raw, false, nil |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func formatInsertFieldMetadata(meta map[string]interface{}, key string) string { |
| 102 | + if meta == nil || key == "" { |
| 103 | + return "" |
| 104 | + } |
| 105 | + v, ok := meta[key] |
| 106 | + if !ok || v == nil { |
| 107 | + return "" |
| 108 | + } |
| 109 | + switch t := v.(type) { |
| 110 | + case string: |
| 111 | + return t |
| 112 | + case []byte: |
| 113 | + return string(t) |
| 114 | + case time.Time: |
| 115 | + return t.UTC().Format(time.RFC3339Nano) |
| 116 | + default: |
| 117 | + return fmt.Sprint(t) |
| 118 | + } |
| 119 | +} |
0 commit comments