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

initial config #1

Draft
wants to merge 24 commits into
base: main
Choose a base branch
from
Draft
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
14 changes: 14 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: 2
updates:
- package-ecosystem: 'github-actions'
directory: '/'
schedule:
interval: 'daily'
- package-ecosystem: 'gomod'
directory: '/'
schedule:
interval: 'daily'
- package-ecosystem: 'docker'
directory: '/'
schedule:
interval: 'daily'
38 changes: 38 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
on:
push:
branches:
- main
pull_request:

name: Test
jobs:
lint:
name: runner / golangci-lint
runs-on: ubuntu-latest
steps:
- name: Check out
uses: actions/[email protected]
- name: golangci-lint
uses: reviewdog/[email protected]
test:
strategy:
matrix:
go-version:
- stable
- oldstable
platform:
- ubuntu-latest
runs-on: ${{ matrix.platform }}
steps:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
- name: Check out
uses: actions/[email protected]
- name: go test
run: go test -v -race -coverprofile=profile.cov ./...
- name: Send coverage
uses: shogo82148/[email protected]
with:
path-to-profile: profile.cov
72 changes: 72 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Options for analysis running.
run:
# Timeout for analysis, e.g. 30s, 5m.
# Default: 1m
timeout: 10m
# Which dirs to skip: issues from them won't be reported.
# Can use regexp here: `generated.*`, regexp is applied on full path.
# Default value is empty list,
# but default dirs are skipped independently of this option's value (see skip-dirs-use-default).
# "/" will be replaced by current OS file path separator to properly work on Windows.
skip-dirs:
- contracts/microavia
output:
format: tab

# output configuration options
output:

# All available settings of specific linters.
linters-settings:
# See the dedicated "linters-settings" documentation section.
govet:
check-shadowing: true
golint:
min-confidence: 0.1
maligned:
suggest-new: true
goconst:
min-len: 2
min-occurrences: 2
misspell:
locale: US
lll:
line-length: 140

linters:
enable-all: true
disable:
- scopelint # deprecated: The repository of the linter has been deprecated by the owner. Replaced by exportloopref.
- golint # deprecated: The repository of the linter has been archived by the owner. Replaced by revive.
- deadcode # deprecated: The owner seems to have abandoned the linter. Replaced by unused.
- ifshort # deprecated: The repository of the linter has been deprecated by the owner.
- varcheck # deprecated: The owner seems to have abandoned the linter. Replaced by unused.
- interfacer # deprecated: The repository of the linter has been archived by the owner.
- exhaustivestruct # deprecated: The owner seems to have abandoned the linter. Replaced by exhaustruct.
- maligned # deprecated: The repository of the linter has been archived by the owner. Replaced by govet 'fieldalignment'.
- structcheck # deprecated: The owner seems to have abandoned the linter. Replaced by unused.
- nosnakecase # deprecated: The repository of the linter has been deprecated by the owner. Replaced by revive(var-naming).
- rowserrcheck # disabled because of generics.
- sqlclosecheck # disabled because of generics.
- structcheck # disabled because of generics.
- wastedassign # disabled because of generics.
- dupl # too many false positives
- tagliatelle # too many false positives
- gci # conflicting with goimports order
- godot # useless
- exhaustruct # almost useless
- varnamelen # too many false positives
issues:
# Show only new issues: if there are unstaged changes or untracked files,
# only those changes are analyzed, else only changes in HEAD~ are analyzed.
# It's a super-useful option for integration of golangci-lint into existing large codebase.
# It's not practical to fix all existing issues at the moment of integration:
# much better don't allow issues in new code.
#
# Default: false.
# new: true
# Show only new issues created after git revision `REV`.
# new-from-rev: remotes/origin/main
severity:
# See the dedicated "severity" documentation section.
option: value
99 changes: 99 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
= go-messgen image:https://github.com/microavia/go-messgen/actions/workflows/test.yml/badge.svg?branch=main["Build Status"] image:https://coveralls.io/repos/github/microavia/go-messgen/badge.svg["Coverage Status",link="https://coveralls.io/github/microavia/go-messgen"]
Daniel Podolsky <[email protected]>
:toc:

