Skip to content

Commit

Permalink
Port from gitlab (#1)
Browse files Browse the repository at this point in the history
# Port from gitlab

1. Copy from gitlab
2. Port gitlab-ci -> github workflows
3. Added test coverage and linting
  • Loading branch information
stewartboyd119 authored Jul 22, 2024
1 parent dbfd0bf commit 04865f9
Show file tree
Hide file tree
Showing 35 changed files with 2,456 additions and 0 deletions.
60 changes: 60 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Go

on:
push:
branches: [main]
tags: ['v*']
pull_request:
branches: ['*']

permissions:
contents: read

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
go: ["1.21.x", "1.22.x"]
include:
- go: 1.22.x
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}
cache-dependency-path: '**/go.sum'

- name: Download Dependencies
run: |
go mod download
- name: Test
run: make cover

lint:
name: Lint
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
name: Check out repository
- uses: actions/setup-go@v5
name: Set up Go
with:
go-version: 1.22.x
cache: false # managed by golangci-lint

- uses: golangci/golangci-lint-action@v6
name: Install golangci-lint
with:
version: latest
# Hack: Use the official action to download, but not run.
# make lint below will handle actually running the linter.
args: --help

- run: make lint
name: Lint
48 changes: 48 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test
vendor

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof
*.out
#*.jar

config/local/config.json

sms
main
.DS_Store
debug
appConfig.local.json
settings.json
liquibase.properties
*.sample.sql

**/obj
**/bin

# JetBrains
.idea/
/.run/


coverage.txt
69 changes: 69 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
run:
skip-dirs:
- docs
- datadog
- kustomize
skip-files:
- 'wire_gen.go'
tests: false
linters-settings:
errcheck:
check-type-assertions: true
check-blank: true
gci:
sections:
- standard
- default
gosimple:
go: '1.17'
govet:
check-shadowing: true
settings:
printf:
funcs:
- (gitlab.zgtools.net/devex/archetypes/gomods/zlog.Logger).Debug
- (gitlab.zgtools.net/devex/archetypes/gomods/zlog.Logger).Info
- (gitlab.zgtools.net/devex/archetypes/gomods/zlog.Logger).Warn
- (gitlab.zgtools.net/devex/archetypes/gomods/zlog.Logger).Error
depguard:
rules:
Main:
files:
- $all
- "!$test"
deny:
- github.com/satori/go.uuid: Prefer "github.com/google/uuid"
disable-all: true
enable:
- asciicheck
- bidichk
- bodyclose
- cyclop
- decorder
- depguard
- deadcode
- dupl
- errcheck
- errchkjson
- errname
- errorlint
- exportloopref
- gci
- gocognit
- goconst
- gocritic
- gocyclo
- gofmt
- gosimple
- govet
- ineffassign
- nolintlint
- prealloc
- staticcheck
- structcheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM golang:1.22 AS build

ENV CGO_ENABLED=0

WORKDIR /go/src/zfmt
COPY . .

RUN go install -v ./...
RUN go build -o zfmt
FROM debian

COPY --from=build /go/src/zfmt /


ENTRYPOINT ["/zfmt"]
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Directories containing independent Go modules.
MODULE_DIRS = .
LOCAL_GOLANGCI_VERSION=$(shell golangci-lint --version)
REMOTE_GOLANGCI_VERSION=1.56.2

.PHONY: cover
cover:
go test -v ./... -count=1 -coverprofile=coverage.txt -covermode atomic

.PHONY: lint
lint: golangci-lint

.PHONY: golangci-lint
golangci-lint:
@$(foreach mod,$(MODULE_DIRS), \
(cd $(mod) && \
echo "[lint] golangci-lint: $(mod)" && \
golangci-lint run --path-prefix $(mod) ./...) &&) true
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# zfmt

