Skip to content

Commit

Permalink
Merge pull request #1 from grafana/github-action-ci
Browse files Browse the repository at this point in the history
Setup basic CI pipeline as a GitHub action
  • Loading branch information
K-Phoen authored Sep 8, 2023
2 parents 2f04a86 + 327fe5d commit 4ce7ccf
Show file tree
Hide file tree
Showing 21 changed files with 265 additions and 209 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: CI
on: [pull_request]

env:
GO_VERSION: '1.21'

jobs:
linters:
name: Linters
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}

- name: Install vendors
run: make deps

- name: Run golangci-lint
run: make lint

tests:
name: Tests
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- uses: actions/setup-go@v3
with:
go-version: 1.21
cache: true

- name: Install vendors
run: make deps

- name: Tests
run: make tests
45 changes: 45 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
linters:
enable-all: true
disable:
- exhaustruct
- wrapcheck
- nolintlint
- wastedassign
- varnamelen
- forcetypeassert
- goerr113
- exhaustivestruct
- ireturn
- funlen
- forbidigo
- godot
- godox
- cyclop
- prealloc
- dupl
- lll
- maligned
- goconst
- depguard
- exhaustive
- wsl
- unparam
- gomoddirectives
- gofumpt
- nestif
# deprecated
- deadcode
- scopelint
- nosnakecase
- golint
- interfacer
- structcheck
- ifshort
- varcheck

linters-settings:
errcheck:
ignore: fmt:.*

run:
modules-download-mode: vendor
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
GOLANGCI_LINT_VERSION='v1.54.2'

.PHONY: lint
lint:
docker run --rm -v $(shell pwd):/app -w /app golangci/golangci-lint:${GOLANGCI_LINT_VERSION}-alpine golangci-lint run -c .golangci.yaml

.PHONY: tests
tests:
go test -v ./...

.PHONY: deps
deps:
go mod vendor
8 changes: 0 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,4 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)

// replace github.com/grafana/grafana => ../grafana

// replace github.com/grafana/codejen => ../codejen

// replace github.com/grafana/thema => ../thema

replace cuelang.org/go => github.com/sdboyer/cue v0.5.0-beta.2.0.20221218111347-341999f48bdb

