-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
399 additions
and
205 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
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,27 @@ | ||
package proto | ||
|
||
func (a *Action) IsFunction() bool { | ||
return a.Implementation == ActionImplementation_ACTION_IMPLEMENTATION_CUSTOM | ||
} | ||
|
||
func (a *Action) IsArbitraryFunction() bool { | ||
return a.IsFunction() && (a.Type == ActionType_ACTION_TYPE_READ || a.Type == ActionType_ACTION_TYPE_WRITE) | ||
} | ||
|
||
func (a *Action) IsWriteAction() bool { | ||
switch a.Type { | ||
case ActionType_ACTION_TYPE_CREATE, ActionType_ACTION_TYPE_DELETE, ActionType_ACTION_TYPE_WRITE, ActionType_ACTION_TYPE_UPDATE: | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
func (a *Action) IsReadAction() bool { | ||
switch a.Type { | ||
case ActionType_ACTION_TYPE_GET, ActionType_ACTION_TYPE_LIST, ActionType_ACTION_TYPE_READ: | ||
return true | ||
default: | ||
return false | ||
} | ||
} |
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,33 @@ | ||
package proto | ||
|
||
// IsFile tells us if the field is a file | ||
func (f *Field) IsFile() bool { | ||
if f.Type == nil { | ||
return false | ||
} | ||
|
||
return f.Type.Type == Type_TYPE_INLINE_FILE | ||
} | ||
|
||
// IsTypeModel returns true of the field's type is Model. | ||
func (f *Field) IsTypeModel() bool { | ||
return f.Type.Type == Type_TYPE_MODEL | ||
} | ||
|
||
// IsTypeRepeated returns true if the field is specified as | ||
// being "repeated". | ||
func (f *Field) IsRepeated() bool { | ||
return f.Type.Repeated | ||
} | ||
|
||
func (f *Field) IsHasMany() bool { | ||
return f.Type.Type == Type_TYPE_MODEL && f.ForeignKeyFieldName == nil && f.Type.Repeated | ||
} | ||
|
||
func (f *Field) IsHasOne() bool { | ||
return f.Type.Type == Type_TYPE_MODEL && f.ForeignKeyFieldName == nil && !f.Type.Repeated | ||
} | ||
|
||
func (f *Field) IsBelongsTo() bool { | ||
return f.Type.Type == Type_TYPE_MODEL && f.ForeignKeyFieldName != nil && !f.Type.Repeated | ||
} |
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,42 @@ | ||
package proto | ||
|
||
import "github.com/samber/lo" | ||
|
||
// HasFiles checks if the message has any Inline file fields | ||
func (m *Message) HasFiles() bool { | ||
return len(m.FileFields()) > 0 | ||
} | ||
|
||
// FileFields will return a slice of fields for the model that are of type file | ||
func (m *Message) FileFields() []*MessageField { | ||
return lo.Filter(m.Fields, func(f *MessageField, _ int) bool { | ||
return f.IsFile() | ||
}) | ||
} | ||
|
||
// IsModelField returns true if the input targets a model field | ||
// and is handled automatically by the runtime. | ||
// This will only be true for inputs that are built-in actions, | ||
// as functions never have this behaviour. | ||
func (f *MessageField) IsModelField() bool { | ||
return len(f.Target) > 0 | ||
} | ||
|
||
// IsFile tells us if the field is a file | ||
func (f *MessageField) IsFile() bool { | ||
if f.Type == nil { | ||
return false | ||
} | ||
|
||
return f.Type.Type == Type_TYPE_INLINE_FILE | ||
} | ||
|
||
func (m *Message) FindField(fieldName string) *MessageField { | ||
for _, field := range m.Fields { | ||
if field.Name == fieldName { | ||
return field | ||
} | ||
} | ||
|
||
return nil | ||
} |
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,48 @@ | ||
package proto | ||
|
||
import ( | ||
"sort" | ||
|
||
"github.com/samber/lo" | ||
) | ||
|
||
// FileFields will return a slice of fields for the model that are of type file | ||
func (m *Model) FileFields() []*Field { | ||
return lo.Filter(m.Fields, func(f *Field, _ int) bool { | ||
return f.IsFile() | ||
}) | ||
} | ||
|
||
// HasFiles checks if the model has any fields that are files | ||
func (m *Model) HasFiles() bool { | ||
return len(m.FileFields()) > 0 | ||
} | ||
|
||
// FieldNames provides a (sorted) list of the fields in the model of the given name. | ||
func (m *Model) FieldNames() []string { | ||
names := lo.Map(m.Fields, func(x *Field, _ int) string { | ||
return x.Name | ||
}) | ||
sort.Strings(names) | ||
return names | ||
} | ||
|
||
// ForeignKeyFields returns all the fields in the given model which have their ForeignKeyInfo | ||
// populated. | ||
func (m *Model) ForeignKeyFields() []*Field { | ||
return lo.Filter(m.Fields, func(f *Field, _ int) bool { | ||
return f.ForeignKeyInfo != nil | ||
}) | ||
} | ||
|
||
// PrimaryKeyFieldName returns the name of the field in the given model, | ||
// that is marked as being the model's primary key. (Or empty string). | ||
func (m *Model) PrimaryKeyFieldName() string { | ||
field, _ := lo.Find(m.Fields, func(f *Field) bool { | ||
return f.PrimaryKey | ||
}) | ||
if field != nil { | ||
return field.Name | ||
} | ||
return "" | ||
} |
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,12 @@ | ||
package proto | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFieldNames(t *testing.T) { | ||
t.Parallel() | ||
require.Equal(t, []string{"Field1", "Field2"}, referenceSchema.Models[0].FieldNames()) | ||
} |
Oops, something went wrong.