Skip to content

Commit

Permalink
feat: admin search and search-ability of JSON fields (#11)
Browse files Browse the repository at this point in the history
* feat: search with admin params

Signed-off-by: Sarah Funkhouser <147884153+golanglemonade@users.noreply.github.com>

* missing annotation

Signed-off-by: Sarah Funkhouser <147884153+golanglemonade@users.noreply.github.com>

* more ports

Signed-off-by: Sarah Funkhouser <147884153+golanglemonade@users.noreply.github.com>

* fix admin search

Signed-off-by: Sarah Funkhouser <147884153+golanglemonade@users.noreply.github.com>

* test and cleanup

Signed-off-by: Sarah Funkhouser <147884153+golanglemonade@users.noreply.github.com>

* add type, allow JSON fields to be searched

Signed-off-by: Sarah Funkhouser <147884153+golanglemonade@users.noreply.github.com>

* fix imports

Signed-off-by: Sarah Funkhouser <147884153+golanglemonade@users.noreply.github.com>

---------

Signed-off-by: Sarah Funkhouser <147884153+golanglemonade@users.noreply.github.com>
  • Loading branch information
golanglemonade authored Sep 3, 2024
1 parent 9a01f6f commit 05550c9
Show file tree
Hide file tree
Showing 10 changed files with 534 additions and 102 deletions.
15 changes: 12 additions & 3 deletions annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ type ThroughCleanup struct {
}

// SchemaGenAnnotation is an annotation used to indicate that schema generation should be skipped for this type
// Searchable will be ignored if Skip is set to true
// Searchable and SkipSearch allow for schemas to be be opt in or opt out
// When Skip is true, the search schema generation is always skipped
// SkipSearch allow for schemas to be be opt out of search schema generation
type SchemaGenAnnotation struct {
// Skip indicates that the schema generation should be skipped for this type
Skip bool
// SkipSearch indicates that the schema should not be searchable, this is ignored if Skip is set to true
// SkipSearch indicates that the schema should not be searchable
// Schemas are also not searchable if not fields are marked as searchable
SkipSearch bool
}
Expand All @@ -53,6 +53,8 @@ type QueryGenAnnotation struct {
type SearchFieldAnnotation struct {
// Searchable indicates that the field should be searchable
Searchable bool
// ExcludeAdmin indicates that the field will be excluded from the admin search which includes all fields by default
ExcludeAdmin bool
}

// Name returns the name of the CascadeAnnotation
Expand Down Expand Up @@ -122,6 +124,13 @@ func FieldSearchable() *SearchFieldAnnotation {
}
}

// FieldAdminSearchable returns a new SearchFieldAnnotation with the exclude admin searchable flag set
func FieldAdminSearchable(s bool) *SearchFieldAnnotation {
return &SearchFieldAnnotation{
ExcludeAdmin: !s,
}
}

// Decode unmarshalls the CascadeAnnotation
func (a *CascadeAnnotation) Decode(annotation interface{}) error {
buf, err := json.Marshal(annotation)
Expand Down
10 changes: 0 additions & 10 deletions genhooks/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,3 @@ func isSoftDeleteField(f *gen.Field) bool {
func getFileName(dir, name string) string {
return filepath.Clean(dir + strings.ToLower(name) + ".graphql")
}

// toFirstLower converts the first character of a string to lowercase
// except if the entire string is uppercase, e.g TTL, should remain as TTL
func toFirstLower(s string) string {
if strings.ToUpper(s) == s {
return s
}

return strings.ToLower(s[:1]) + s[1:]
}
35 changes: 0 additions & 35 deletions genhooks/gen_test.go

This file was deleted.

6 changes: 3 additions & 3 deletions genhooks/gen_query.go → genhooks/genquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

"entgo.io/contrib/entgql"
"entgo.io/ent/entc/gen"
"github.com/99designs/gqlgen/codegen/templates"
"github.com/gertd/go-pluralize"
"github.com/stoewer/go-strcase"

"github.com/theopenlane/entx"
)
Expand Down Expand Up @@ -97,7 +97,7 @@ func getFieldNames(fields []*gen.Field) []string {
continue
}

fieldNames = append(fieldNames, toFirstLower(f.StructField()))
fieldNames = append(fieldNames, templates.ToGoPrivate(f.StructField()))
}

// sort field names
Expand Down Expand Up @@ -167,7 +167,7 @@ func isHistorySchema(node *gen.Type) bool {
func createQuery() *template.Template {
// function map for template
fm := template.FuncMap{
"ToLowerCamel": strcase.LowerCamelCase,
"ToLowerCamel": templates.ToGoPrivate,
"ToPlural": pluralize.NewClient().Plural,
}

Expand Down
4 changes: 2 additions & 2 deletions genhooks/gen_schema.go → genhooks/genschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"os"

"entgo.io/ent/entc/gen"
"github.com/99designs/gqlgen/codegen/templates"
"github.com/gertd/go-pluralize"
"github.com/stoewer/go-strcase"

"github.com/theopenlane/entx"
)
Expand Down Expand Up @@ -77,7 +77,7 @@ func checkSchemaGenSkip(node *gen.Type) bool {
func createTemplate() *template.Template {
// function map for template
fm := template.FuncMap{
"ToLowerCamel": strcase.LowerCamelCase,
"ToLowerCamel": templates.ToGoPrivate,
"ToPlural": pluralize.NewClient().Plural,
}

Expand Down
Loading

0 comments on commit 05550c9

Please sign in to comment.