Skip to content

Commit

Permalink
Add lint and CI
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitaksv committed Mar 27, 2022
1 parent a279d18 commit 8f2cab6
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 29 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: tests

on:
push:
branches:
- '**'
pull_request:

jobs:
test:
strategy:
matrix:
go-version: [ 1.17.x ]
os: [ ubuntu-latest ]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/cache@v2
with:
path: |
~/go/pkg/mod
~/.cache/go-build
~/Library/Caches/go-build
%LocalAppData%\go-build
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Test
run: go test ./...
- name: Run coverage
run: go test -race -coverprofile=coverage.out -covermode=atomic
- name: Upload coverage to Codecov
run: bash <(curl -s https://codecov.io/bash)
24 changes: 24 additions & 0 deletions .github/workflows/code_quality.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: golangci-lint

on:
push:
branches:
- '**'
pull_request:

jobs:
golangci:
strategy:
matrix:
go-version: [ 1.17.x ]
os: [ ubuntu-latest ]
name: lint
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: latest
skip-build-cache: true
skip-pkg-cache: true
28 changes: 28 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: release

on:
push:
tags:
- v*

jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.17

- name: Unshallow
run: git fetch --prune --unshallow

- name: Create release
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
44 changes: 44 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
#########################
#########################
## Golang Linter rules ##
#########################
#########################

# configure golangci-lint
# see https://github.com/golangci/golangci-lint/blob/master/.golangci.example.yml
issues:
exclude-rules:
- path: _test\.go
linters:
- dupl
- gosec
- goconst
linters:
enable:
- gosec
- unconvert
- gocyclo
- goconst
- goimports
- gocritic
disable:
- maligned
- golint
linters-settings:
errcheck:
# report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`;
# default is false: such cases aren't reported by default.
check-blank: true
govet:
# report about shadowed variables
check-shadowing: true
enable:
- fieldalignment
- revive
gocyclo:
# minimal code complexity to report, 30 by default
min-complexity: 15
maligned:
# print struct with more effective memory layout or not, false by default
suggest-new: true
23 changes: 23 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com
before:
hooks:
- go mod tidy
project_name: gendata

build:
skip: true

release:
github:
prerelease: auto

checksum:
name_template: 'checksums.txt'

changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
repos:
- repo: https://github.com/golangci/golangci-lint
rev: v1.41.1
hooks:
- id: golangci-lint
2 changes: 1 addition & 1 deletion cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var genCmd = &cobra.Command{
if err != nil {
return err
}
if err = os.WriteFile(outPath+file.FileName, bs, 0666); err != nil {
if err = os.WriteFile(outPath+file.FileName, bs, 0600); err != nil {
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/generator/formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func WithOptions(opts *Options) Option {
}

type Options struct {
TypeFormatters *meta.TypeFormatters `json:"typeFormatters"`
ClassNameFormatter ClassNameFormatter `json:"classNameFormatter"`
RootClassName string `json:"rootClassName"`
PrefixClassName string `json:"prefixClassName"`
SuffixClassName string `json:"suffixClassName"`
SortProperties bool `json:"sortProperties"`
TypeFormatters *meta.TypeFormatters `json:"typeFormatters"`
ClassNameFormatter ClassNameFormatter `json:"classNameFormatter"`
}

func (o *Options) apply(opts ...Option) error {
Expand Down
6 changes: 3 additions & 3 deletions pkg/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ func WithOptions(opts *Options) Option {
}

type Options struct {
FileNameFormatter FileNameFormatter
FileExtension string
// Split nested object by separate template file
SplitObjectsByFiles bool
FileExtension string
FileNameFormatter FileNameFormatter
}

func (o *Options) apply(opts ...Option) error {
Expand All @@ -45,8 +45,8 @@ type Generator interface {
}

type RenderedFile struct {
FileName string
Content io.ReadWriter
FileName string
}

type generator struct {
Expand Down
32 changes: 17 additions & 15 deletions pkg/generator/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ import (
)

const (
TypeNull string = "null"
TypeInt = "int"
TypeString = "string"
TypeBool = "bool"
TypeFloat = "float"
TypeObject = "object"
TypeArray = "array"
TypeArrayObject = "arrayObject"
TypeArrayInt = "arrayInt"
TypeArrayString = "arrayString"
TypeArrayBool = "arrayBool"
TypeArrayFloat = "arrayFloat"
TypeNull = "null"
TypeInt = "int"
TypeString = "string"
TypeBool = "bool"
TypeFloat = "float"
TypeObject = "object"
TypeArray = "array"
TypeArrayObject = "arrayObject"
TypeArrayInt = "arrayInt"
TypeArrayString = "arrayString"
TypeArrayBool = "arrayBool"
TypeArrayFloat = "arrayFloat"
)

type TypeFormatter func(t Type) string
Expand All @@ -59,9 +59,9 @@ func (n *Meta) Sort() {
}

type Property struct {
Nest *Meta `json:"nest"`
Key Key `json:"key"`
Type Type `json:"type"`
Nest *Meta `json:"nest"`
}

type Key string
Expand Down Expand Up @@ -96,9 +96,10 @@ func (k Key) DotCase() string {
}

type Type struct {
Key Key `json:"key"`
Value string `json:"value"`
Formatters *TypeFormatters `json:"formatters"`

Key Key `json:"key"`
Value string `json:"value"`
}

func (t Type) String() string {
Expand Down Expand Up @@ -164,6 +165,7 @@ func TypeOf(key Key, v interface{}) Type {
}
}

//nolint:gocyclo
func typeOfArray(key Key, arr []interface{}) Type {
t := Type{Key: key}

Expand Down
16 changes: 9 additions & 7 deletions pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ import (
)

type GenRequest struct {
Config *Config `json:"config"`
Tmpl []byte `json:"tmpl"`
Data []byte `json:"data"`
Config *Config `json:"config"`
}

type GenFileRequest struct {
Config *Config `json:"config"`
TmplFile string `json:"tmplFile"`
DataFile string `json:"dataFile"`
ConfigFile string `json:"configFile"`
Config *Config `json:"config"`
}

type PredefinedLangSettingsListRequest struct {
Expand All @@ -45,8 +45,8 @@ type GenResponse struct {
}

type Config struct {
Lang string `json:"lang"`
LangSettings *LangSettings `json:"langSettings,omitempty"`
Lang string `json:"lang"`
DataFormat string `json:"dataFormat"`
// Root object name
RootClassName string `json:"rootClassName"`
Expand Down Expand Up @@ -88,6 +88,7 @@ func (s *service) PredefinedLangSettings(_ context.Context, request *PredefinedL
}, nil
}

//nolint:gocyclo
func (s *service) Gen(ctx context.Context, request *GenRequest) (*GenResponse, error) {
beginTs := time.Now()
s.log.Debug("Gen: request log", zap.Any("request", request))
Expand All @@ -100,21 +101,22 @@ func (s *service) Gen(ctx context.Context, request *GenRequest) (*GenResponse, e
}

var langSettings LangSettings
if request.Config.Lang != "" {
switch {
case request.Config.Lang != "":
prefLangSettings, ok := getPredefinedLangSettings(request.Config.Lang)
if !ok {
return nil, errors.Errorf("config.lang \"%s\" not supported", request.Config.Lang)
}
langSettings = prefLangSettings
} else if request.Config.LangSettings != nil {
case request.Config.LangSettings != nil:
if request.Config.LangSettings.ConfigMapping == nil {
return nil, errors.New("config.langSettings.configMapping is empty")
}
if request.Config.LangSettings.ConfigMapping.TypeMapping == nil {
return nil, errors.New("config.langSettings.configMapping.typeMapping is empty")
}
langSettings = *request.Config.LangSettings
} else {
default:
return nil, errors.Errorf("config.lang and config.langSettings is empty")
}
if request.Config.DataFormat == "" {
Expand Down Expand Up @@ -215,11 +217,11 @@ func (s *service) GenFile(ctx context.Context, request *GenFileRequest) (*GenRes
}

type LangSettings struct {
ConfigMapping *ConfigMapping `json:"configMapping"`
Code string `json:"code"`
Name string `json:"name"`
FileExtension string `json:"fileExtension"`
SplitObjectByFiles bool `json:"splitObjectByFiles"`
ConfigMapping *ConfigMapping `json:"configMapping"`
}

var PredefinedLangSettings = []*LangSettings{
Expand Down
2 changes: 1 addition & 1 deletion pkg/syntax/syntax_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import "testing"
func TestValidate(t *testing.T) {
tests := []struct {
name string
wantErr bool
in []byte
wantErr bool
}{
{
name: "valid Properties",
Expand Down

0 comments on commit 8f2cab6

Please sign in to comment.