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

Annotations, tests #9

Merged
merged 2 commits into from
Oct 10, 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
25 changes: 25 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Tests

on:
push:
branches:
- master
pull_request:

jobs:
test:
name: Test check
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.21.0

- name: Run tests and coverage
run: |
go test -v -coverprofile=coverage.txt -covermode=atomic ./...
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
.idea/*

bin/*
_examples/e2.ridl
coverage.txt
16 changes: 9 additions & 7 deletions _examples/e1.ridl
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
webrpc = v1 # version of webrpc schema format (ridl or json)
name = example # name of your backend app
version = v0.0.1 # version of your schema
webrpc = v1 # version of webrpc schema format (ridl or json)
name = example # name of your backend app
version=v0.0.1#version of your schema

# bar
enum Intent: string
#! foo
- openSession
- closeSession
- validateSession
- openSession
- closeSession

enum Kind: uint32
- USER
Expand Down Expand Up @@ -68,9 +67,12 @@ error 20 UserNotFound "User not found" HTTP 404
error 4 UserTooYoung "" HTTP 404

service ExampleService # oof
@ deprecated : Pong
@ auth : ApiKeyAuth @ who dsa : J W T ## dadsadadsa
- Ping()
- Status() => (status: bool)
@ internal @ public ## dsada s dsa
- Version() => (version: Version)
@public
- GetUser ( header : map < string , string > , userID : uint64 ) => ( code : uint32 , user : User )
- FindUser(s :SearchFilter) => (name: string, user: User) ###! last

55 changes: 53 additions & 2 deletions formatter/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@ func (f *form) formatLine(line string) (string, error) {
f.parseSection(line)

switch f.section {
case sectionWebRPC:
fallthrough
case sectionName:
fallthrough
case sectionVersion:
f.padding = 0
line = reduceSpaces(line)
s, c := parseAndDivideInlineComment(line)
parts := strings.Split(s, "=")
if len(parts) != 2 {
return "", fmt.Errorf("unexpected amount of parts=(%d) %s", len(parts), line)
}

line = fmt.Sprintf("%s = %s", removeSpaces(parts[0]), removeSpaces(parts[1]))

line = c.appendInlineComment(line)

case sectionComment:
f.comments = append(f.comments, parseComment(line))
case sectionEnum:
Expand Down Expand Up @@ -218,6 +235,34 @@ func (f *form) formatLine(line string) (string, error) {

line = fmt.Sprintf("%s%s", strings.Repeat(" ", f.padding), line)
line = c.appendInlineComment(line)
case sectionAnnotation:
f.padding = 4
s, c := parseAndDivideInlineComment(line)

parts := strings.Split(s, "@")
if len(parts) < 2 {
return "", fmt.Errorf("unexpected amount of parts=(%d)", len(parts))
}

var as string
for i := 1; i < len(parts); i++ {
ap := strings.Split(parts[i], ":")
if i > 1 {
as += " "
}

switch len(ap) {
case 1:
as = fmt.Sprintf("%s@%s", as, removeSpaces(ap[0]))
case 2:
as = fmt.Sprintf("%s@%s:%s", as, removeSpaces(ap[0]), removeSpaces(ap[1]))
default:
return "", fmt.Errorf("unexpected amount of parts for one anotation parts=(%d) %s", len(ap), line)
}
}

line = fmt.Sprintf("%s%s", strings.Repeat(" ", f.padding), as)
line = c.appendInlineComment(line)
default:
}

Expand Down Expand Up @@ -261,6 +306,8 @@ func (f *form) parseSection(line string) {
f.section = sectionField
case strings.HasPrefix(line, "+"):
f.section = sectionTag
case strings.HasPrefix(line, "@"):
f.section = sectionAnnotation
default:
f.section = sectionUnknown
}
Expand Down Expand Up @@ -330,6 +377,10 @@ func reduceSpaces(input string) string {
return pattern.ReplaceAllString(input, " ")
}

func removeSpaces(input string) string {
return strings.ReplaceAll(input, " ", "")
}

func formatMethodArguments(s string) (string, error) {
content, err := extractFromParenthesis(s)
if err != nil {
Expand Down Expand Up @@ -360,7 +411,7 @@ func extractFromParenthesis(s string) (string, error) {
}

func splitArguments(s string) []string {
s = strings.ReplaceAll(strings.TrimSpace(s), " ", "")
s = removeSpaces(strings.TrimSpace(s))
var parts []string
var ic, im int

Expand All @@ -378,7 +429,7 @@ func splitArguments(s string) []string {
break
}
} else {
s = strings.ReplaceAll(s, " ", "")
s = removeSpaces(s)
c, more := findComma(ic, s)
if more {
parts = append(parts, s[:c])
Expand Down
1 change: 1 addition & 0 deletions formatter/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ const (
sectionTag
sectionError
sectionImport
sectionAnnotation
)
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module github.com/webrpc/ridlfmt

go 1.20

require github.com/stretchr/testify v1.9.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
29 changes: 20 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,38 @@ import (
)

func main() {
flag.Usage = usage
flagSet := flag.NewFlagSet("ridlfmt", flag.ExitOnError)
if err := runRidlfmt(flagSet, os.Args[1:]); err != nil {
log.Fatalf("Error: %v", err)
}
}

sortErrorsFlag := flag.Bool("s", false, "sort errors by code")
writeFlag := flag.Bool("w", false, "write output to input file (overwrites the file)")
helpFlag := flag.Bool("h", false, "show help")
func runRidlfmt(flagSet *flag.FlagSet, args []string) error {
flag.Usage = usage

flag.Parse()
sortErrorsFlag := flagSet.Bool("s", false, "sort errors by code")
writeFlag := flagSet.Bool("w", false, "write output to input file (overwrites the file)")
helpFlag := flagSet.Bool("h", false, "show help")

args := flag.Args()
if err := flagSet.Parse(args); err != nil {
return fmt.Errorf("parse args: %w", err)
}

if *helpFlag {
flag.Usage()
os.Exit(0)
}

if len(args) == 0 && !isInputFromPipe() {
fileArgs := flagSet.Args()

if len(fileArgs) == 0 && !isInputFromPipe() {
fmt.Fprintln(os.Stderr, "error: no input files specified")
flag.Usage()
os.Exit(1)
}

if *writeFlag {
for _, fileName := range args {
for _, fileName := range fileArgs {
err := formatAndWriteToFile(fileName, *sortErrorsFlag)
if err != nil {
log.Fatalf("Error processing file %s: %v", fileName, err)
Expand All @@ -47,14 +56,16 @@ func main() {
log.Fatalf("Error processing input from pipe: %v", err)
}
} else {
for _, fileName := range args {
for _, fileName := range fileArgs {
err := formatAndPrintToStdout(fileName, *sortErrorsFlag)
if err != nil {
log.Fatalf("Error processing file %s: %v", fileName, err)
}
}
}
}

return nil
}

func isInputFromPipe() bool {
Expand Down
Loading
Loading