Skip to content

Commit

Permalink
feat: give aipreflect.MethodType an explicit order
Browse files Browse the repository at this point in the history
Allows sorting of methods.
  • Loading branch information
odsod committed Jan 9, 2021
1 parent 43141da commit 361e08a
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 40 deletions.
18 changes: 14 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ all: \
prettier-markdown \
buf-lint \
buf-generate \
go-generate \
go-lint \
go-review \
go-test \
Expand All @@ -22,21 +23,30 @@ include tools/protoc-gen-go-grpc/rules.mk
include tools/protoc-gen-go/rules.mk
include tools/protoc/rules.mk
include tools/semantic-release/rules.mk
include tools/stringer/rules.mk

.PHONY: examples/proto/api-common-protos
examples/proto/api-common-protos:
@git submodule update --init --recursive $@

.PHONY: go-mod-tidy
go-mod-tidy:
$(info [$@] tidying Go module files...)
@go mod tidy -v
.PHONY: go-generate
go-generate: \
reflect/aipreflect/methodtype_string.go

%_string.go: %.go $(stringer)
$(info [stringer] generating $@ from $<)
@go generate ./$<

.PHONY: go-test
go-test:
$(info [$@] running Go tests...)
@go test -count 1 -cover -race ./...

.PHONY: go-mod-tidy
go-mod-tidy:
$(info [$@] tidying Go module files...)
@go mod tidy -v

.PHONY: api-linter-lint
api-linter-lint: $(api_linter_wrapper)
$(info [$@] linting APIs...)
Expand Down
51 changes: 32 additions & 19 deletions reflect/aipreflect/methodtype.go
Original file line number Diff line number Diff line change
@@ -1,54 +1,66 @@
package aipreflect

import "google.golang.org/protobuf/reflect/protoreflect"

// MethodType is an AIP method type.
type MethodType string
type MethodType int

//go:generate stringer -type MethodType -trimprefix MethodType

const (
// MethodTypeNone represents no method type.
MethodTypeNone MethodType = iota

// MethodTypeGet is the method type of the AIP standard Get method.
// See: https://google.aip.dev/131 (Standard methods: Get).
MethodTypeGet MethodType = "Get"
MethodTypeGet

// MethodTypeList is the method type of the AIP standard List method.
// See: https://google.aip.dev/132 (Standard methods: List).
MethodTypeList MethodType = "List"
MethodTypeList

// MethodTypeCreate is the method type of the AIP standard Create method.
// See: https://google.aip.dev/133 (Standard methods: Create).
MethodTypeCreate MethodType = "Create"
MethodTypeCreate

// MethodTypeUpdate is the method type of the AIP standard Update method.
// See: https://google.aip.dev/133 (Standard methods: Update).
MethodTypeUpdate MethodType = "Update"
MethodTypeUpdate

// MethodTypeDelete is the method type of the AIP standard Delete method.
// See: https://google.aip.dev/135 (Standard methods: Delete).
MethodTypeDelete MethodType = "Delete"

// MethodTypeSearch is the method type of the custom AIP method for searching a resource collection.
// See: https://google.aip.dev/136 (Custom methods).
MethodTypeSearch MethodType = "Search"
MethodTypeDelete

// MethodTypeUndelete is the method type of the AIP Undelete method for soft delete.
// See: https://google.aip.dev/164 (Soft delete).
MethodTypeUndelete MethodType = "Undelete"
MethodTypeUndelete

// MethodTypeBatchGet is the method type of the AIP standard BatchGet method.
// See: https://google.aip.dev/231 (Batch methods: Get).
MethodTypeBatchGet MethodType = "BatchGet"
MethodTypeBatchGet

// MethodTypeBatchCreate is the method type of the AIP standard BatchCreate method.
// See: https://google.aip.dev/233 (Batch methods: Create).
MethodTypeBatchCreate MethodType = "BatchCreate"
MethodTypeBatchCreate

// MethodTypeBatchUpdate is the method type of the AIP standard BatchUpdate method.
// See: https://google.aip.dev/234 (Batch methods: Update).
MethodTypeBatchUpdate MethodType = "BatchUpdate"
MethodTypeBatchUpdate

// MethodTypeBatchDelete is the method type of the AIP standard BatchDelete method.
// See: https://google.aip.dev/235 (Batch methods: Delete).
MethodTypeBatchDelete MethodType = "BatchDelete"
MethodTypeBatchDelete

// MethodTypeSearch is the method type of the custom AIP method for searching a resource collection.
// See: https://google.aip.dev/136 (Custom methods).
MethodTypeSearch
)

// NamePrefix returns the method type's method name prefix.
func (s MethodType) NamePrefix() protoreflect.Name {
return protoreflect.Name(s.String())
}

