Skip to content

Commit

Permalink
fixes #1285
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanj-square committed May 20, 2024
1 parent e6c8660 commit 21ed251
Show file tree
Hide file tree
Showing 6 changed files with 649 additions and 500 deletions.
1,058 changes: 563 additions & 495 deletions backend/protos/xyz/block/ftl/v1/schema/schema.pb.go

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions backend/protos/xyz/block/ftl/v1/schema/schema.proto
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,16 @@ message EnumVariant {
}

message Error {
enum ErrorLevel {
INFO = 0;
WARN = 1;
ERROR = 2;
}

string msg = 1;
Position pos = 2;
int64 endColumn = 3;
ErrorLevel level = 4;
}

message ErrorList {
Expand Down
31 changes: 26 additions & 5 deletions backend/schema/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@ import (
schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema"
)

type ErrorLevel int

const (
INFO ErrorLevel = iota
WARN
ERROR
)

type Error struct {
Msg string `json:"msg" protobuf:"1"`
Pos Position `json:"pos" protobuf:"2"`
EndColumn int `json:"endCol" protobuf:"3"`
Msg string `json:"msg" protobuf:"1"`
Pos Position `json:"pos" protobuf:"2"`
EndColumn int `json:"endCol" protobuf:"3"`
Level ErrorLevel `json:"level" protobuf:"4"`
}

func (e Error) ToProto() *schemapb.Error {
Expand Down Expand Up @@ -67,11 +76,23 @@ func ErrorListFromProto(e *schemapb.ErrorList) *ErrorList {
}
}

func Errorf(pos Position, endColumn int, format string, args ...any) *Error {
err := Error{Msg: fmt.Sprintf(format, args...), Pos: pos, EndColumn: endColumn}
func makeError(level ErrorLevel, pos Position, endColumn int, format string, args ...any) *Error {
err := Error{Msg: fmt.Sprintf(format, args...), Pos: pos, EndColumn: endColumn, Level: level}
return &err
}

func Infof(pos Position, endColumn int, format string, args ...any) *Error {
return makeError(INFO, pos, endColumn, format, args...)
}

func Warnf(pos Position, endColumn int, format string, args ...any) *Error {
return makeError(WARN, pos, endColumn, format, args...)
}

func Errorf(pos Position, endColumn int, format string, args ...any) *Error {
return makeError(ERROR, pos, endColumn, format, args...)
}

func Wrapf(pos Position, endColumn int, err error, format string, args ...any) *Error {
if format == "" {
format = "%s"
Expand Down
17 changes: 17 additions & 0 deletions backend/schema/protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func generateStruct(t reflect.Type, messages map[string]string) {
if typesWithRuntime[t.Name()] {
fmt.Fprintf(w, "\n optional %sRuntime runtime = 31634;\n", name)
}
generateWellKnownNestedEnums(t, w)
fields := reflect.VisibleFields(t)
// Sort by protobuf tag
slices.SortFunc(fields, func(a, b reflect.StructField) int {
Expand Down Expand Up @@ -132,6 +133,18 @@ func generateStruct(t reflect.Type, messages map[string]string) {
messages[t.Name()] = w.String()
}

// generateWellKnownNestedEnums generates the well-known nested enums for a given reflect.Type.
// Reflection cannot discover type aliased enums, so they need to get generated manually.
func generateWellKnownNestedEnums(t reflect.Type, w *strings.Builder) {
if (t == reflect.TypeOf(Error{})) {
fmt.Fprintf(w, " enum %s {\n", reflect.TypeOf(ErrorLevel(0)).Name())
fmt.Fprintf(w, " INFO = %d;\n", INFO)
fmt.Fprintf(w, " WARN = %d;\n", WARN)
fmt.Fprintf(w, " ERROR = %d;\n", ERROR)
fmt.Fprintf(w, " }\n")
}
}

func generateUnion(t reflect.Type, messages map[string]string) {
t = indirect(t)
if _, ok := messages[t.Name()]; ok {
Expand Down Expand Up @@ -166,6 +179,10 @@ func generateProtoType(t reflect.Type) string {
case reflect.String:
return "string"
case reflect.Int:
// determine if t is a well-known type aliased enum
if t == reflect.TypeOf(ErrorLevel(0)) {
return t.Name()
}
return "int64"
case reflect.Bool:
return "bool"
Expand Down
4 changes: 4 additions & 0 deletions backend/schema/protobuf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ func TestProtoRoundtrip(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, Normalise(testSchema), Normalise(actual))
}

func TestErrorProtoGeneration(t *testing.T) {

}
32 changes: 32 additions & 0 deletions frontend/src/protos/xyz/block/ftl/v1/schema/schema_pb.ts

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

0 comments on commit 21ed251

Please sign in to comment.