Skip to content

Commit

Permalink
fix: gen tools response links for to-many fields (#1620)
Browse files Browse the repository at this point in the history
  • Loading branch information
davenewza authored Sep 30, 2024
1 parent 16bd1d9 commit f54ec65
Show file tree
Hide file tree
Showing 7 changed files with 707 additions and 24 deletions.
39 changes: 38 additions & 1 deletion tools/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ func (g *Generator) decorateTools() error {
}
}
}

// for all responses that have a link for to-many fields,
// decorate the data mapping now that we have all inputs and responses generated
for _, response := range tool.Config.Response {
if !tool.Action.IsArbitraryFunction() && response.Link != nil && response.Link.ToolId != "" && response.Link.Data[0].Path == nil {
response.Link.Data[0].Path = &toolsproto.JsonPath{
Path: tool.getIDResponseFieldPath(),
}
}
}
}

return nil
Expand Down Expand Up @@ -571,7 +581,9 @@ func (g *Generator) makeResponsesForModel(model *proto.Model, pathPrefix string,
fields = append(fields, embeddedFields...)
}

continue
if !f.IsHasMany() {
continue
}
}

config := &toolsproto.ResponseFieldConfig{
Expand Down Expand Up @@ -618,6 +630,19 @@ func (g *Generator) makeResponsesForModel(model *proto.Model, pathPrefix string,
}
}

if f.IsHasMany() {
if getToolID, input := g.findListByForeignID(f.Type.ModelName.Value, f.InverseFieldName.Value); getToolID != "" {
config.Link = &toolsproto.ActionLink{
ToolId: getToolID,
Data: []*toolsproto.DataMapping{
{
Key: input.FieldLocation.Path,
},
},
}
}
}

fields = append(fields, config)
}

Expand Down Expand Up @@ -691,6 +716,18 @@ func (g *Generator) findGetByIDTool(modelName string) string {
return ""
}

// findListByForeignID will search for a list tool for the given model which takes a specific foreign key as an input
// It will also return the request input field for that tool
func (g *Generator) findListByForeignID(modelName string, inverseFieldName string) (string, *toolsproto.RequestFieldConfig) {
for id, tool := range g.Tools {
if input := tool.getInput("$.where." + inverseFieldName + ".id.equals"); tool.Model.Name == modelName && tool.Action.Type == proto.ActionType_ACTION_TYPE_LIST && input != nil {
return id, input
}
}

return "", nil
}

// findAllByIDTools searches for the tools that operate on the given model and take in an ID as an input; Returns a map of
// tool IDs and the path of the input field; e.g. getPost: $.id. Results will omit the given tool id (ignoreID).
//
Expand Down
47 changes: 24 additions & 23 deletions tools/proto/tools.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tools/proto/tools.proto
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ message ResponseFieldConfig {
bool sortable = 9; // Based on @sortable()

// Set if this field is a FK and link to a get/list action on the target model
// Or set to an appropriate list action if this field is a to-many field - for example, linking to listSaleItems(sale.id)
optional ActionLink link = 10;

// for file fields only, display images inline
Expand Down
Loading

0 comments on commit f54ec65

Please sign in to comment.