[![coverage report](https://gitlab.zgtools.net/devex/archetypes/gomods/zfmt/badges/main/coverage.svg)](https://gitlab.zgtools.net/devex/archetypes/gomods/zfmt/-/commits/main)

[![pipeline status](https://gitlab.zgtools.net/devex/archetypes/gomods/zfmt/badges/main/pipeline.svg)](https://gitlab.zgtools.net/devex/archetypes/gomods/zfmt/-/commits/main)


A go library which contains a number of useful implementations of the Formatter interface,
an interface which contains Marshall and Unmarshall methods.

## Dependencies

### Gogen Avro

Install [Gogen Avro](https://github.com/actgardner/gogen-avro) a utility for generating go code from avro schemas (used for testdata)

```
go get github.com/actgardner/gogen-avro/v7/cmd/...
```


### Migration Guide

1. #### V0 to V1 Migration

* Enum `ProtoFmt`(`proto`) renamed to `ProtoBase64Fmt`(`proto_base64`). This is intended for SQS use only while the newly introduced `ProtoRawFmt`(`proto_raw`) is intended for most other use cases.
* Enum `ProtoSchemaFmt`(`proto_schema`) renamed to `ProtoSchemaDeprecatedFmt`(`proto_schema_deprecated`). The proto schema is deprecated because it doesn't work properly with the confluent schema registry. Use the `ProtoRawFmt` or `ProtoBase64Fmt` instead.
58 changes: 58 additions & 0 deletions avro.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package zfmt

import (
"bytes"
"fmt"

"github.com/actgardner/gogen-avro/v10/compiler"
"github.com/actgardner/gogen-avro/v10/soe"
"github.com/actgardner/gogen-avro/v10/vm"
"github.com/actgardner/gogen-avro/v10/vm/types"
heetch "github.com/heetch/avro"
heetchtypegen "github.com/heetch/avro/avrotypegen"
)

// GeneratedAvroRecord combines interfaces that make Encoding/Decoding possible
// for gogen-avro struct
type GeneratedAvroRecord interface {
soe.AvroRecord
types.Field
Schema() string
}

type AvroFormatter struct{}

func (p *AvroFormatter) Marshall(v any) ([]byte, error) {
switch m := v.(type) {
case soe.AvroRecord:
buf := &bytes.Buffer{}
err := m.Serialize(buf)
return buf.Bytes(), err
case heetchtypegen.AvroRecord:
b, _, err := heetch.Marshal(v)
return b, err
default:
return nil, fmt.Errorf("%T, avro formatter supports only gogen-avro or heetch avro messages", v)
}
}

func (p *AvroFormatter) Unmarshal(b []byte, v any) error {
switch m := v.(type) {
case GeneratedAvroRecord:
r := bytes.NewReader(b)
deser, err := compiler.CompileSchemaBytes([]byte(m.Schema()), []byte(m.Schema()))
if err != nil {
return err
}
return vm.Eval(r, deser, m)
case heetchtypegen.AvroRecord:
t, err := heetch.ParseType(m.AvroRecord().Schema)
if err != nil {
return err
}
_, err = heetch.Unmarshal(b, v, t)
return err
default:
return fmt.Errorf("%T, avro formatter supports only gogen-avro or heetch avro messages", v)
}
}
61 changes: 61 additions & 0 deletions formatter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package zfmt

import (
err "fmt"
)

// FormatterType defines a formatter
type FormatterType string

const (
// JSONFmt indicates json formatter
JSONFmt FormatterType = "json"
// ProtoRawFmt indicates protocol buffer formatter. Does not Base64 encode/decode.
ProtoRawFmt FormatterType = "proto_raw"
// ProtoBase64Fmt indicates protocol buffer formatter. Base64 encodes/decodes the message. Only intended for use with SQS.
ProtoBase64Fmt FormatterType = "proto_base64"
// ProtoJSONFmt indicates usage protojson formatter (which should be used for json formatted proto messages).
ProtoJSONFmt FormatterType = "proto_json"
// StringFmt indicates string formatter
StringFmt FormatterType = "string"
// AvroFmt indicates apache avro formatter
AvroFmt FormatterType = "avro"
// AvroSchemaFmt indicates apache avro formatter with schemaID encoded
AvroSchemaFmt FormatterType = "avro_schema"
// JSONSchemaFmt indicates json formatter with schemaID encoded
JSONSchemaFmt FormatterType = "json_schema"
// ProtoSchemaDeprecatedFmt indicates proto formatter with schemaID encoded - deprecated because it doesn't work properly.
ProtoSchemaDeprecatedFmt FormatterType = "proto_schema_deprecated"
)

// Formatter allows the user to extend formatting capability to unsupported data types
type Formatter interface {
Marshall(v any) ([]byte, error)
Unmarshal(data []byte, v any) error
}

// GetFormatter returns supported formatter from its name
func GetFormatter(fmt FormatterType, schemaID int) (Formatter, error) {
switch fmt {
case StringFmt:
return &StringFormatter{}, nil
case JSONFmt:
return &JSONFormatter{}, nil
case ProtoJSONFmt:
return &ProtoJSONFormatter{}, nil
case ProtoBase64Fmt:
return &ProtobufBase64Formatter{}, nil
case ProtoRawFmt:
return &ProtobufRawFormatter{}, nil
case AvroFmt:
return &AvroFormatter{}, nil
case AvroSchemaFmt:
return &SchematizedAvroFormatter{SchemaID: schemaID}, nil
case JSONSchemaFmt:
return &SchematizedJSONFormatter{SchemaID: schemaID}, nil
case ProtoSchemaDeprecatedFmt:
return &SchematizedProtoFormatterDeprecated{SchemaID: schemaID}, nil
default:
return nil, err.Errorf("unsupported formatter %s", fmt)
}
}
Loading

0 comments on commit 04865f9

Please sign in to comment.