Skip to content

Commit

Permalink
Add cybozu.validate package
Browse files Browse the repository at this point in the history
cybozu.validate is a normalization and validation system for
Protocol Buffers.

The repository is set up for a Buf module in order to prepare
publishing protobuf files on BSR.

This commit does not include any code generator but includes
a hand-written Go code `examples/validation_cybozu_validate2.pb.go`
to demonstrate what to be generated.
  • Loading branch information
ymmt2005 committed Mar 10, 2023
1 parent d6156dd commit 95e7cf7
Show file tree
Hide file tree
Showing 25 changed files with 4,718 additions and 4 deletions.
28 changes: 28 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: bug
assignees: ''

---

**Describe the bug**
A clear and concise description of what the bug is.

**Environments**
- Version:
- OS:

**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

**Expected behavior**
A clear and concise description of what you expected to happen.

**Additional context**
Add any other context about the problem here.
23 changes: 23 additions & 0 deletions .github/ISSUE_TEMPLATE/issue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
name: Task
about: Describe this issue
title: ''
labels: ''
assignees: ''

---

## What

Describe what this issue should address.

## How

Describe how to address the issue.

## Checklist

- [ ] Finish implementation of the issue
- [ ] Test all functions
- [ ] Have enough logs to trace activities
- [ ] Notify developers of necessary actions
15 changes: 15 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: CI
on: [push]
jobs:
test:
name: Run tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version-file: go.mod
cache: true
- run: make lint
- run: make check-generate
- run: make test
36 changes: 36 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Release
on:
push:
tags:
- 'v*'

permissions:
contents: write

jobs:
release:
name: Release on GitHub
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- run: git fetch --force --tags
- uses: actions/setup-go@v3
with:
go-version-file: go.mod
cache: true
- uses: bufbuild/buf-setup-action@b5ee8ca8d082edeeb8b0fc7fe1178ae0d671816a # v1.15.0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
- uses: bufbuild/buf-push-action@1c45f6a21ec277ee4c1fa2772e49b9541ea17f38 # v1.1.1
with:
buf_token: ${{ secrets.BSR_TOKEN }}
- name: GoReleaser
uses: goreleaser/goreleaser-action@f82d6c1c344bcacabba2c841718984797f664a6b # v4.2.0
with:
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
# Downloaded files
/bin

# For GoReleaser
/dist
44 changes: 44 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com
builds:
- env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin
main: ./cmd/protoc-gen-go-cybozu-validate
binary: protoc-gen-go-cybozu-validate

archives:
- format: tar.gz
# this name template makes the OS and Arch compatible with the results of uname.
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
# use zip for windows archives
format_overrides:
- goos: windows
format: zip
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ incpatch .Version }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'

release:
prerelease: auto

# The lines beneath this are called `modelines`. See `:help modeline`
# Feel free to remove those if you don't want/use them.
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
85 changes: 85 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Makefile for compiling protobuf files

# Tool versions
BUF_VERSION = 1.15.0
GO_VERSION := $(shell awk '/^go / {print $$2}' go.mod)
PROTOC_GEN_GO_VERSION := $(shell awk '/google.golang.org\/protobuf/ {print substr($$NF, 2)}' go.mod)


## sanity checks
ifeq ($(shell go version | grep -F $(GO_VERSION)),)
$(error Go must be version $(GO_VERSION))
endif

# Variables
PWD := $(shell pwd)
BUF = $(PWD)/bin/buf
RUN_BUF = PATH=$(PWD)/bin:$$PATH ./bin/buf
PROTOC_GEN_GO = $(PWD)/bin/protoc-gen-go
PROTOC_GEN_GO_CYBOZU_VALIDATE = $(PWD)/bin/protoc-gen-go-validate

help:
@echo 'Available targets:'
@echo ' all: build everything.'
@echo ' test: run tests.'
@echo ' clean: remove downloaded files.'
@echo ' check-generate: test if the generated files are up-to-date or not.'
@echo ' lint: lint protobuf files.'
@echo ' format: format protobuf files.'
@echo ' go: generate Go code with protoc-gen-go.'
@echo ' validate: generate code for normalization/validation.'
@echo ' create-tag: creates a new Git tag.'

$(BUF):
mkdir -p bin
GOBIN=$(PWD)/bin go install github.com/bufbuild/buf/cmd/buf@v$(BUF_VERSION)

$(PROTOC_GEN_GO):
mkdir -p bin
GOBIN=$(PWD)/bin go install google.golang.org/protobuf/cmd/protoc-gen-go@v$(PROTOC_GEN_GO_VERSION)

$(PROTOC_GEN_GO_CYBOZU_VALIDATE):
mkdir -p bin
go build -o $@ ./cmd/protoc-gen-go-cybozu-validate

.PHONY: all
all:
$(MAKE) format
$(MAKE) go
echo skip this for now $(MAKE) validate

