Skip to content

Commit

Permalink
remove unecessary type casting check
Browse files Browse the repository at this point in the history
  • Loading branch information
fredcarle committed Feb 6, 2025
1 parent 0c519c8 commit 107576d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 83 deletions.
54 changes: 7 additions & 47 deletions internal/request/graphql/schema/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,52 +629,20 @@ func embeddingFromAST(directive *ast.Directive, fieldDef *ast.FieldDefinition) (
for _, arg := range directive.Arguments {
switch arg.Name.Value {
case types.EmbeddingDirectivePropFields:
val, ok := arg.Value.(*ast.ListValue)
if !ok {
return client.EmbeddingDescription{},
NewErrEmbeddingInvalidProp[[]string](types.ConstraintsDirectivePropSize, arg.Value.GetValue())
}
val := arg.Value.(*ast.ListValue)
fields := make([]string, len(val.Values))
for i, untypedField := range val.Values {
field, ok := untypedField.(*ast.StringValue)
if !ok {
return client.EmbeddingDescription{},
NewErrEmbeddingInvalidProp[[]string](types.ConstraintsDirectivePropSize, field.GetValue())
}
fields[i] = field.Value
fields[i] = untypedField.(*ast.StringValue).Value
}
embedding.Fields = fields
case types.EmbeddingDirectivePropModel:
val, ok := arg.Value.(*ast.StringValue)
if !ok {
return client.EmbeddingDescription{},
NewErrEmbeddingInvalidProp[string](types.EmbeddingDirectivePropModel, arg.Value.GetValue())
}
embedding.Model = val.Value
embedding.Model = arg.Value.(*ast.StringValue).Value
case types.EmbeddingDirectivePropProvider:
val, ok := arg.Value.(*ast.StringValue)
if !ok {
return client.EmbeddingDescription{},
NewErrEmbeddingInvalidProp[string](types.EmbeddingDirectivePropProvider, arg.Value.GetValue())
}
embedding.Provider = val.Value
embedding.Provider = arg.Value.(*ast.StringValue).Value
case types.EmbeddingDirectivePropTemplate:
val, ok := arg.Value.(*ast.StringValue)
if !ok {
return client.EmbeddingDescription{},
NewErrEmbeddingInvalidProp[string](types.EmbeddingDirectivePropTemplate, arg.Value.GetValue())
}
embedding.Template = val.Value
embedding.Template = arg.Value.(*ast.StringValue).Value
case types.EmbeddingDirectivePropURL:
val, ok := arg.Value.(*ast.StringValue)
if !ok {
return client.EmbeddingDescription{},
NewErrEmbeddingInvalidProp[string](types.EmbeddingDirectivePropURL, arg.Value.GetValue())
}
embedding.URL = val.Value
default:
return client.EmbeddingDescription{},
NewErrDirectiveWithUnknownArg(types.ConstraintsDirectiveLabel, arg.Name.Value)
embedding.URL = arg.Value.(*ast.StringValue).Value

Check warning on line 645 in internal/request/graphql/schema/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/schema/collection.go#L625-L645

Added lines #L625 - L645 were not covered by tests
}
}
return embedding, nil

Check warning on line 648 in internal/request/graphql/schema/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/schema/collection.go#L648

Added line #L648 was not covered by tests
Expand All @@ -692,19 +660,11 @@ func contraintsFromAST(kind client.FieldKind, directive *ast.Directive) (constra
if !kind.IsArray() {
return constraintDescription{}, NewErrInvalidTypeForContraint(kind)
}
val, ok := arg.Value.(*ast.IntValue)
if !ok {
return constraintDescription{},
NewErrContraintsInvalidProp[int](types.ConstraintsDirectivePropSize, arg.Value.GetValue())
}
size, err := strconv.Atoi(val.Value)
size, err := strconv.Atoi(arg.Value.(*ast.IntValue).Value)
if err != nil {
return constraintDescription{}, err
}
constraints.Size = size

Check warning on line 667 in internal/request/graphql/schema/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/schema/collection.go#L655-L667

Added lines #L655 - L667 were not covered by tests
default:
return constraintDescription{},
NewErrDirectiveWithUnknownArg(types.ConstraintsDirectiveLabel, arg.Name.Value)
}
}
return constraints, nil

Check warning on line 670 in internal/request/graphql/schema/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/schema/collection.go#L670

Added line #L670 was not covered by tests
Expand Down
36 changes: 0 additions & 36 deletions internal/request/graphql/schema/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
package schema

import (
"fmt"

"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/errors"
)
Expand Down Expand Up @@ -40,10 +38,7 @@ const (
errDefaultValueInvalid string = "default value is invalid"
errDefaultValueOneArg string = "default value must specify one argument"
errFieldTypeNotSpecified string = "field type not specified"
errDirectiveWithUnknownArg string = "directive with unknown argument"
errConstraintsInvalidProp string = "constraints directive with invalid property"
errInvalidTypeForContraint string = "size constraint can only be applied to array fields"
errEmbeddingInvalidProp string = "embedding directive with invalid property"
)

var (
Expand Down Expand Up @@ -71,9 +66,6 @@ var (
ErrPolicyInvalidIDProp = errors.New(errPolicyInvalidIDProp)
ErrPolicyInvalidResourceProp = errors.New(errPolicyInvalidResourceProp)
ErrFieldTypeNotSpecified = errors.New(errFieldTypeNotSpecified)
ErrrDirectiveWithUnknownArg = errors.New(errDirectiveWithUnknownArg)
ErrConstraintsInvalidSize = errors.New(errConstraintsInvalidProp)
ErrEmbedingInvalidSize = errors.New(errEmbeddingInvalidProp)
ErrInvalidTypeForContraint = errors.New(errInvalidTypeForContraint)
)

Expand Down Expand Up @@ -196,34 +188,6 @@ func NewErrFieldTypeNotSpecified(objectName, fieldName string) error {
)
}

func NewErrDirectiveWithUnknownArg(directive, arg string) error {
return errors.New(
errDirectiveWithUnknownArg,
errors.NewKV("Directive", directive),
errors.NewKV("Argument", arg),
)
}

func NewErrContraintsInvalidProp[TExpected any](name string, actual any) error {
var expected TExpected
return errors.New(
errConstraintsInvalidProp,
errors.NewKV("Prop", name),
errors.NewKV("Expected", fmt.Sprintf("%T", expected)),
errors.NewKV("Actual", fmt.Sprintf("%T", actual)),
)
}

func NewErrEmbeddingInvalidProp[TExpected any](name string, actual any) error {
var expected TExpected
return errors.New(
errEmbeddingInvalidProp,
errors.NewKV("Prop", name),
errors.NewKV("Expected", fmt.Sprintf("%T", expected)),
errors.NewKV("Actual", fmt.Sprintf("%T", actual)),
)
}

func NewErrInvalidTypeForContraint(actual client.FieldKind) error {
return errors.New(errInvalidTypeForContraint, errors.NewKV("Actual", actual.String()))

Check warning on line 192 in internal/request/graphql/schema/errors.go

View check run for this annotation

Codecov / codecov/patch

internal/request/graphql/schema/errors.go#L191-L192

Added lines #L191 - L192 were not covered by tests
}

0 comments on commit 107576d

Please sign in to comment.