|
| 1 | +package tap |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +// OutputType is the type of Singer tap output for each message. |
| 11 | +type OutputType string |
| 12 | + |
| 13 | +var ( |
| 14 | + OutputTypeSchema OutputType = "SCHEMA" |
| 15 | + OutputTypeRecord OutputType = "RECORD" |
| 16 | +) |
| 17 | + |
| 18 | +// Output is what we log to STDOUT as a message provided to the downstream Singer target. |
| 19 | +// |
| 20 | +// This tap supports type types of output: |
| 21 | +// |
| 22 | +// - SCHEMA: Specifies the schema of this stream in JSON schema format. |
| 23 | +// - RECORD: A record from the stream. |
| 24 | +// |
| 25 | +// We (currently) do not support the other types of output such as STATE. |
| 26 | +type Output struct { |
| 27 | + // Type is the type of the stream, e.g. "SCHEMA" or "RECORD" |
| 28 | + Type OutputType `json:"type,omitempty"` |
| 29 | + // Stream is the name of the stream, e.g. "users" |
| 30 | + Stream string `json:"stream,omitempty"` |
| 31 | + // Schema is the schema of the stream, if Type == "SCHEMA", in JSON schema format. |
| 32 | + Schema *Schema `json:"schema,omitempty"` |
| 33 | + // Record is a record from the stream, if Type == "RECORD". |
| 34 | + Record map[string]any `json:"record,omitempty"` |
| 35 | + // KeyProperties is a list of strings indicating which properties make up the primary |
| 36 | + // key for this stream. |
| 37 | + // |
| 38 | + // Each item in the list must be the name of a top-level property defined in the schema. |
| 39 | + // A value for key_properties must be provided, but it may be an empty list to indicate |
| 40 | + // that there is no primary key. |
| 41 | + KeyProperties []string `json:"key_properties,omitempty"` |
| 42 | + // BookmarkProperties is an optional list of strings indicating which properties |
| 43 | + // should be used to bookmark the stream, such as "last_updated_at". |
| 44 | + BookmarkProperties []string `json:"bookmark_properties,omitempty"` |
| 45 | +} |
| 46 | + |
| 47 | +// Schema is a JSON schema for a stream. |
| 48 | +type Schema struct { |
| 49 | + // Type is the type of the schema, e.g. "object" |
| 50 | + Type []string `json:"type"` |
| 51 | + // HasAdditionalProperties indicates whether the schema allows additional properties |
| 52 | + // not defined in the schema. |
| 53 | + HasAdditionalProperties bool `json:"additionalProperties"` |
| 54 | + // Properties is a map of property names to their schema. |
| 55 | + Properties map[string]Property `json:"properties"` |
| 56 | +} |
| 57 | + |
| 58 | +// Property is a property in a JSON schema. |
| 59 | +type Property struct { |
| 60 | + // Types is a list of types that this property can be, e.g. "string" or "integer". |
| 61 | + Types []string `json:"type"` |
| 62 | + // CustomFormat is a custom format for this property, e.g. "date-time". |
| 63 | + CustomFormat string `json:"format,omitempty"` |
| 64 | +} |
| 65 | + |
| 66 | +func (s Property) IsBoolean() bool { |
| 67 | + return s.hasType("boolean") |
| 68 | +} |
| 69 | + |
| 70 | +func (s Property) IsNumber() bool { |
| 71 | + return s.hasType("number") |
| 72 | +} |
| 73 | + |
| 74 | +func (s Property) IsInteger() bool { |
| 75 | + return s.hasType("integer") |
| 76 | +} |
| 77 | + |
| 78 | +func (s Property) hasType(typeName string) bool { |
| 79 | + for _, t := range s.Types { |
| 80 | + if strings.EqualFold(t, typeName) { |
| 81 | + return true |
| 82 | + } |
| 83 | + } |
| 84 | + return false |
| 85 | +} |
| 86 | + |
| 87 | +func (s Property) IsDateTime() bool { |
| 88 | + return s.CustomFormat == "date-time" |
| 89 | +} |
| 90 | + |
| 91 | +// OutputLogger is a logger that logs to STDOUT in the format expected by the downstream |
| 92 | +// Singer target. |
| 93 | +type OutputLogger struct { |
| 94 | + w io.Writer |
| 95 | +} |
| 96 | + |
| 97 | +func NewOutputLogger(w io.Writer) *OutputLogger { |
| 98 | + return &OutputLogger{w: w} |
| 99 | +} |
| 100 | + |
| 101 | +func (o *OutputLogger) Log(op *Output) error { |
| 102 | + data, err := json.Marshal(op) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + |
| 107 | + _, err = fmt.Fprintln(o.w, string(data)) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + |
| 112 | + return nil |
| 113 | +} |
0 commit comments