.PHONY: test
test:
go test -count 1 -v ./...
go vet ./...
test -z $$(gofmt -s -l . | tee /dev/stderr)

.PHONY: clean
clean:
rm -rf bin dist

.PHONY: check-generate
check-generate:
$(MAKE) all
go mod tidy
git diff --exit-code --name-only

.PHONY: lint
lint: $(BUF)
$(RUN_BUF) lint

.PHONY: format
format: $(BUF)
$(RUN_BUF) format -w

.PHONY: go
go: $(BUF) $(PROTOC_GEN_GO)
$(RUN_BUF) generate --template buf.go.gen.yaml

.PHONY: validate
validate: $(BUF) $(PROTOC_GEN_GO_CYBOZU_VALIDATE)
$(RUN_BUF) generate --template buf.go-cybozu-validate.gen.yaml

.PHONY: create-tag
create-tag:
./create-tag.sh
69 changes: 67 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,67 @@
# protonorm
Normalization and validation system for Protocol Buffers
# Cybozu Public Protocol Buffers Schema

This repository contains public Protocol Buffer files and related tools from Cybozu.

This repository forms a [Buf module][module] to publish protobuf files on [BSR][BSR].
You can import packages in this repository by adding the following lines to `buf.yaml`.

```yaml
version: v1
deps:
- buf.build/cybozu/protobuf
```
If you are not familiar with `buf`, read https://docs.buf.build/manuals/cli/overview

## `cybozu.validate`

This protobuf package provides custom options to normalize and validate messages.
Read [examples/validation.proto](examples/validation.proto) for example usage.

You can get the code generator for Go as follows:

```console
$ go install github.com/cybozu/protobuf/cmd/protoc-gen-go-cybozu-validate@latest
```

The following is an example `buf.gen.yaml` to generate validation code:

```yaml
version: v1
plugins:
- plugin: go
out: .
opt: paths=source_relative
- plugin: go-cybozu-validate
out: .
opt: paths=source_relative
```

## API documentation

Visit https://buf.build/cybozu/protobuf

## Versioning and backward-compatibility

We keep backward-compatibility of Protocol Buffers files.
When we want to add breaking changes to Protocol Buffers files, we create
a different package like `cybozu.foo.v2` not to break the current package.

However, the generated code in this repository may not be backward compatible.
For example, Go code generated by `protoc-gen-go` may not be always backward compatible.

So, we use a [semver](https://semver.org/) with major version zero (0.y.z) to
tag this repository to declare the generated code may bring breaking changes.

### For developers

Running `make create-tag` creates a new tag and push it to GitHub automatically.
The rest of the release process will be done by GitHub Actions.

## License

All code in this repository is licensed under the Apache License Version 2.0.
Read [LICENSE](LICENSE) for terms and conditions.

[module]: https://docs.buf.build/bsr/overview#modules
[BSR]: https://docs.buf.build/bsr/introduction
6 changes: 6 additions & 0 deletions buf.go-cybozu-validate.gen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: v1
plugins:
- name: go-cybozu-validate
out: .
opt:
- paths=source_relative
6 changes: 6 additions & 0 deletions buf.go.gen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: v1
plugins:
- name: go
out: .
opt:
- paths=source_relative
9 changes: 9 additions & 0 deletions buf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Cybozu Public Protocol Buffers Schema

This module provides Protocol Buffers packages that are intended for public use.
Currently, following packages are available:

- `cybozu.validate`: custom options for generating normalization & validation code.
- `examples`: usage examples of our custom Protocol Buffer options.

The source code is available on https://github.com/cybozu/protobuf
11 changes: 11 additions & 0 deletions buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: v1
name: buf.build/cybozu/protobuf
breaking:
use:
- FILE
lint:
use:
- BASIC
except:
- ENUM_PASCAL_CASE
- ENUM_NO_ALLOW_ALIAS
4 changes: 4 additions & 0 deletions cmd/protoc-gen-go-cybozu-validate/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package main

func main() {
}
39 changes: 39 additions & 0 deletions create-tag.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/sh

set -e

if [ "$(git branch --show-current)" != main ]; then
echo "not on the main branch."
exit 1
fi

echo "Running git pull to be up-to-date."
git fetch origin --prune --prune-tags

MAJOR=0
MINOR=$(date '+%Y%m%d')
REV=0

while true; do
TAG=v${MAJOR}.${MINOR}.${REV}
if git tag | grep -qF ${TAG}; then
REV=$((REV + 1))
else
break
fi
done

echo "Creating a new Git tag ${TAG}"
echo -n "Proceed? [y/N]: "
read t
if echo $t | grep -i '^y'; then
:
else
echo "Aborted."
exit 1
fi

git tag -a -m "Release ${TAG}" ${TAG}
git push origin ${TAG}

echo "Tagged and pushed ${TAG}"
Loading

0 comments on commit 95e7cf7

Please sign in to comment.