// IsPlural returns true if the method type relates to a plurality of resources.
func (s MethodType) IsPlural() bool {
switch s {
Expand All @@ -59,11 +71,12 @@ func (s MethodType) IsPlural() bool {
MethodTypeBatchUpdate,
MethodTypeBatchDelete:
return true
case MethodTypeCreate,
MethodTypeDelete,
case MethodTypeNone,
MethodTypeGet,
MethodTypeUndelete,
MethodTypeUpdate:
MethodTypeCreate,
MethodTypeUpdate,
MethodTypeDelete,
MethodTypeUndelete:
return false
}
return false
Expand Down
34 changes: 34 additions & 0 deletions reflect/aipreflect/methodtype_string.go

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

4 changes: 2 additions & 2 deletions reflect/aipreflect/resourcedescriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ func (r *ResourceDescriptor) InferMethodName(t MethodType) (protoreflect.Name, e
if r.Plural == "" {
return "", fmt.Errorf("infer %s method name %s: plural not specified", r.Type, t)
}
return protoreflect.Name(t) + protoreflect.Name(r.Plural.UpperCamelCase()), nil
return t.NamePrefix() + protoreflect.Name(r.Plural.UpperCamelCase()), nil
}
if r.Singular == "" {
return "", fmt.Errorf("infer %s method name %s: singular not specified", r.Type, t)
}
return protoreflect.Name(t) + protoreflect.Name(r.Singular.UpperCamelCase()), nil
return t.NamePrefix() + protoreflect.Name(r.Singular.UpperCamelCase()), nil
}
30 changes: 15 additions & 15 deletions reflect/aipregistry/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ func TestNewResources(t *testing.T) {
Singular: "shipper",
Plural: "shippers",
Methods: map[aipreflect.MethodType]protoreflect.FullName{
"Get": "einride.example.freight.v1.FreightService.GetShipper",
"List": "einride.example.freight.v1.FreightService.ListShippers",
"Create": "einride.example.freight.v1.FreightService.CreateShipper",
"Update": "einride.example.freight.v1.FreightService.UpdateShipper",
"Delete": "einride.example.freight.v1.FreightService.DeleteShipper",
aipreflect.MethodTypeGet: "einride.example.freight.v1.FreightService.GetShipper",
aipreflect.MethodTypeList: "einride.example.freight.v1.FreightService.ListShippers",
aipreflect.MethodTypeCreate: "einride.example.freight.v1.FreightService.CreateShipper",
aipreflect.MethodTypeUpdate: "einride.example.freight.v1.FreightService.UpdateShipper",
aipreflect.MethodTypeDelete: "einride.example.freight.v1.FreightService.DeleteShipper",
},
},

Expand All @@ -75,11 +75,11 @@ func TestNewResources(t *testing.T) {
Singular: "shipment",
Plural: "shipments",
Methods: map[aipreflect.MethodType]protoreflect.FullName{
"Get": "einride.example.freight.v1.FreightService.GetShipment",
"List": "einride.example.freight.v1.FreightService.ListShipments",
"Create": "einride.example.freight.v1.FreightService.CreateShipment",
"Update": "einride.example.freight.v1.FreightService.UpdateShipment",
"Delete": "einride.example.freight.v1.FreightService.DeleteShipment",
aipreflect.MethodTypeGet: "einride.example.freight.v1.FreightService.GetShipment",
aipreflect.MethodTypeList: "einride.example.freight.v1.FreightService.ListShipments",
aipreflect.MethodTypeCreate: "einride.example.freight.v1.FreightService.CreateShipment",
aipreflect.MethodTypeUpdate: "einride.example.freight.v1.FreightService.UpdateShipment",
aipreflect.MethodTypeDelete: "einride.example.freight.v1.FreightService.DeleteShipment",
},
},

Expand All @@ -106,11 +106,11 @@ func TestNewResources(t *testing.T) {
Singular: "site",
Plural: "sites",
Methods: map[aipreflect.MethodType]protoreflect.FullName{
"Get": "einride.example.freight.v1.FreightService.GetSite",
"List": "einride.example.freight.v1.FreightService.ListSites",
"Create": "einride.example.freight.v1.FreightService.CreateSite",
"Update": "einride.example.freight.v1.FreightService.UpdateSite",
"Delete": "einride.example.freight.v1.FreightService.DeleteSite",
aipreflect.MethodTypeGet: "einride.example.freight.v1.FreightService.GetSite",
aipreflect.MethodTypeList: "einride.example.freight.v1.FreightService.ListSites",
aipreflect.MethodTypeCreate: "einride.example.freight.v1.FreightService.CreateSite",
aipreflect.MethodTypeUpdate: "einride.example.freight.v1.FreightService.UpdateSite",
aipreflect.MethodTypeDelete: "einride.example.freight.v1.FreightService.DeleteSite",
},
},
},
Expand Down
5 changes: 5 additions & 0 deletions tools/stringer/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module tools/stringer

go 1.13

require golang.org/x/tools v0.0.0-20201208062317-e652b2f42cc7
24 changes: 24 additions & 0 deletions tools/stringer/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20201208062317-e652b2f42cc7 h1:2OSu5vYyX4LVqZAtqZXnFEcN26SDKIJYlEVIRl1tj8U=
golang.org/x/tools v0.0.0-20201208062317-e652b2f42cc7/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
8 changes: 8 additions & 0 deletions tools/stringer/rules.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
stringer_cwd := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
stringer := $(stringer_cwd)/bin/stringer
export PATH := $(PATH):$(dir $(stringer))

$(stringer): $(stringer_cwd)/go.mod
$(info [stringer] building binary...)
@cd $(stringer_cwd) && go build -o $@ golang.org/x/tools/cmd/stringer
@cd $(stringer_cwd) && go mod tidy
5 changes: 5 additions & 0 deletions tools/stringer/tool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//+build tool

package tool

import _ "golang.org/x/tools/cmd/stringer"

0 comments on commit 361e08a

Please sign in to comment.