Skip to content

Commit

Permalink
Use ConvertObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
andresmgot committed Aug 30, 2024
1 parent 4029417 commit 4570e6c
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions examples/datasource-http-backend/pkg/plugin/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,13 @@ var DatasourceOpts = datasource.ManageOpts{
attribute.String("my_plugin.my_attribute", "custom value"),
},
},
ConversionHandler: &Datasource{},
}

// Datasource is an example datasource which can respond to data queries, reports
// its health and has streaming skills.
type Datasource struct {
backend.QueryMigrationHandler
backend.ConversionHandler

settings backend.DataSourceInstanceSettings

Expand Down Expand Up @@ -270,25 +271,36 @@ func newHealthCheckErrorf(format string, args ...interface{}) *backend.CheckHeal
return &backend.CheckHealthResult{Status: backend.HealthStatusError, Message: fmt.Sprintf(format, args...)}
}

func (d *Datasource) MigrateQuery(ctx context.Context, req *backend.QueryMigrationRequest) (*backend.QueryMigrationResponse, error) {
res := &backend.QueryMigrationResponse{}
// NOTE: req.PluginContext.APIVersion and req.PluginContext.PluginVersion are available here
for _, query := range req.Queries {
func (d *Datasource) ConvertObjects(ctx context.Context, req *backend.ConversionRequest) (*backend.ConversionResponse, error) {
res := &backend.ConversionResponse{}
// NOTE: req.PluginContext.APIVersion and req.PluginContext.PluginVersion are available here, which represent
// the current runtime version, not the version the query was built with.
for _, query := range req.Objects {
q := &backend.DataQuery{}
err := json.Unmarshal(query.Raw, q)
if err != nil {
return &backend.ConversionResponse{}, fmt.Errorf("unmarshal: %w", err)
}
input := &kinds.DataQuery{}
err := json.Unmarshal(query.JSON, input)
err = json.Unmarshal(q.JSON, input)
if err != nil {
return &backend.QueryMigrationResponse{}, fmt.Errorf("unmarshal: %w", err)
return &backend.ConversionResponse{}, fmt.Errorf("unmarshal: %w", err)
}
if input.Multiplier != 0 && input.Multiply == 0 {
input.Multiply = input.Multiplier
input.Multiplier = 0
}
newJSON, err := json.Marshal(input)
if err != nil {
return &backend.QueryMigrationResponse{}, fmt.Errorf("marshal: %w", err)
return &backend.ConversionResponse{}, fmt.Errorf("marshal: %w", err)
}
q.JSON = newJSON
qBytes, err := json.Marshal(q)
if err != nil {
return &backend.ConversionResponse{}, fmt.Errorf("marshal: %w", err)
}
query.JSON = newJSON
res.Queries = append(res.Queries, query)
query.Raw = qBytes
res.Objects = append(res.Objects, query)
}
return res, nil
}

0 comments on commit 4570e6c

Please sign in to comment.