[![Coverage Status](https://coveralls.io/repos/github/microavia/go-messgen/badge.svg)](https://coveralls.io/github/microavia/go-messgen)

Lightweight and fast message serialization library.
Generates message classes/structs from yml scheme.

Features:

- Embedded-friendly
- Fixed size arrays
- Dynamic size arrays
- Nested messages
- Messages metadata
- Supported languages: C++, Go, JavaScript

Based on the https://github.com/microavia/messgen[original messgen by Microavia].

== Dependencies

- Golang to build the generator

== Installation

[source]
----
go install github.com/microavia/go-messgen/cmd/messgen@latest
----

or get the precompiled binary from the https://github.com/microavia/go-messgen/releases[releases page].

== Generate messages

Each protocol should be placed in directory `base_dir/vendor/protocol`.
`base_dir` is base directory for message definitions (is allowed to specify multiple base directories).
`vendor` is protocol vendor, it is used as namespace in generated messages allowing to avoid conflict between protocols from different vendors if used in one application.
`protocol` is protocol name, each protocol has protocol ID, that allows to use multiple protocols on single connection, e.g. bootloader and application protocols.

Message generator usage:

[source]
----
messgen -b <base_dir> -m <vendor>/<protocol> -l <lang> -o <out_dir> [-D variable=value]
----

For some languages it's necessary to specify some variables using `-D` option.

Generated messages placed in `out_dir` directory.

=== Go

Example for Go messages generation:

[source]
----
messgen -b ./base_dir -m my_vendor/my_protocol -l go -o out/go -D messgen_go_module=example.com/path/to/messgen
----

Variable `messgen_go_module` must point to messgen Go module (`port/go/messgen`), to add necessary imports in generated messages.

=== C++

Example for C++ messages generation:

[source]
----
messgen -b ./base_dir -m my_vendor/my_protocol -l cpp -o out/cpp
----

Variable `metadata_json=true` can be passed to generate metadata in JSON format, rather than legacy.

=== JS/TS

Example for JS messages generation:

[source]
----
messgen -b ./base_dir -m my_vendor/my_protocol -l json -o out/json
----
This command will generate json messages.

The types of these messages for TS can be generated as follows:

[source]
----
messgen -b ./base_dir -m my_vendor/my_protocol -l ts -o out/ts
----

=== MD

Example for protocol documentation generation:

[source]
----
messgen -b ./base_dir -m my_vendor/my_protocol -l md -o out/md
----

2 changes: 0 additions & 2 deletions README.md

This file was deleted.

77 changes: 77 additions & 0 deletions cmd/messgen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

import (
"errors"
"fmt"
"os"
"path/filepath"

"github.com/powerman/structlog"

"github.com/microavia/go-messgen/internal/config"
"github.com/microavia/go-messgen/internal/definition"
gogen "github.com/microavia/go-messgen/internal/generator/golang"
"github.com/microavia/go-messgen/internal/validator"
)

func main() {
structlog.DefaultLogger.SetLogLevel(structlog.INF)

cfg, err := config.Parse(os.Args[1:])
if err != nil {
structlog.DefaultLogger.Fatal(err)
}

if *cfg.Verbose {
structlog.DefaultLogger.SetLogLevel(structlog.DBG)
}

structlog.DefaultLogger.Debug("started", "config", cfg)

baseDirs, err := absDirs(*cfg.BaseDirs)
if err != nil {
structlog.DefaultLogger.Fatal("enumerating base dirs: ", err)
}

def, err := definition.LoadModules(os.DirFS("/"), baseDirs, *cfg.Modules)
if err != nil {
structlog.DefaultLogger.Fatal("loading definitions: ", err)
}

err = validator.Validate(def)
if err != nil {
structlog.DefaultLogger.Fatal("validating definitions: ", err)
}

switch *cfg.Lang {
case "go":
err = gogen.GenerateModules(*cfg.OutDir, def)
default:
structlog.DefaultLogger.Fatal("unreachable reached: ", *cfg.Lang)
}

if err != nil {
structlog.DefaultLogger.Fatal("generating code: ", err)
}
}

var errNotFound = errors.New("not found")

func absDirs(in []string) ([]string, error) {
out := make([]string, 0, len(in))

for _, dir := range in {
absDir, err := filepath.Abs(dir)
if err != nil {
continue
}

out = append(out, absDir)
}

if len(out) == 0 {
return nil, fmt.Errorf("no one of %+v: %w", in, errNotFound)
}

return out, nil
}
20 changes: 20 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module github.com/microavia/go-messgen

go 1.19

require (
github.com/ghodss/yaml v1.0.0
github.com/powerman/structlog v0.7.3
github.com/stoewer/go-strcase v1.2.1
github.com/stretchr/testify v1.8.1
gopkg.in/alecthomas/kingpin.v2 v2.2.6
)

require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading