Skip to content

Commit

Permalink
Rewrite interface{} to any (#3263)
Browse files Browse the repository at this point in the history
* chore: Rewrite `interface{}` to `any`

* chore: Rewrite `interface{}` to `any`

* chore: Rewirte `interface{}` to `any`

* chore: Rewrite `interface{}` to `any`

* chore: Rewrite `interface{}` to `any`

* chore: Rewrite `interface{}` to `any`

* chore: Rewirte `interface{}` to `any`

* chore: Adjust tests

* chore: Rewrite `interface{}` to `any`

* chore: Rewrite `interface{}` to `any`

* chore: Adjust tests
  • Loading branch information
ikawaha committed Mar 20, 2023
1 parent 825bd01 commit 8888068
Show file tree
Hide file tree
Showing 142 changed files with 1,774 additions and 1,774 deletions.
4 changes: 2 additions & 2 deletions cmd/goa/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (g *Generator) Write(debug bool) error {

var sections []*codegen.SectionTemplate
{
data := map[string]interface{}{
data := map[string]any{
"Command": g.Command,
"CleanupDirs": cleanupDirs(g.Command, g.Output),
"DesignVersion": g.DesignVersion,
Expand Down Expand Up @@ -330,7 +330,7 @@ const mainT = `func main() {
fmt.Println(strings.Join(outputs, "\n"))
}
func fail(msg string, vals ...interface{}) {
func fail(msg string, vals ...any) {
fmt.Fprintf(os.Stderr, msg, vals...)
os.Exit(1)
}
Expand Down
16 changes: 8 additions & 8 deletions codegen/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type (
// Example returns a JSON serialized example value.
Example string
// Default returns the default value if any.
Default interface{}
Default any
}

// BuildFunctionData contains the data needed to generate a constructor
Expand Down Expand Up @@ -264,7 +264,7 @@ func FlagsCode(data []*CommandData) string {
Name: "parse-endpoint-flags",
Source: parseFlagsT,
Data: data,
FuncMap: map[string]interface{}{"printDescription": printDescription},
FuncMap: map[string]any{"printDescription": printDescription},
}
var flagsCode bytes.Buffer
err := section.Write(&flagsCode)
Expand All @@ -282,7 +282,7 @@ func CommandUsage(data *CommandData) *codegen.SectionTemplate {
Name: "cli-command-usage",
Source: commandUsageT,
Data: data,
FuncMap: map[string]interface{}{"printDescription": printDescription},
FuncMap: map[string]any{"printDescription": printDescription},
}
}

Expand All @@ -293,7 +293,7 @@ func PayloadBuilderSection(buildFunction *BuildFunctionData) *codegen.SectionTem
Name: "cli-build-payload",
Source: buildPayloadT,
Data: buildFunction,
FuncMap: map[string]interface{}{
FuncMap: map[string]any{
"fieldCode": fieldCode,
},
}
Expand All @@ -308,7 +308,7 @@ func PayloadBuilderSection(buildFunction *BuildFunctionData) *codegen.SectionTem
// description is the flag description
// required determines if the flag is required
// example is an example value for the flag
func NewFlagData(svcn, en, name, typeName, description string, required bool, example, def interface{}) *FlagData {
func NewFlagData(svcn, en, name, typeName, description string, required bool, example, def any) *FlagData {
ex := jsonExample(example)
fn := goifyTerms(svcn, en, name)
return &FlagData{
Expand All @@ -326,7 +326,7 @@ func NewFlagData(svcn, en, name, typeName, description string, required bool, ex
// FieldLoadCode returns the code used in the build payload function that
// initializes one of the payload object fields. It returns the initialization
// code and a boolean indicating whether the code requires an "err" variable.
func FieldLoadCode(f *FlagData, argName, argTypeName, validate string, defaultValue interface{}, payload expr.DataType, payloadRef string) (string, bool) {
func FieldLoadCode(f *FlagData, argName, argTypeName, validate string, defaultValue any, payload expr.DataType, payloadRef string) (string, bool) {
var (
code string
declErr bool
Expand Down Expand Up @@ -410,13 +410,13 @@ func flagType(tname string) string {
}

// jsonExample generates a json example
func jsonExample(v interface{}) string {
func jsonExample(v any) string {
// In JSON, keys must be a string. But goa allows map keys to be anything.
r := reflect.ValueOf(v)
if r.Kind() == reflect.Map {
keys := r.MapKeys()
if keys[0].Kind() != reflect.String {
a := make(map[string]interface{}, len(keys))
a := make(map[string]any, len(keys))
var kstr string
for _, k := range keys {
switch t := k.Interface().(type) {
Expand Down
26 changes: 13 additions & 13 deletions codegen/example/example_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,30 @@ func exampleCLIMain(genpkg string, root *expr.RootExpr, svr *expr.ServerExpr) *c
&codegen.SectionTemplate{
Name: "cli-main-start",
Source: cliMainStartT,
Data: map[string]interface{}{
Data: map[string]any{
"Server": svrdata,
},
FuncMap: map[string]interface{}{
FuncMap: map[string]any{
"join": strings.Join,
},
},
&codegen.SectionTemplate{
Name: "cli-main-var-init",
Source: cliMainVarInitT,
Data: map[string]interface{}{
Data: map[string]any{
"Server": svrdata,
},
FuncMap: map[string]interface{}{
FuncMap: map[string]any{
"join": strings.Join,
},
},
&codegen.SectionTemplate{
Name: "cli-main-endpoint-init",
Source: cliMainEndpointInitT,
Data: map[string]interface{}{
Data: map[string]any{
"Server": svrdata,
},
FuncMap: map[string]interface{}{
FuncMap: map[string]any{
"join": strings.Join,
"toUpper": strings.ToUpper,
},
Expand All @@ -77,11 +77,11 @@ func exampleCLIMain(genpkg string, root *expr.RootExpr, svr *expr.ServerExpr) *c
&codegen.SectionTemplate{
Name: "cli-main-usage",
Source: cliMainUsageT,
Data: map[string]interface{}{
Data: map[string]any{
"APIName": root.API.Name,
"Server": svrdata,
},
FuncMap: map[string]interface{}{
FuncMap: map[string]any{
"toUpper": strings.ToUpper,
"join": strings.Join,
},
Expand All @@ -91,7 +91,7 @@ func exampleCLIMain(genpkg string, root *expr.RootExpr, svr *expr.ServerExpr) *c
}

const (
// input: map[string]interface{}{"Server": *Data}
// input: map[string]any{"Server": *Data}
cliMainStartT = `func main() {
var (
hostF = flag.String("host", {{ printf "%q" .Server.DefaultHost.Name }}, "Server host (valid values: {{ (join .Server.AvailableHosts ", ") }})")
Expand All @@ -107,7 +107,7 @@ const (
flag.Parse()
`

// input: map[string]interface{}{"Server": *Data}
// input: map[string]any{"Server": *Data}
cliMainVarInitT = `var (
addr string
timeout int
Expand Down Expand Up @@ -163,10 +163,10 @@ const (
}
`

// input: map[string]interface{}{"Server": *Data}
// input: map[string]any{"Server": *Data}
cliMainEndpointInitT = `var(
endpoint goa.Endpoint
payload interface{}
payload any
err error
)
{
Expand Down Expand Up @@ -204,7 +204,7 @@ const (
}
`

// input: map[string]interface{}{"APIName": string, "Server": *Data}
// input: map[string]any{"APIName": string, "Server": *Data}
cliMainUsageT = `
func usage() {
fmt.Fprintf(os.Stderr, ` + "`" + `%s is a command line client for the {{ .APIName }} API.
Expand Down
18 changes: 9 additions & 9 deletions codegen/example/example_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,35 +79,35 @@ func exampleSvrMain(genpkg string, root *expr.RootExpr, svr *expr.ServerExpr) *c
{
Name: "server-main-start",
Source: mainStartT,
Data: map[string]interface{}{
Data: map[string]any{
"Server": svrdata,
},
FuncMap: map[string]interface{}{
FuncMap: map[string]any{
"join": strings.Join,
},
}, {
Name: "server-main-logger",
Source: mainLoggerT,
Data: map[string]interface{}{
Data: map[string]any{
"APIPkg": apiPkg,
},
}, {
Name: "server-main-services",
Source: mainSvcsT,
Data: map[string]interface{}{
Data: map[string]any{
"APIPkg": apiPkg,
"Services": svcData,
},
FuncMap: map[string]interface{}{
FuncMap: map[string]any{
"mustInitServices": mustInitServices,
},
}, {
Name: "server-main-endpoints",
Source: mainEndpointsT,
Data: map[string]interface{}{
Data: map[string]any{
"Services": svcData,
},
FuncMap: map[string]interface{}{
FuncMap: map[string]any{
"mustInitServices": mustInitServices,
},
}, {
Expand All @@ -116,11 +116,11 @@ func exampleSvrMain(genpkg string, root *expr.RootExpr, svr *expr.ServerExpr) *c
}, {
Name: "server-main-handler",
Source: mainServerHndlrT,
Data: map[string]interface{}{
Data: map[string]any{
"Server": svrdata,
"Services": svcData,
},
FuncMap: map[string]interface{}{
FuncMap: map[string]any{
"goify": codegen.Goify,
"join": strings.Join,
"toUpper": strings.ToUpper,
Expand Down
2 changes: 1 addition & 1 deletion codegen/example/server_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func buildHostData(host *expr.HostExpr) *HostData {
}

// convertToString converts primitive type to a string.
func convertToString(vals ...interface{}) []string {
func convertToString(vals ...any) []string {
str := make([]string, len(vals))
for i, v := range vals {
switch t := v.(type) {
Expand Down
10 changes: 5 additions & 5 deletions codegen/example/testdata/example_cli_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const (
}
var (
endpoint goa.Endpoint
payload interface{}
payload any
err error
)
{
Expand Down Expand Up @@ -157,7 +157,7 @@ func indent(s string) string {
}
var (
endpoint goa.Endpoint
payload interface{}
payload any
err error
)
{
Expand Down Expand Up @@ -285,7 +285,7 @@ func indent(s string) string {
}
var (
endpoint goa.Endpoint
payload interface{}
payload any
err error
)
{
Expand Down Expand Up @@ -404,7 +404,7 @@ func indent(s string) string {
}
var (
endpoint goa.Endpoint
payload interface{}
payload any
err error
)
{
Expand Down Expand Up @@ -533,7 +533,7 @@ func indent(s string) string {
}
var (
endpoint goa.Endpoint
payload interface{}
payload any
err error
)
{
Expand Down
4 changes: 2 additions & 2 deletions codegen/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ type (
// renders the section text.
Source string
// FuncMap lists the functions used to render the templates.
FuncMap map[string]interface{}
FuncMap map[string]any
// Data used as input of template.
Data interface{}
Data any
}
)

Expand Down
4 changes: 2 additions & 2 deletions codegen/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ type (
)

// TemplateFuncs lists common template helper functions.
func TemplateFuncs() map[string]interface{} {
return map[string]interface{}{
func TemplateFuncs() map[string]any {
return map[string]any{
"commandLine": CommandLine,
"comment": Comment,
}
Expand Down
10 changes: 5 additions & 5 deletions codegen/go_transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func transformArray(source, target *expr.Array, sourceVar, targetVar string, new
if err := IsCompatible(source.ElemType.Type, target.ElemType.Type, sourceVar+"[0]", targetVar+"[0]"); err != nil {
return "", err
}
data := map[string]interface{}{
data := map[string]any{
"ElemTypeRef": ta.TargetCtx.Scope.Ref(target.ElemType, ta.TargetCtx.Pkg(target.ElemType)),
"SourceElem": source.ElemType,
"TargetElem": target.ElemType,
Expand All @@ -325,7 +325,7 @@ func transformMap(source, target *expr.Map, sourceVar, targetVar string, newVar
if err := IsCompatible(source.ElemType.Type, target.ElemType.Type, sourceVar+"[*]", targetVar+"[*]"); err != nil {
return "", err
}
data := map[string]interface{}{
data := map[string]any{
"KeyTypeRef": ta.TargetCtx.Scope.Ref(target.KeyType, ta.TargetCtx.Pkg(target.KeyType)),
"ElemTypeRef": ta.TargetCtx.Scope.Ref(target.ElemType, ta.TargetCtx.Pkg(target.ElemType)),
"SourceKey": source.KeyType,
Expand Down Expand Up @@ -382,7 +382,7 @@ func transformUnion(source, target *expr.AttributeExpr, sourceVar, targetVar str
// Need to type assert targetVar before assigning field values.
ta.TargetCtx.IsInterface = true

data := map[string]interface{}{
data := map[string]any{
"SourceTypeRefs": sourceTypeRefs,
"SourceTypes": srcUnion.Values,
"TargetTypes": tgtUnion.Values,
Expand Down Expand Up @@ -415,7 +415,7 @@ func transformUnionToObject(source, target *expr.AttributeExpr, sourceVar, targe
sourceTypeRefs[i] = ta.SourceCtx.Scope.Ref(st.Attribute, ta.SourceCtx.Pkg(st.Attribute))
sourceTypeNames[i] = st.Name
}
data := map[string]interface{}{
data := map[string]any{
"NewVar": newVar,
"TargetVar": targetVar,
"TypeRef": ta.TargetCtx.Scope.Ref(target, ta.TargetCtx.Pkg(target)),
Expand Down Expand Up @@ -451,7 +451,7 @@ func transformObjectToUnion(source, target *expr.AttributeExpr, sourceVar, targe
unionTypes[i] = tt.Name
targetTypeRefs[i] = ta.TargetCtx.Scope.Ref(tt.Attribute, ta.TargetCtx.Pkg(tt.Attribute))
}
data := map[string]interface{}{
data := map[string]any{
"NewVar": newVar,
"TargetVar": targetVar,
"TypeRef": ta.TargetCtx.Scope.Ref(target, ta.TargetCtx.Pkg(target)),
Expand Down
2 changes: 1 addition & 1 deletion codegen/go_transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ const (
}
}
{
var zero interface{}
var zero any
if target.Any == zero {
target.Any = "something"
}
Expand Down
4 changes: 2 additions & 2 deletions codegen/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func Header(title, pack string, imports []*ImportSpec) *SectionTemplate {
return &SectionTemplate{
Name: "source-header",
Source: headerT,
Data: map[string]interface{}{
Data: map[string]any{
"Title": title,
"ToolVersion": goa.Version(),
"Pkg": pack,
Expand All @@ -25,7 +25,7 @@ func AddImport(section *SectionTemplate, imprts ...*ImportSpec) {
return
}
var specs []*ImportSpec
if data, ok := section.Data.(map[string]interface{}); ok {
if data, ok := section.Data.(map[string]any); ok {
if imports, ok := data["Imports"]; ok {
specs = imports.([]*ImportSpec)
}
Expand Down
2 changes: 1 addition & 1 deletion codegen/service/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const serviceClientMethodT = `
{{- end }}
func (c *{{ .ClientVarName }}) {{ .VarName }}(ctx context.Context, {{ if .PayloadRef }}p {{ .PayloadRef }}{{ end }}{{ if .MethodData.SkipRequestBodyEncodeDecode}}, req io.ReadCloser{{ end }}) ({{ if $resultType }}res {{ $resultType }}, {{ end }}{{ if .MethodData.SkipResponseBodyEncodeDecode }}resp io.ReadCloser, {{ end }}err error) {
{{- if or $resultType .MethodData.SkipResponseBodyEncodeDecode }}
var ires interface{}
var ires any
{{- end }}
{{ if or $resultType .MethodData.SkipResponseBodyEncodeDecode }}ires{{ else }}_{{ end }}, err = c.{{ .VarName}}Endpoint(ctx, {{ if .MethodData.SkipRequestBodyEncodeDecode }}&{{ .RequestStruct }}{ {{ if .PayloadRef }}Payload: p, {{ end }}Body: req }{{ else if .PayloadRef }}p{{ else }}nil{{ end }})
{{- if not (or $resultType .MethodData.SkipResponseBodyEncodeDecode) }}
Expand Down
Loading

0 comments on commit 8888068

Please sign in to comment.