Skip to content

Commit

Permalink
Start implementing the ComposeDashboardPanelAction veneer
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Oct 3, 2023
1 parent d149e80 commit 975b4d6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 6 deletions.
7 changes: 6 additions & 1 deletion internal/jennies/typescript/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ func (jenny *Builder) emptyValueForType(builders ast.Builders, pkg string, typeD
return jenny.emptyValueForType(builders, pkg, typeDef.AsDisjunction().Branches[0])
case ast.KindRef:
ref := typeDef.AsRef()
referredTypeBuilder, _ := builders.LocateByObject(ref.ReferredPkg, ref.ReferredType)
// FIXME: trying to find a reference to an object by only looking at builders is wrong
// since the builder could be omitted by veneers
referredTypeBuilder, found := builders.LocateByObject(ref.ReferredPkg, ref.ReferredType)
if !found {
return fmt.Sprintf("\"ref to %s.%s not found\"", ref.ReferredPkg, ref.ReferredType)
}

return jenny.emptyValueForType(builders, referredTypeBuilder.Package, referredTypeBuilder.For.Type)
case ast.KindStruct:
Expand Down
68 changes: 63 additions & 5 deletions internal/veneers/builder/actions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package builder

import (
"strings"

"github.com/grafana/cog/internal/ast"
"github.com/grafana/cog/internal/tools"
)
Expand All @@ -15,10 +17,13 @@ func OmitAction() RewriteAction {

func MergeIntoAction(sourceBuilderName string, underPath string, excludeOptions []string) RewriteAction {
return func(builders ast.Builders, destinationBuilder ast.Builder) ast.Builder {
// we're implicitly saying that this action only works on builders originating from the same package.
// that's probably not good enough.
sourcePkg, sourceBuilderNameWithoutPkg, found := strings.Cut(sourceBuilderName, ".")
if !found {
sourcePkg = destinationBuilder.Package
sourceBuilderNameWithoutPkg = sourceBuilderName
}

sourceBuilder, found := builders.LocateByObject(destinationBuilder.Package, sourceBuilderName)
sourceBuilder, found := builders.LocateByObject(sourcePkg, sourceBuilderNameWithoutPkg)
if !found {
return destinationBuilder
}
Expand Down Expand Up @@ -51,8 +56,61 @@ func MergeIntoAction(sourceBuilderName string, underPath string, excludeOptions
}
}

func ComposeDashboardPanelAction(_ /*panelBuilderName*/ string) RewriteAction {
func ComposeDashboardPanelAction(panelBuilderName string) RewriteAction {
return func(builders ast.Builders, destinationBuilder ast.Builder) ast.Builder {
return destinationBuilder
panelBuilderPkg, panelBuilderNameWithoutPkg, found := strings.Cut(panelBuilderName, ".")
if !found {
panelBuilderPkg = destinationBuilder.Package
panelBuilderNameWithoutPkg = panelBuilderName
}

panelBuilder, found := builders.LocateByObject(panelBuilderPkg, panelBuilderNameWithoutPkg)
if !found {
// TODO: we failed here, an error is the correct action.
return destinationBuilder
}

// we're more building a new thing than updating any of the panel or composable builder,
// so let's create a completely fresh instance
newBuilder := ast.Builder{
Schema: panelBuilder.Schema,
For: panelBuilder.For,
RootPackage: destinationBuilder.RootPackage,
Package: destinationBuilder.Package,
}

// re-add panel-related options
for _, panelOpt := range panelBuilder.Options {
// this value is a constant that depends on the plugin being composed into a panel
if panelOpt.Name == "type" {
continue
}

newBuilder.Options = append(newBuilder.Options, panelOpt)
}

// re-add plugin-specific options
for _, pluginOpt := range destinationBuilder.Options {
newOpt := pluginOpt
newOpt.Assignments = nil

// re-root assignments
for _, assignment := range pluginOpt.Assignments {
newAssignment := assignment
newAssignment.Path = "options." + newAssignment.Path

newOpt.Assignments = append(newOpt.Assignments, newAssignment)
}

newBuilder.Options = append(newBuilder.Options, newOpt)
}

newBuilder.Initializations = append(newBuilder.Initializations, ast.Assignment{
Path: "type",
ValueType: ast.String(),
Value: destinationBuilder.Schema.Metadata.Identifier,
})

return newBuilder
}
}

0 comments on commit 975b4d6

Please sign in to comment.