Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependencies Upgrade 2024-08 #428

Merged
merged 4 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/actions/prepare/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ runs:
- name: "Setup Go"
uses: actions/setup-go@v4
with:
go-version: '~1.21.5' # go 1.20 has issues: `go test -covermode=atomic` fails when files doesn't have empty line at the end
# go-version-file: 'go.mod'
# go-version: '~1.21.5' # go 1.20 has issues: `go test -covermode=atomic` fails when files doesn't have empty line at the end
go-version-file: 'go.mod'
cache-dependency-path: "**/*.sum"
- name: "Install CI Tools"
shell: bash
Expand Down
2 changes: 1 addition & 1 deletion .github/actions/verify/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ runs:
- name: "Run Tests with Coverage"
shell: bash
run: |
make test report SHELL=/bin/bash ARGS="-covermode atomic -coverpkg ${{ inputs.coverpkg || './pkg/...' }}"
make test report SHELL=/bin/bash GOTESTSUM_FORMAT="github-actions" ARGS="-covermode atomic -coverpkg ${{ inputs.coverpkg || './pkg/...' }}"
go tool cover -func=dist/coverage.out -o dist/coverage-func.out

- name: "Code Quality Analysis"
Expand Down
2 changes: 1 addition & 1 deletion Module.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ generates:

binaries:
- package: github.com/golangci/golangci-lint/cmd/golangci-lint
version: v1.55.2
version: v1.59.1
16 changes: 2 additions & 14 deletions build/ci/golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@

# Ref: https://golangci-lint.run/usage/configuration/
run:
go: '1.22'
tests: false
skip-dirs:
- cmd
- pkg/security/example
modules-download-mode: readonly

output:
Expand All @@ -35,8 +33,6 @@ linters:
# - asciicheck
# - ...
disable:
- maligned # deprecated
- scopelint # deprecated

linters-settings:
# https://golangci-lint.run/usage/linters/#exhaustive:
Expand All @@ -50,8 +46,6 @@ linters-settings:
# So we allow plain error switching for now.
# TODO we should revise this setting when patterns are more consistent within golang communities
comparison: false
staticcheck:
go: "1.16"

severity:
# - Code climate: https://docs.codeclimate.com/docs/issues#issue-severity
Expand All @@ -73,11 +67,5 @@ severity:
issues:
max-issues-per-linter: 0
max-same-issues: 0
exclude-rules:
# - linters:
# - errcheck
# - dupl
# path: _test\.go
# text: "SA9003:"
# source: "^//go:generate "


