-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
codegen: add nested field selection to the command context (#177)
- Loading branch information
Showing
21 changed files
with
304 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...sura-ndc-go/command/internal/testdata/single_op/expected/function/types.generated.go.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Code generated by github.com/hasura/ndc-sdk-go/cmd/hasura-ndc-go, DO NOT EDIT. | ||
package function | ||
import ( | ||
"context" | ||
"github.com/hasura/ndc-codegen-function-only-test/types" | ||
"github.com/hasura/ndc-sdk-go/connector" | ||
"github.com/hasura/ndc-sdk-go/schema" | ||
"github.com/hasura/ndc-sdk-go/utils" | ||
"go.opentelemetry.io/otel/trace" | ||
"log/slog" | ||
"slices" | ||
) | ||
|
||
// ToMap encodes the struct to a value map | ||
func (j SimpleResult) ToMap() map[string]any { | ||
r := make(map[string]any) | ||
r["reply"] = j.Reply | ||
|
||
return r | ||
} | ||
// DataConnectorHandler implements the data connector handler | ||
type DataConnectorHandler struct{} | ||
|
||
// QueryExists check if the query name exists | ||
func (dch DataConnectorHandler) QueryExists(name string) bool { | ||
return slices.Contains(enumValues_FunctionName, name) | ||
} | ||
func (dch DataConnectorHandler) Query(ctx context.Context, state *types.State, request *schema.QueryRequest, rawArgs map[string]any) (*schema.RowSet, error) { | ||
if !dch.QueryExists(request.Collection) { | ||
return nil, utils.ErrHandlerNotfound | ||
} | ||
queryFields, err := utils.EvalFunctionSelectionFieldValue(request) | ||
if err != nil { | ||
return nil, schema.UnprocessableContentError(err.Error(), nil) | ||
} | ||
|
||
result, err := dch.execQuery(context.WithValue(ctx, utils.CommandSelectionFieldKey, queryFields), state, request, queryFields, rawArgs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &schema.RowSet{ | ||
Aggregates: schema.RowSetAggregates{}, | ||
Rows: []map[string]any{ | ||
{ | ||
"__value": result, | ||
}, | ||
}, | ||
}, nil | ||
} | ||
|
||
func (dch DataConnectorHandler) execQuery(ctx context.Context, state *types.State, request *schema.QueryRequest, queryFields schema.NestedField, rawArgs map[string]any) (any, error) { | ||
span := trace.SpanFromContext(ctx) | ||
logger := connector.GetLogger(ctx) | ||
switch request.Collection { | ||
case "simpleObject": | ||
|
||
selection, err := queryFields.AsObject() | ||
if err != nil { | ||
return nil, schema.UnprocessableContentError("the selection field type must be object", map[string]any{ | ||
"cause": err.Error(), | ||
}) | ||
} | ||
rawResult, err := FunctionSimpleObject(ctx, state) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
connector_addSpanEvent(span, logger, "evaluate_response_selection", map[string]any{ | ||
"raw_result": rawResult, | ||
}) | ||
result, err := utils.EvalNestedColumnObject(selection, rawResult) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return result, nil | ||
|
||
default: | ||
return nil, utils.ErrHandlerNotfound | ||
} | ||
} | ||
var enumValues_FunctionName = []string{"simpleObject"} | ||
func connector_addSpanEvent(span trace.Span, logger *slog.Logger, name string, data map[string]any, options ...trace.EventOption) { | ||
logger.Debug(name, slog.Any("data", data)) | ||
attrs := utils.DebugJSONAttributes(data, utils.IsDebug(logger)) | ||
span.AddEvent(name, append(options, trace.WithAttributes(attrs...))...) | ||
} |
65 changes: 65 additions & 0 deletions
65
cmd/hasura-ndc-go/command/internal/testdata/single_op/expected/hello/types.generated.go.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Code generated by github.com/hasura/ndc-sdk-go/cmd/hasura-ndc-go, DO NOT EDIT. | ||
package hello | ||
import ( | ||
"context" | ||
"github.com/hasura/ndc-codegen-function-only-test/types" | ||
"github.com/hasura/ndc-sdk-go/connector" | ||
"github.com/hasura/ndc-sdk-go/schema" | ||
"github.com/hasura/ndc-sdk-go/utils" | ||
"go.opentelemetry.io/otel/trace" | ||
"log/slog" | ||
"slices" | ||
) | ||
|
||
// DataConnectorHandler implements the data connector handler | ||
type DataConnectorHandler struct{} | ||
|
||
// QueryExists check if the query name exists | ||
func (dch DataConnectorHandler) QueryExists(name string) bool { | ||
return slices.Contains(enumValues_FunctionName, name) | ||
} | ||
func (dch DataConnectorHandler) Query(ctx context.Context, state *types.State, request *schema.QueryRequest, rawArgs map[string]any) (*schema.RowSet, error) { | ||
if !dch.QueryExists(request.Collection) { | ||
return nil, utils.ErrHandlerNotfound | ||
} | ||
queryFields, err := utils.EvalFunctionSelectionFieldValue(request) | ||
if err != nil { | ||
return nil, schema.UnprocessableContentError(err.Error(), nil) | ||
} | ||
|
||
result, err := dch.execQuery(context.WithValue(ctx, utils.CommandSelectionFieldKey, queryFields), state, request, queryFields, rawArgs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &schema.RowSet{ | ||
Aggregates: schema.RowSetAggregates{}, | ||
Rows: []map[string]any{ | ||
{ | ||
"__value": result, | ||
}, | ||
}, | ||
}, nil | ||
} | ||
|
||
func (dch DataConnectorHandler) execQuery(ctx context.Context, state *types.State, request *schema.QueryRequest, queryFields schema.NestedField, rawArgs map[string]any) (any, error) { | ||
span := trace.SpanFromContext(ctx) | ||
logger := connector.GetLogger(ctx) | ||
switch request.Collection { | ||
case "hello": | ||
|
||
if len(queryFields) > 0 { | ||
return nil, schema.UnprocessableContentError("cannot evaluate selection fields for scalar", nil) | ||
} | ||
return FunctionHello(ctx, state) | ||
|
||
default: | ||
return nil, utils.ErrHandlerNotfound | ||
} | ||
} | ||
var enumValues_FunctionName = []string{"hello"} | ||
func connector_addSpanEvent(span trace.Span, logger *slog.Logger, name string, data map[string]any, options ...trace.EventOption) { | ||
logger.Debug(name, slog.Any("data", data)) | ||
attrs := utils.DebugJSONAttributes(data, utils.IsDebug(logger)) | ||
span.AddEvent(name, append(options, trace.WithAttributes(attrs...))...) | ||
} |
67 changes: 67 additions & 0 deletions
67
...ura-ndc-go/command/internal/testdata/single_op/expected/procedure/types.generated.go.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Code generated by github.com/hasura/ndc-sdk-go/cmd/hasura-ndc-go, DO NOT EDIT. | ||
package procedure | ||
import ( | ||
"context" | ||
"github.com/hasura/ndc-codegen-function-only-test/types" | ||
"github.com/hasura/ndc-sdk-go/connector" | ||
"github.com/hasura/ndc-sdk-go/schema" | ||
"github.com/hasura/ndc-sdk-go/utils" | ||
"go.opentelemetry.io/otel/trace" | ||
"log/slog" | ||
"slices" | ||
) | ||
|
||
// DataConnectorHandler implements the data connector handler | ||
type DataConnectorHandler struct{} | ||
|
||
// MutationExists check if the mutation name exists | ||
func (dch DataConnectorHandler) MutationExists(name string) bool { | ||
return slices.Contains(enumValues_ProcedureName, name) | ||
} | ||
func (dch DataConnectorHandler) Mutation(ctx context.Context, state *types.State, operation *schema.MutationOperation) (schema.MutationOperationResults, error) { | ||
span := trace.SpanFromContext(ctx) | ||
logger := connector.GetLogger(ctx) | ||
ctx = context.WithValue(ctx, utils.CommandSelectionFieldKey, operation.Fields) | ||
connector_addSpanEvent(span, logger, "validate_request", map[string]any{ | ||
"operations_name": operation.Name, | ||
}) | ||
|
||
switch operation.Name { | ||
case "createDemo": | ||
|
||
selection, err := operation.Fields.AsObject() | ||
if err != nil { | ||
return nil, schema.UnprocessableContentError("the selection field type must be object", map[string]any{ | ||
"cause": err.Error(), | ||
}) | ||
} | ||
span.AddEvent("execute_procedure") | ||
rawResult, err := ProcedureCreateDemo(ctx, state) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if rawResult == nil { | ||
return schema.NewProcedureResult(nil).Encode(), nil | ||
} | ||
connector_addSpanEvent(span, logger, "evaluate_response_selection", map[string]any{ | ||
"raw_result": rawResult, | ||
}) | ||
result, err := utils.EvalNestedColumnObject(selection, rawResult) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
return schema.NewProcedureResult(result).Encode(), nil | ||
|
||
default: | ||
return nil, utils.ErrHandlerNotfound | ||
} | ||
} | ||
var enumValues_ProcedureName = []string{"createDemo"} | ||
func connector_addSpanEvent(span trace.Span, logger *slog.Logger, name string, data map[string]any, options ...trace.EventOption) { | ||
logger.Debug(name, slog.Any("data", data)) | ||
attrs := utils.DebugJSONAttributes(data, utils.IsDebug(logger)) | ||
span.AddEvent(name, append(options, trace.WithAttributes(attrs...))...) | ||
} |
Oops, something went wrong.