parseSource parses a source definition and adds the resulting resource to p.Resources.
(ctx context.Context, node *Node)
| 12 | |
| 13 | // parseSource parses a source definition and adds the resulting resource to p.Resources. |
| 14 | func (p *Parser) parseSource(ctx context.Context, node *Node) error { |
| 15 | // Parse YAML |
| 16 | tmp := make(map[string]any) |
| 17 | if node.YAML == nil { |
| 18 | node.YAML = &yaml.Node{} |
| 19 | } |
| 20 | err := node.YAML.Decode(tmp) |
| 21 | if err != nil { |
| 22 | return err |
| 23 | } |
| 24 | |
| 25 | // Backwards compatibility: "type:" was previously used instead of "connector:". |
| 26 | // So if "type:" is not a valid resource kind, we treat it as a connector. |
| 27 | if typ, ok := tmp["type"].(string); ok { |
| 28 | if _, err := ParseResourceKind(typ); err != nil { |
| 29 | node.Connector = typ |
| 30 | node.ConnectorInferred = false |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | tmp["type"] = "model" |
| 35 | tmp["defined_as_source"] = true |
| 36 | tmp["materialize"] = true |
| 37 | if _, ok := tmp["output"]; !ok { |
| 38 | tmp["output"] = map[string]any{"connector": p.defaultOLAPConnector()} |
| 39 | } |
| 40 | |
| 41 | // Backward compatibility: when the default connector is "olap", and it's a DuckDB connector, a source with connector "duckdb" should run on it |
| 42 | if p.DefaultOLAPConnector == "olap" && node.Connector == "duckdb" { |
| 43 | node.Connector = "olap" |
| 44 | } |
| 45 | |
| 46 | // Validate the source has a connector |
| 47 | if node.ConnectorInferred { |
| 48 | return fmt.Errorf("must explicitly specify a connector for sources") |
| 49 | } |
| 50 | |
| 51 | // Convert back to YAML |
| 52 | err = node.YAML.Encode(tmp) |
| 53 | if err != nil { |
| 54 | return err |
| 55 | } |
| 56 | bytes, err := yaml.Marshal(node.YAML) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | node.YAMLRaw = string(bytes) |
| 61 | |
| 62 | // NOTE: Not changing node.Kind such that the call to decodeNodeYAML in parseModel still applies the correct project-wide defaults. |
| 63 | |
| 64 | // We allowed a special resource type (source) to ingest data from external connector. |
| 65 | // After the unification of sources and models everything is a model. |
| 66 | return p.parseModel(ctx, node) |
| 67 | } |
no test coverage detected