replace github.com/deepmap/oapi-codegen => github.com/spinillos/oapi-codegen v1.12.5-0.20230417081915-2945b61c0b1c
1 change: 1 addition & 0 deletions internal/ast/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func (generator *BuilderGenerator) structObjectToBuilder(file *File, object Obje
for _, field := range structType.Fields {
if generator.fieldHasStaticValue(field) {
builder.Initializations = append(builder.Initializations, generator.structFieldToStaticInitialization(field))

continue
}

Expand Down
4 changes: 2 additions & 2 deletions internal/ast/compiler/disjunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (pass *DisjunctionToType) processDisjunction(def ast.DisjunctionType) ast.T
// Ex: type | null
if len(def.Branches) == 2 && def.Branches.HasNullType() {
finalType := def.Branches.NonNullTypes()[0]
//finalType.Nullable = true
// finalType.Nullable = true

return finalType
}
Expand Down Expand Up @@ -134,7 +134,7 @@ func (pass *DisjunctionToType) processDisjunction(def ast.DisjunctionType) ast.T

return ast.RefType{
ReferredType: newTypeName,
//Nullable: def.Branches.HasNullType(),
// Nullable: def.Branches.HasNullType(),
}
}

Expand Down
51 changes: 22 additions & 29 deletions internal/jennies/golang/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,18 @@ import (
"github.com/grafana/cog/internal/tools"
)

type GoBuilder struct {
type Builder struct {
}

func (jenny *GoBuilder) JennyName() string {
func (jenny *Builder) JennyName() string {
return "GoBuilder"
}

func (jenny *GoBuilder) Generate(builders []ast.Builder) (codejen.Files, error) {
func (jenny *Builder) Generate(builders []ast.Builder) (codejen.Files, error) {
files := codejen.Files{}

for _, builder := range builders {
output, err := jenny.generateBuilder(builders, builder)
if err != nil {
return nil, err
}
output := jenny.generateBuilder(builders, builder)

files = append(
files,
Expand All @@ -34,7 +31,7 @@ func (jenny *GoBuilder) Generate(builders []ast.Builder) (codejen.Files, error)
return files, nil
}

func (jenny *GoBuilder) generateBuilder(builders ast.Builders, builder ast.Builder) ([]byte, error) {
func (jenny *Builder) generateBuilder(builders ast.Builders, builder ast.Builder) []byte {
var buffer strings.Builder

buffer.WriteString(fmt.Sprintf("package %s\n\n", strings.ToLower(builder.For.Name)))
Expand Down Expand Up @@ -82,10 +79,10 @@ func (builder *Builder) Internal() *types.%s {
buffer.WriteString("}\n")
buffer.WriteString("}\n")

return []byte(buffer.String()), nil
return []byte(buffer.String())
}

func (jenny *GoBuilder) generateConstructor(builders ast.Builders, builder ast.Builder) string {
func (jenny *Builder) generateConstructor(builders ast.Builders, builder ast.Builder) string {
var buffer strings.Builder

typeName := tools.UpperCamelCase(builder.For.Name)
Expand Down Expand Up @@ -140,7 +137,7 @@ func New(%[2]soptions ...Option) (Builder, error) {
return buffer.String()
}

func (jenny *GoBuilder) formatFieldPath(fieldPath string) string {
func (jenny *Builder) formatFieldPath(fieldPath string) string {
parts := strings.Split(fieldPath, ".")
formatted := make([]string, 0, len(parts))

Expand All @@ -151,7 +148,7 @@ func (jenny *GoBuilder) formatFieldPath(fieldPath string) string {
return strings.Join(formatted, ".")
}

func (jenny *GoBuilder) generateInitAssignment(builders ast.Builders, builder ast.Builder, assignment ast.Assignment) string {
func (jenny *Builder) generateInitAssignment(builders ast.Builders, builder ast.Builder, assignment ast.Assignment) string {
fieldPath := jenny.formatFieldPath(assignment.Path)
valueType := assignment.ValueType

Expand All @@ -173,13 +170,13 @@ func (jenny *GoBuilder) generateInitAssignment(builders ast.Builders, builder as

generatedConstraints := strings.Join(jenny.constraints(argName, assignment.Constraints), "\n")
if generatedConstraints != "" {
generatedConstraints = generatedConstraints + "\n\n"
generatedConstraints += "\n\n"
}

return generatedConstraints + fmt.Sprintf("%[1]s: %[3]s%[2]s", fieldPath, argName, asPointer)
}

func (jenny *GoBuilder) generateOption(builders ast.Builders, builder ast.Builder, def ast.Option) string {
func (jenny *Builder) generateOption(builders ast.Builders, builder ast.Builder, def ast.Option) string {
var buffer strings.Builder

for _, commentLine := range def.Comments {
Expand Down Expand Up @@ -219,7 +216,7 @@ func (jenny *GoBuilder) generateOption(builders ast.Builders, builder ast.Builde
return buffer.String()
}

func (jenny *GoBuilder) typeHasBuilder(builders ast.Builders, builder ast.Builder, t ast.Type) (string, bool) {
func (jenny *Builder) typeHasBuilder(builders ast.Builders, builder ast.Builder, t ast.Type) (string, bool) {
if t.Kind() != ast.KindRef {
return "", false
}
Expand All @@ -231,7 +228,7 @@ func (jenny *GoBuilder) typeHasBuilder(builders ast.Builders, builder ast.Builde
return referredTypePkg, builderFound
}

func (jenny *GoBuilder) generateArgument(builders ast.Builders, builder ast.Builder, arg ast.Argument) string {
func (jenny *Builder) generateArgument(builders ast.Builders, builder ast.Builder, arg ast.Argument) string {
typeName := formatType(arg.Type, true, "types")

if builderPkg, found := jenny.typeHasBuilder(builders, builder, arg.Type); found {
Expand All @@ -243,7 +240,7 @@ func (jenny *GoBuilder) generateArgument(builders ast.Builders, builder ast.Buil
return fmt.Sprintf("%s %s", name, typeName)
}

func (jenny *GoBuilder) generateAssignment(builders ast.Builders, builder ast.Builder, assignment ast.Assignment) string {
func (jenny *Builder) generateAssignment(builders ast.Builders, builder ast.Builder, assignment ast.Assignment) string {
fieldPath := jenny.formatFieldPath(assignment.Path)
valueType := assignment.ValueType

Expand Down Expand Up @@ -276,21 +273,21 @@ func (jenny *GoBuilder) generateAssignment(builders ast.Builders, builder ast.Bu

generatedConstraints := strings.Join(jenny.constraints(argName, assignment.Constraints), "\n")
if generatedConstraints != "" {
generatedConstraints = generatedConstraints + "\n\n"
generatedConstraints += "\n\n"
}

return generatedConstraints + fmt.Sprintf("builder.internal.%[1]s = %[3]s%[2]s", fieldPath, argName, asPointer)
}

func (jenny *GoBuilder) escapeVarName(varName string) string {
func (jenny *Builder) escapeVarName(varName string) string {
if isReservedGoKeyword(varName) {
return varName + "Arg"
}

return varName
}

func (jenny *GoBuilder) generateDefaultCall(option ast.Option) string {
func (jenny *Builder) generateDefaultCall(option ast.Option) string {
args := make([]string, 0, len(option.Default.ArgsValues))
for _, arg := range option.Default.ArgsValues {
args = append(args, formatScalar(arg))
Expand All @@ -299,7 +296,7 @@ func (jenny *GoBuilder) generateDefaultCall(option ast.Option) string {
return fmt.Sprintf("%s(%s)", tools.UpperCamelCase(option.Name), strings.Join(args, ", "))
}

func (jenny *GoBuilder) constraints(argumentName string, constraints []ast.TypeConstraint) []string {
func (jenny *Builder) constraints(argumentName string, constraints []ast.TypeConstraint) []string {
output := make([]string, 0, len(constraints))

for _, constraint := range constraints {
Expand All @@ -309,7 +306,7 @@ func (jenny *GoBuilder) constraints(argumentName string, constraints []ast.TypeC
return output
}

func (jenny *GoBuilder) constraint(argumentName string, constraint ast.TypeConstraint) string {
func (jenny *Builder) constraint(argumentName string, constraint ast.TypeConstraint) string {
var buffer strings.Builder

buffer.WriteString(fmt.Sprintf("if !(%s) {\n", jenny.constraintComparison(argumentName, constraint)))
Expand All @@ -319,7 +316,7 @@ func (jenny *GoBuilder) constraint(argumentName string, constraint ast.TypeConst
return buffer.String()
}

func (jenny *GoBuilder) constraintComparison(argumentName string, constraint ast.TypeConstraint) string {
func (jenny *Builder) constraintComparison(argumentName string, constraint ast.TypeConstraint) string {
if constraint.Op == "minLength" {
return fmt.Sprintf("len([]rune(%[1]s)) >= %[2]v", argumentName, constraint.Args[0])
}
Expand All @@ -331,10 +328,6 @@ func (jenny *GoBuilder) constraintComparison(argumentName string, constraint ast
}

func isReservedGoKeyword(input string) bool {
// TODO
if input == "type" {
return true
}

return false
// TODO: there's more than that
return input == "type"
}
4 changes: 2 additions & 2 deletions internal/jennies/golang/jennies.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ func Jennies() *codejen.JennyList[[]*ast.File] {
return "golang"
})
targets.AppendManyToMany(
tools.Foreach[*ast.File](GoRawTypes{}),
tools.Foreach[*ast.File](RawTypes{}),
)
targets.AppendOneToMany(
codejen.AdaptOneToMany[[]ast.Builder, []*ast.File](
&GoBuilder{},
&Builder{},
func(files []*ast.File) []ast.Builder {
generator := &ast.BuilderGenerator{}
builders := generator.FromAST(files)
Expand Down
Loading

0 comments on commit 4ce7ccf

Please sign in to comment.