4 changes: 2 additions & 2 deletions cmd/lanai-cli/cmdutils/go_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@
ShellUseWorkingDir(),
ShellStdOut(os.Stdout),
}
for _, v := range mod.Replace {
for i, v := range mod.Replace {
if isInvalidReplace(&v) {

Check failure on line 153 in cmd/lanai-cli/cmdutils/go_cmd.go

View workflow job for this annotation

GitHub Actions / Test, Analyze Code & Report

G601: Implicit memory aliasing in for loop. (gosec)
ret = append(ret, &v)
ret = append(ret, &mod.Replace[i])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the github action annotation is referring to the original code on line 154, right?

Copy link
Contributor Author

@stonedu1011 stonedu1011 Aug 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a bug fix that brought to my attention after I enabled linters on /cmd.

If you are asking about the github-action output format for tests, it's in .github/actions/verify/action.yml, and it's already in main

cmdOpts = append(cmdOpts, dropReplaceCmd(v.Old.Path, v.Old.Version, opts))
}
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/lanai-cli/codegen/generator/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ func FormatFile(path string, typeHint FileType) error {

func formatFile(path string, formatter TextFormatter) error {
content, fi, e := readFile(path)
if e != nil {
return e
}
formatted, e := formatter.Format(content)
if e != nil {
return e
Expand Down
4 changes: 4 additions & 0 deletions cmd/lanai-cli/codegen/generator/group_security.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,14 @@ func (g SecurityGroup) CustomizeData(data GenerationData) error {
switch g.Components.Security.Authentication.Method {
case AuthOAuth2:
modules = append(modules, LanaiSecurity, LanaiResServer)
default:
// nothing to add
}
switch g.Components.Security.Access.Preset {
case AccessPresetOPA:
modules = append(modules, LanaiOPA)
default:
// nothing to add
}
sec := ResolveEnabledLanaiModules(modules...)
data.ProjectMetadata().EnabledModules.Add(sec.Values()...)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func ExternalImportsFromFormat(element interface{}) (result []string) {

func MatchesFormat(element interface{}, specificType string) bool {
schema, err := ConvertToSchemaRef(element)
if err != nil && schema.Value.Type != openapi3.TypeString {
if err != nil || schema == nil || schema.Value.Type == nil || !schema.Value.Type.Is(openapi3.TypeString) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch

return false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ func (o Operation) ResponseRefsUsed() (result []string) {
result = append(result, resp.RefsUsed()...)
break
}
break
}
return result
}
Expand Down
61 changes: 31 additions & 30 deletions cmd/lanai-cli/codegen/generator/template_funcs/lanai/property.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ func ShouldHavePointer(p Property) bool {
if schema.Value.Nullable {
return true
}
switch schema.Value.Type {
case openapi3.TypeObject:
switch {
case schema.Value.Type.Is(openapi3.TypeObject):
return isRequired
case openapi3.TypeArray, openapi3.TypeBoolean:
case schema.Value.Type.Is(openapi3.TypeArray), schema.Value.Type.Is(openapi3.TypeBoolean):
return false
default:
if zeroValueIsValid(schema) {
Expand All @@ -105,15 +105,15 @@ func zeroValue(schema *openapi3.Schema) reflect.Value {
return zvValue
}
func schemaToGoType(val *openapi3.Schema) (result reflect.Type) {
switch val.Type {
case openapi3.TypeBoolean, openapi3.TypeNumber, openapi3.TypeInteger, openapi3.TypeString:
switch {
case val.Type.Is(openapi3.TypeBoolean), val.Type.Is(openapi3.TypeNumber), val.Type.Is(openapi3.TypeInteger), val.Type.Is(openapi3.TypeString):
result = schemaToGoBaseTypes(val)
case openapi3.TypeArray:
case val.Type.Is(openapi3.TypeArray):
itemsType := schemaToGoType(val.Items.Value)
if itemsType != nil {
result = reflect.SliceOf(itemsType)
}
case openapi3.TypeObject:
case val.Type.Is(openapi3.TypeObject):
// Do nothing
default:
logger.Warnf("getType: type %v doesn't have corresponding mapping", val.Type)
Expand All @@ -122,12 +122,12 @@ func schemaToGoType(val *openapi3.Schema) (result reflect.Type) {
return result
}
func schemaToGoBaseTypes(val *openapi3.Schema) (result reflect.Type) {
switch val.Type {
case openapi3.TypeBoolean:
switch {
case val.Type.Is(openapi3.TypeBoolean):
result = reflect.TypeOf(true)
case openapi3.TypeNumber:
case val.Type.Is(openapi3.TypeNumber):
result = reflect.TypeOf(1.1)
case openapi3.TypeInteger:
case val.Type.Is(openapi3.TypeInteger):
var v interface{}
switch val.Format {
case "int32":
Expand All @@ -138,7 +138,7 @@ func schemaToGoBaseTypes(val *openapi3.Schema) (result reflect.Type) {
v = 1
}
result = reflect.TypeOf(v)
case openapi3.TypeString:
case val.Type.Is(openapi3.TypeString):
result = reflect.TypeOf("string")
default:
result = nil
Expand All @@ -155,8 +155,10 @@ func valuePassesValidation(schema *openapi3.Schema, value reflect.Value) (result
return false
}
}
fallthrough
default:
return valueIsWithinSchemaLimits(schema, value)
}
return valueIsWithinSchemaLimits(schema, value)
}

func valueIsWithinSchemaLimits(schema *openapi3.Schema, value reflect.Value) bool {
Expand All @@ -176,30 +178,30 @@ func valueIsWithinSchemaLimits(schema *openapi3.Schema, value reflect.Value) boo
fallthrough
case reflect.Slice:
return !isOutOfBounds(value.Len(), min, max)
default:
return false
}

return false
}

func limitsForSchema(element *openapi3.Schema) (min, max string) {
switch element.Type {
case "array":
switch {
case element.Type.Is(openapi3.TypeArray):
if element.MinItems > 0 {
min = strconv.FormatUint(element.MinItems, 10)
}
if element.MaxItems != nil {
max = strconv.FormatUint(*element.MaxItems, 10)
}
case "number":
case element.Type.Is(openapi3.TypeNumber):
fallthrough
case "integer":
case element.Type.Is(openapi3.TypeInteger):
if element.Min != nil {
min = strconv.FormatFloat(*element.Min, 'f', -1, 64)
}
if element.Max != nil {
max = strconv.FormatFloat(*element.Max, 'f', -1, 64)
}
case "string":
case element.Type.Is(openapi3.TypeString):
if element.MinLength > 0 {
min = strconv.FormatUint(element.MinLength, 10)
}
Expand All @@ -210,25 +212,24 @@ func limitsForSchema(element *openapi3.Schema) (min, max string) {
return min, max
}
func isOutOfBounds[V int | int32 | int64 | float64](value V, min, max string) (result bool) {
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.Int:
switch v := interface{}(value).(type) {
case int:
minConverted, _ := strconv.Atoi(min)
maxConverted, _ := strconv.Atoi(max)
result = minConverted > any(value).(int) || maxConverted < any(value).(int)
case reflect.Int32:
result = minConverted > v || maxConverted < v
case int32:
minConverted, _ := strconv.ParseInt(min, 10, 32)
maxConverted, _ := strconv.ParseInt(max, 10, 32)
result = int32(minConverted) > any(value).(int32) || int32(maxConverted) < any(value).(int32)
case reflect.Int64:
result = int32(minConverted) > v || int32(maxConverted) < v
case int64:
minConverted, _ := strconv.ParseInt(min, 10, 64)
maxConverted, _ := strconv.ParseInt(max, 10, 64)
result = minConverted > any(value).(int64) || maxConverted < any(value).(int64)
result = minConverted > v || maxConverted < v

case reflect.Float64:
case float64:
minConverted, _ := strconv.ParseFloat(min, 64)
maxConverted, _ := strconv.ParseFloat(max, 64)
result = minConverted > any(value).(float64) || maxConverted < any(value).(float64)
result = minConverted > v || maxConverted < v
}
return result
}
6 changes: 3 additions & 3 deletions cmd/lanai-cli/codegen/generator/template_funcs/lanai/regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package lanai

import (
"crypto/md5"
"crypto/sha256"
"errors"
"fmt"
"github.com/getkin/kin-openapi/openapi3"
Expand All @@ -42,7 +42,7 @@ type Regex struct {
}

func NewRegex(value openapi3.Schema) (*Regex, error) {
if value.Type != "string" {
if !value.Type.Is(openapi3.TypeString) {
return nil, errors.New("schema is not a string type for regex")
}

Expand Down Expand Up @@ -73,7 +73,7 @@ func generateNameFromRegex(regex string) string {
}
}

hashedString := strings.ToUpper(fmt.Sprintf("%x", md5.Sum([]byte(regex))))[0:5]
hashedString := strings.ToUpper(fmt.Sprintf("%x", sha256.Sum224([]byte(regex))))[0:5]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what prompted this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

linter, use of md5 result in lint issue

return fmt.Sprintf("regex%v", hashedString)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (r RequestBody) ExternalImports() (result []string) {
for _, schema := range r.Schemas() {
result = append(result, lanaiutil.ExternalImportsFromFormat(schema)...)
if schema.Ref == "" {
if schema.Value.Type != "" && schema.Value.Type != "object" {
if schema.Value != nil && schema.Value.Type != nil && !schema.Value.Type.Is(openapi3.TypeObject) {
result = append(result, lanaiutil.JSON_IMPORT_PATH)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ func (s Schema) StructProperties() (result openapi3.SchemaRefs) {

func (s Schema) AllProperties() (result openapi3.Schemas) {
result = make(openapi3.Schemas)
if s.Data.Value.Type == openapi3.TypeObject || s.Data.Value.Type == "" {
if s.Data.Value.Type == nil || s.Data.Value.Type.Is(openapi3.TypeObject) {
result = s.Data.Value.Properties
}
return result
}

func (s Schema) Type() string {
if s.Data.Value.Type == "" {
if s.Data.Value.Type == nil || len(*s.Data.Value.Type) == 0 {
return openapi3.TypeObject
}
return s.Data.Value.Type
return (*s.Data.Value.Type)[0]
}
func (s Schema) HasAdditionalProperties() bool {
return openapi.SchemaRef(*s.Data).HasAdditionalProperties()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ func (b baseType) ToText() (string, error) {
return schemaToGoBaseTypes(schema.Value).String(), nil
}
func schemaToGoBaseTypes(val *openapi3.Schema) (result reflect.Type) {
switch val.Type {
case openapi3.TypeBoolean:
switch {
case val.Type.Is(openapi3.TypeBoolean):
result = reflect.TypeOf(true)
case openapi3.TypeNumber:
case val.Type.Is(openapi3.TypeNumber):
result = reflect.TypeOf(1.1)
case openapi3.TypeInteger:
case val.Type.Is(openapi3.TypeInteger):
var v interface{}
switch val.Format {
case "int32":
Expand All @@ -61,7 +61,7 @@ func schemaToGoBaseTypes(val *openapi3.Schema) (result reflect.Type) {
v = 1
}
result = reflect.TypeOf(v)
case openapi3.TypeString:
case val.Type.Is(openapi3.TypeString):
result = reflect.TypeOf("string")
default:
result = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ func NewDataTypeToTextTranslator(element interface{}, opts ...func(option *trans
return NewDefaultType(element, opts...)
}
var translator ToTextTranslator
switch schema.Value.Type {
case openapi3.TypeNumber, openapi3.TypeInteger, openapi3.TypeBoolean:
switch {
case schema.Value.Type.Is(openapi3.TypeNumber), schema.Value.Type.Is(openapi3.TypeInteger), schema.Value.Type.Is(openapi3.TypeBoolean):
translator = NewBaseType(element, opts...)
case openapi3.TypeString:
case schema.Value.Type.Is(openapi3.TypeString):
translator = NewStringType(element, opts...)
case openapi3.TypeArray:
case schema.Value.Type.Is(openapi3.TypeArray):
translator = NewArrayType(element, opts...)
case openapi3.TypeObject:
case schema.Value.Type.Is(openapi3.TypeObject):
translator = NewObjectType(element, opts...)
default:
translator = NewDefaultType(element, opts...)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func (o objectType) ToText() (result string, err error) {
if err != nil {
return "", err
}
switch schema.Value.Type {
case openapi3.TypeObject:
switch {
case schema.Value.Type.Is(openapi3.TypeObject):
if len(schema.Value.Properties) == 0 {
if schema.Value.AdditionalProperties.Has != nil && *schema.Value.AdditionalProperties.Has {
result = "map[string]interface{}"
Expand Down
Loading
Loading