Skip to content

Commit

Permalink
Add Join Table specifications
Browse files Browse the repository at this point in the history
  • Loading branch information
HuwCampbell committed Feb 18, 2024
1 parent 98d990d commit a218b0d
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
2 changes: 2 additions & 0 deletions client/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ type Table struct {
EventInfo *EventDescription `json:"eventDescription,omitempty"`
EntityMapping int `json:"entityMapping,omitempty"`
ExtraFeatures []int `json:"extraFeatures,omitempty"`
Base *int `json:"base,omitempty"`
Joins []int `json:"joins,omitempty"`
Labels []string `json:"labels"`
Attributes []Attribute `json:"attributes"`
}
Expand Down
92 changes: 91 additions & 1 deletion client/resource_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func ResourceTable() *schema.Resource {
Optional: true,
MaxItems: 1,
Elem: sourceSchema(),
ExactlyOneOf: []string{"source", "event_store", "expression", "entity_mapping"},
ExactlyOneOf: []string{"source", "event_store", "expression", "entity_mapping", "join"},
},
"event_store": {
Type: schema.TypeList,
Expand Down Expand Up @@ -148,12 +148,21 @@ func ResourceTable() *schema.Resource {
Elem: pointInTimeSchema(),
},

"join": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ConflictsWith: []string{"event", "scd2", "point_in_time"},
Elem: joinTableSchema(),
},

"entity_mapping": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateAnamlIdentifier(),
ConflictsWith: []string{"event", "scd2", "point_in_time"},
},

"extra_features": {
Type: schema.TypeList,
Description: "Tables upon which this view is created",
Expand Down Expand Up @@ -271,6 +280,29 @@ func pointInTimeSchema() *schema.Resource {
}
}

func joinTableSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"table": {
Type: schema.TypeString,
Description: "The root tables on the Left of the Join.",
Required: true,
ValidateFunc: validateAnamlIdentifier(),
},
"joins": {
Type: schema.TypeList,
Description: "Dimensions tables to join to.",
Optional: true,

Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validateAnamlIdentifier(),
},
},
},
}
}

func sourceSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -356,12 +388,20 @@ func resourceTableRead(d *schema.ResourceData, m interface{}) error {
if err := d.Set("source", flattenSourceReferences(table.Source)); err != nil {
return err
}
} else {
if err := d.Set("source", nil); err != nil {
return err
}
}

if table.Type == "eventstore" {
if err := d.Set("event_store", flattenEventStoreReferences(table.Source)); err != nil {
return err
}
} else {
if err := d.Set("event_store", nil); err != nil {
return err
}
}

if table.Type == "pivot" {
Expand All @@ -372,6 +412,24 @@ func resourceTableRead(d *schema.ResourceData, m interface{}) error {
if err := d.Set("extra_features", identifierList(table.ExtraFeatures)); err != nil {
return err
}
} else {
if err := d.Set("entity_mapping", nil); err != nil {
return err
}

if err := d.Set("extra_features", nil); err != nil {
return err
}
}

if table.Type == "join" {
if err := d.Set("join", flattenJoinTableSpecification(table)); err != nil {
return err
}
} else {
if err := d.Set("join", nil); err != nil {
return err
}
}

if err := d.Set("labels", table.Labels); err != nil {
Expand Down Expand Up @@ -441,6 +499,11 @@ func buildTable(d *schema.ResourceData) *Table {
} else if len(d.Get("source").([]interface{})) == 1 {
table.Type = "root"
table.Source = expandSourceReferences(d)
} else if len(d.Get("join").([]interface{})) == 1 {
table.Type = "join"
base, others := expandJoinSpecification(d)
table.Base = base
table.Joins = others
} else {
table.Type = "eventstore"
table.Source = expandEventStoreReferences(d)
Expand Down Expand Up @@ -631,6 +694,19 @@ func expandEventStoreReferences(d *schema.ResourceData) *SourceReference {
return nil
}

func expandJoinSpecification(d *schema.ResourceData) (*int, []int) {
srs := d.Get("join").([]interface{})

for _, sr := range srs {
val, _ := sr.(map[string]interface{})
store, _ := strconv.Atoi(val["table"].(string))
joinList := expandIdentifierList(val["joins"].([]interface{}))
return &store, joinList
}

return nil, nil
}

func flattenSourceReferences(source *SourceReference) []map[string]interface{} {
res := make([]map[string]interface{}, 0, 1)

Expand Down Expand Up @@ -663,3 +739,17 @@ func flattenEventStoreReferences(source *SourceReference) []map[string]interface

return res
}

func flattenJoinTableSpecification(table *Table) []map[string]interface{} {
res := make([]map[string]interface{}, 0, 1)
if table.Base == nil {
return res
}

single := make(map[string]interface{})
single["table"] = strconv.Itoa(*table.Base)
single["joins"] = identifierList(table.Joins)
res = append(res, single)

return res
}

0 comments on commit a218b0d

Please sign in to comment.