Skip to content

Commit

Permalink
feature: add subscription type, changelog, and linter (#1)
Browse files Browse the repository at this point in the history
* feature: add subscription type, changelog, and linter

* bump: changelog

* feature: add new function to only get the operation name

* refactor: use strconv to parse int to string
  • Loading branch information
zhenqianz authored May 29, 2020
1 parent 416f3e3 commit 5453609
Show file tree
Hide file tree
Showing 18 changed files with 286 additions and 79 deletions.
41 changes: 41 additions & 0 deletions .chglog/CHANGELOG.tpl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# go-connect
{{ if .Versions -}}

{{ if .Unreleased.CommitGroups -}}
{{ range .Unreleased.CommitGroups -}}
### {{ .Title }}
{{ range .Commits -}}
- {{ if .Scope }}**{{ .Scope }}:** {{ end }}{{ .Subject }}
{{ end }}
{{ end -}}
{{ end -}}
{{ end -}}

{{ range .Versions }}
<a name="{{ .Tag.Name }}"></a>
## {{ if .Tag.Previous }}[{{ .Tag.Name }}]{{ else }}{{ .Tag.Name }}{{ end }} - {{ datetime "2006-01-02" .Tag.Date }}
{{ range .CommitGroups -}}
### {{ .Title }}
{{ range .Commits -}}
- {{ if .Scope }}**{{ .Scope }}:** {{ end }}{{ .Subject }}
{{ end }}
{{ end -}}

{{- if .NoteGroups -}}
{{ range .NoteGroups -}}
### {{ .Title }}
{{ range .Notes }}
{{ .Body }}
{{ end }}
{{ end -}}
{{ end -}}
{{ end -}}

{{- if .Versions }}
[Unreleased]: {{ .Info.RepositoryURL }}/compare/{{ $latest := index .Versions 0 }}{{ $latest.Tag.Name }}...HEAD
{{ range .Versions -}}
{{ if .Tag.Previous -}}
[{{ .Tag.Name }}]: {{ $.Info.RepositoryURL }}/compare/{{ .Tag.Previous.Name }}...{{ .Tag.Name }}
{{ end -}}
{{ end -}}
{{ end -}}
32 changes: 32 additions & 0 deletions .chglog/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
style: github
template: CHANGELOG.tpl.md
info:
title: CHANGELOG
repository_url: https://github.com/kumparan/gqlyzer
options:
commits:
filters:
Type:
- feature
- bugfix
- hotfix
- refactor
- test
- misc
commit_groups:
title_maps:
feature: New Features
bugfix: Fixes
hotfix: Fixes
refactor: Code Improvements
test: Test Improvements
misc: Other Improvements
header:
pattern: "^(\\w*)\\:\\s(.*)$"
pattern_maps:
- Type
- Subject
notes:
keywords:
- BREAKING CHANGE

16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
.idea
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# go-connect

<a name="v1.2.0"></a>
## [v1.2.0] - 2020-05-28
### New Features
- add subscription type, changelog, and linter


<a name="v1.1.0"></a>
## [v1.1.0] - 2019-09-18

<a name="v1.0.0"></a>
## v1.0.0 - 2019-09-13

[Unreleased]: https://github.com/kumparan/gqlyzer/compare/v1.2.0...HEAD
[v1.2.0]: https://github.com/kumparan/gqlyzer/compare/v1.1.0...v1.2.0
[v1.1.0]: https://github.com/kumparan/gqlyzer/compare/v1.0.0...v1.1.0
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Kumparan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
SHELL:=/bin/bash

changelog_args=-o CHANGELOG.md -p '^v'

lint:
golangci-lint run --concurrency 4 --print-issued-lines=false --exclude-use-default=false --enable=golint --enable=goimports --enable=unconvert --enable=unparam

changelog:
ifdef version
$(eval changelog_args=--next-tag $(version) $(changelog_args))
endif
git-chglog $(changelog_args)

check-gotest:
ifeq (, $(shell which richgo))
$(warning "richgo is not installed, falling back to plain go test")
$(eval TEST_BIN=go test)
else
$(eval TEST_BIN=richgo test)
endif
$(eval test_command=$(TEST_BIN) ./... -v --cover)

test-only: check-gotest
$(test_command)

test: lint test-only
17 changes: 13 additions & 4 deletions gqlyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package gqlyzer
import (
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/kumparan/gqlyzer/token/operation"

"github.com/kumparan/gqlyzer/token"
)

Expand Down Expand Up @@ -36,8 +39,14 @@ func (l *Lexer) Parse() (token.Operation, error) {
return l.parseOperation()
}

func (l *Lexer) ParseWithVariables(variables string) (token.Operation, error) {
// ParseOperationType parse operation type only
func (l *Lexer) ParseOperationType() (operation.Type, error) {
ot, _, err := l.parseOperationType()
return ot, err
}

// ParseWithVariables parse operation with variable
func (l *Lexer) ParseWithVariables(variables string) (token.Operation, error) {
variableMap := make(map[string]interface{})
err := json.Unmarshal([]byte(variables), &variableMap)
if err != nil {
Expand All @@ -46,11 +55,11 @@ func (l *Lexer) ParseWithVariables(variables string) (token.Operation, error) {

for key, content := range variableMap {
var s string
switch content.(type) {
switch c := content.(type) {
case string:
s = fmt.Sprintf("\"%s\"", content.(string))
s = fmt.Sprintf("\"%s\"", c)
case int:
s = string(content.(int))
s = strconv.Itoa(c)
default:
jsonStr, err := json.Marshal(content)
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions parse_keyword_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package gqlyzer

import "github.com/stretchr/testify/assert"
import "testing"
import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseMutationKeyword(t *testing.T) {
t.Run(`should return no error when given correct keyword`, func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions parse_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
func (l *Lexer) parseName() (string, error) {
var name string
c, err := l.read()
if !isAlphabet(c) {
return "", errors.New("first character of an identifier Name have to be an alphabet: " + string(c))
if !isAlphabet(c) && c != '_' {
return "", errors.New("first character of an identifier Name have to be an alphabet or underscore: " + string(c))
}

for (isAlphabet(c) || c == '_' || isNumber(c)) &&
Expand Down
71 changes: 44 additions & 27 deletions parse_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,48 @@ import (
"github.com/stretchr/testify/assert"
)

func TestParseName_Ok(t *testing.T) {
// Given
l := Lexer{
input: "hello",
}
l.Reset()

// When
output, err := l.parseName()

// Then
assert.NoError(t, err)
assert.Equal(t, "hello", output)
}

func TestParseName_Fail(t *testing.T) {
// Given
l := Lexer{
input: "9hello",
}
l.Reset()

// When
_, err := l.parseName()

// Then
assert.Error(t, err)
func TestParseName(t *testing.T) {
t.Run("ok, alphabet", func(t *testing.T) {
// Given
l := Lexer{
input: "hello",
}
l.Reset()

// When
output, err := l.parseName()

// Then
assert.NoError(t, err)
assert.Equal(t, "hello", output)
})

t.Run("ok, underscore", func(t *testing.T) {
// Given
l := Lexer{
input: "__hello",
}
l.Reset()

// When
output, err := l.parseName()

// Then
assert.NoError(t, err)
assert.Equal(t, "__hello", output)
})

t.Run("fail: not _ or alphabet", func(t *testing.T) {
// Given
l := Lexer{
input: "9hello",
}
l.Reset()

// When
_, err := l.parseName()

// Then
assert.Error(t, err)
})
}
40 changes: 19 additions & 21 deletions parse_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@ import (
"github.com/kumparan/gqlyzer/token/operation"
)

func (l *Lexer) parseOperation() (op token.Operation, err error) {
func (l *Lexer) parseOperationType() (op operation.Type, isAnonymous bool, err error) {
l.consumeWhitespace()
l.pushFlush()
// parse "query" keyword
var (
isQuery bool
isMutation bool
)

c, err := l.read()
if err != nil {
Expand All @@ -25,40 +20,43 @@ func (l *Lexer) parseOperation() (op token.Operation, err error) {
if err = l.parseKeyword("query"); err != nil {
return
}
isQuery = true
l.cursor++
return operation.Query, false, nil
case 'm':
if err = l.parseKeyword("mutation"); err != nil {
return
}
isMutation = true
l.cursor++
case '{':
break
return operation.Mutation, false, nil
case 's':
if err = l.parseKeyword("subscription"); err != nil {
return
}
return operation.Subscription, false, nil
case '{': // anonymous operation returns query type
return operation.Query, true, nil
default:
err = errors.New("unknown definition")
return
}
}

if isQuery {
op.Type = operation.Query
} else if isMutation {
op.Type = operation.Mutation
func (l *Lexer) parseOperation() (op token.Operation, err error) {
opType, isAnonymous, err := l.parseOperationType()
if err != nil {
return
}

// get name of named operation
if isQuery || isMutation {
op.Type = opType
if !isAnonymous {
l.cursor++
name, err := l.parseName()
if err != nil {
return token.Operation{}, err
}
op.Name = name
} else {
op.Type = operation.Query
}

l.consumeWhitespace()
c, err = l.read()
c, err := l.read()
if err != nil {
return
}
Expand Down
Loading

0 comments on commit 5453609

Please sign in to comment.