Skip to content

Commit

Permalink
Update tags input to map instead of two lists
Browse files Browse the repository at this point in the history
  • Loading branch information
nitsanshai authored and eandre committed Oct 3, 2024
1 parent d1afc74 commit 0143588
Show file tree
Hide file tree
Showing 11 changed files with 549 additions and 545 deletions.
31 changes: 14 additions & 17 deletions cli/cmd/encore/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@ func init() {
rootCmd.AddCommand(genCmd)

var (
output string
lang string
envName string
genServiceNames []string
excludedServices []string
endpointTags []string
excludedEndpointTags []string
output string
lang string
envName string
genServiceNames []string
excludedServices []string
endpointTags map[string]string
)

genClientCmd := &cobra.Command{
Use: "client [<app-id>] [--env=<name>] [--services=foo,bar] [--excluded-services=baz,qux] [--tags=cache,mobile] [--excluded-tags=internal]",
Use: "client [<app-id>] [--env=<name>] [--services=foo,bar] [--excluded-services=baz,qux] [--tags=cache=true,internal=false]",
Short: "Generates an API client for your app",
Long: `Generates an API client for your app.
Expand Down Expand Up @@ -86,13 +85,12 @@ To further narrow down the services to generate, use the '--services' flag.
genServiceNames = []string{"*"}
}
resp, err := daemon.GenClient(ctx, &daemonpb.GenClientRequest{
AppId: appID,
EnvName: envName,
Lang: lang,
Services: genServiceNames,
ExcludedServices: excludedServices,
EndpointTags: endpointTags,
ExcludedEndpointTags: excludedEndpointTags,
AppId: appID,
EnvName: envName,
Lang: lang,
Services: genServiceNames,
ExcludedServices: excludedServices,
EndpointTags: endpointTags,
})
if err != nil {
fatal(err)
Expand Down Expand Up @@ -155,7 +153,6 @@ which may require the user-facing wrapper code to be manually generated.`,

genClientCmd.Flags().StringSliceVarP(&genServiceNames, "services", "s", nil, "The names of the services to include in the output")
genClientCmd.Flags().StringSliceVarP(&excludedServices, "excluded-services", "x", nil, "The names of the services to exclude in the output")
genClientCmd.Flags().StringSliceVarP(&endpointTags, "tags", "t", nil, "The names of endpoint tags to include in the output")
genClientCmd.Flags().
StringSliceVar(&excludedEndpointTags, "excluded-tags", nil, "The names of endpoint tags to exclude in the output")
StringToStringVarP(&endpointTags, "tags", "t", nil, "The names of endpoint tags to include or exclude in the output")
}
9 changes: 7 additions & 2 deletions cli/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,13 @@ func (s *Server) GenClient(ctx context.Context, params *daemonpb.GenClientReques
lang := clientgen.Lang(params.Lang)

servicesToGenerate := clientgentypes.NewServiceSet(md, params.Services, params.ExcludedServices)
tagsToInclude := clientgentypes.NewTagSet(params.EndpointTags, params.ExcludedEndpointTags)
code, err := clientgen.Client(lang, params.AppId, md, servicesToGenerate, tagsToInclude)

tagSet, err := clientgentypes.NewTagSet(params.EndpointTags)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}

code, err := clientgen.Client(lang, params.AppId, md, servicesToGenerate, tagSet)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion e2e-tests/echo_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func doTestEndToEndWithApp(t *testing.T, env []string) {
clientgen.LangJavascript: "js/client.js",
} {
services := clientgentypes.AllServices(app.Meta)
client, err := clientgen.Client(lang, "slug", app.Meta, services, clientgentypes.TagSet{})
client, err := clientgen.Client(lang, "slug", app.Meta, services, &clientgentypes.TagSet{})
if err != nil {
fmt.Println(err.Error())
c.FailNow()
Expand Down
2 changes: 1 addition & 1 deletion internal/clientgen/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func Client(
appSlug string,
md *meta.Data,
services clientgentypes.ServiceSet,
tags clientgentypes.TagSet,
tags *clientgentypes.TagSet,
) (code []byte, err error) {
defer func() {
if e := recover(); e != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/clientgen/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestClientCodeGenerationFromGoApp(t *testing.T) {
c.Assert(ok, qt.IsTrue, qt.Commentf("Unable to detect language type for %s", file.Name()))

services := clientgentypes.AllServices(res.Meta)
generatedClient, err := Client(language, "app", res.Meta, services, clientgentypes.TagSet{})
generatedClient, err := Client(language, "app", res.Meta, services, &clientgentypes.TagSet{})
c.Assert(err, qt.IsNil)

golden.TestAgainst(c, "goapp/"+file.Name(), string(generatedClient))
Expand Down Expand Up @@ -133,7 +133,7 @@ func TestClientCodeGenerationFromTSApp(t *testing.T) {
c.Assert(ok, qt.IsTrue, qt.Commentf("Unable to detect language type for %s", file.Name()))

services := clientgentypes.AllServices(res.Meta)
generatedClient, err := Client(language, "app", res.Meta, services, clientgentypes.TagSet{})
generatedClient, err := Client(language, "app", res.Meta, services, &clientgentypes.TagSet{})
c.Assert(err, qt.IsNil)

golden.TestAgainst(c, "tsapp/"+file.Name(), string(generatedClient))
Expand Down
36 changes: 22 additions & 14 deletions internal/clientgen/clientgentypes/clientgentypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package clientgentypes
import (
"bytes"
"slices"
"strconv"

meta "encr.dev/proto/encore/parser/meta/v1"
)
Expand All @@ -12,7 +13,7 @@ type GenerateParams struct {
AppSlug string
Meta *meta.Data
Services ServiceSet
Tags TagSet
Tags *TagSet
}

type ServiceSet struct {
Expand Down Expand Up @@ -66,22 +67,28 @@ func AllServices(md *meta.Data) ServiceSet {
}

type TagSet struct {
tagMap map[string]bool
includedTags []string
excludedTags []string
}

func NewTagSet(tags, excludedTags []string) TagSet {
filteredTags := make([]string, 0, len(tags))
for _, t := range tags {
if !slices.Contains(excludedTags, t) {
filteredTags = append(filteredTags, t)
}
func NewTagSet(tags map[string]string) (*TagSet, error) {
tagSet := TagSet{
tagMap: make(map[string]bool),
includedTags: make([]string, 0, len(tags)),
}
for tag, includedStr := range tags {
included, err := strconv.ParseBool(includedStr)
if err != nil {
return nil, err
}

return TagSet{
includedTags: filteredTags,
excludedTags: excludedTags,
tagSet.tagMap[tag] = included
if included {
tagSet.includedTags = append(tagSet.includedTags, tag)
}
}

return &tagSet, nil
}

func (t TagSet) IsRPCIncluded(rpc *meta.RPC) bool {
Expand All @@ -91,12 +98,12 @@ func (t TagSet) IsRPCIncluded(rpc *meta.RPC) bool {
continue
}

if slices.Contains(t.excludedTags, selector.Value) {
if included, ok := t.tagMap[selector.Value]; ok && !included {
return false
}
}

// If `tags` is empty, all tags are included.
// If no included tags are specified, all tags are included.
if len(t.includedTags) == 0 {
return true
}
Expand All @@ -107,10 +114,11 @@ func (t TagSet) IsRPCIncluded(rpc *meta.RPC) bool {
continue
}

if slices.Contains(t.includedTags, selector.Value) {
if included, ok := t.tagMap[selector.Value]; ok && included {
return true
}
}

// If no included tags are found, the RPC is not included.
return false
}
2 changes: 1 addition & 1 deletion internal/clientgen/javascript.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (js *javascript) Generate(p clientgentypes.GenerateParams) (err error) {
return nil
}

func (js *javascript) writeService(svc *meta.Service, set clientgentypes.ServiceSet, tags clientgentypes.TagSet) error {
func (js *javascript) writeService(svc *meta.Service, set clientgentypes.ServiceSet, tags *clientgentypes.TagSet) error {
// Determine if we have anything worth exposing.
// Either a public RPC or a named type.
isIncluded := hasPublicRPC(svc) && set.Has(svc.Name)
Expand Down
2 changes: 1 addition & 1 deletion internal/clientgen/openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (g *Generator) Generate(p clientgentypes.GenerateParams) (err error) {
return json.Indent(p.Buf, out, "", " ")
}

func (g *Generator) addService(svc *meta.Service, tags clientgentypes.TagSet) error {
func (g *Generator) addService(svc *meta.Service, tags *clientgentypes.TagSet) error {
for _, rpc := range svc.Rpcs {
// streaming endpoints not supported yet
if rpc.StreamingRequest || rpc.StreamingResponse {
Expand Down
2 changes: 1 addition & 1 deletion internal/clientgen/typescript.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (ts *typescript) Generate(p clientgentypes.GenerateParams) (err error) {
return nil
}

func (ts *typescript) writeService(svc *meta.Service, p clientgentypes.ServiceSet, tags clientgentypes.TagSet) error {
func (ts *typescript) writeService(svc *meta.Service, p clientgentypes.ServiceSet, tags *clientgentypes.TagSet) error {
// Determine if we have anything worth exposing.
// Either a public RPC or a named type.
isIncluded := hasPublicRPC(svc) && p.Has(svc.Name)
Expand Down
Loading

0 comments on commit 0143588

Please sign in to comment.