Compare commits

...

No commits in common. "v3" and "oldmaster" have entirely different histories.

60 changed files with 3649 additions and 13244 deletions

15
.gitattributes vendored Normal file
View File

@ -0,0 +1,15 @@
# Auto detect text files and perform LF normalization
* text=auto
# Collapse vendored and generated files on GitHub
vendor/* linguist-vendored
rules.mk linguist-vendored
*/vendor/* linguist-vendored
*.gen.* linguist-generated
*.pb.go linguist-generated
go.sum linguist-generated
go.mod linguist-generated
gen.sum linguist-generated
# Reduce conflicts on markdown files
*.md merge=union

1
.github/CODEOWNERS vendored Normal file
View File

@ -0,0 +1 @@
* @unisack-org

View File

@ -1,24 +1,31 @@
---
name: Bug report
about: For reporting bugs in go-micro
title: "[BUG]"
labels: ''
about: Create a report to help us improve
title: "[BUG] "
labels: bug
assignees: ''
---
**Describe the bug**
A clear and concise description of what the bug is.
1. What are you trying to do?
2. What did you expect to happen?
3. What happens instead?
**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Type '....'
3. See error
**How to reproduce the bug:**
**Expected behavior**
A clear and concise description of what you expected to happen.
If possible, please include a minimal code snippet here.
**Screenshots / Logs**
If applicable, add screenshots or logs to help explain your problem.
**Environment:**
Go Version: please paste `go version` output here
```
please paste `go env` output here
```
**Versions (please complete the following information, if relevant):**
- Software version: [e.g. v1.2.3, latest, building from sources]
- OS: [e.g. Ubuntu, Mac, iOS, ...]
- Golang version [e.g. 1.13]
**Additional context**
Add any other context about the problem here.

View File

@ -1,8 +1,8 @@
---
name: Feature request / Enhancement
about: If you have a need not served by go-micro
title: "[FEATURE]"
labels: ''
name: Feature request
about: Suggest an idea for this project
title: "[IDEA] "
labels: enhancement
assignees: ''
---
@ -13,5 +13,8 @@ A clear and concise description of what the problem is. Ex. I'm always frustrate
**Describe the solution you'd like**
A clear and concise description of what you want to happen.
**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.
**Additional context**
Add any other context or screenshots about the feature request here.

View File

@ -1,14 +0,0 @@
---
name: Question
about: Ask a question about go-micro
title: ''
labels: ''
assignees: ''
---
Before asking, please check if your question has already been answered:
1. Check the documentation - https://micro.mu/docs/
2. Check the examples and plugins - https://github.com/micro/examples & https://github.com/micro/go-plugins
3. Search existing issues

View File

@ -1,9 +1,9 @@
## Pull Request template
Please, go through these steps before clicking submit on this PR.
<!--
Thank you for your contribution to this repo!
1. Give a descriptive title to your PR.
2. Provide a description of your changes.
3. Make sure you have some relevant tests.
4. Put `closes #XXXX` in your comment to auto-close the issue that your PR fixes (if applicable).
Before submitting a pull request, please check the following:
- reference any related issue, PR, link
- use the "WIP" title prefix if you need help or more time to finish your PR
**PLEASE REMOVE THIS TEMPLATE BEFORE SUBMITTING**
you can remove this markdown comment
-->

View File

@ -1,19 +0,0 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
version: 2
updates:
# Maintain dependencies for GitHub Actions
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
# Maintain dependencies for Golang
- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "daily"

19
.github/renovate.json vendored Normal file
View File

@ -0,0 +1,19 @@
{
"extends": [
"config:base"
],
"packageRules": [
{
"matchUpdateTypes": ["minor", "patch", "pin", "digest"],
"automerge": true
},
{
"groupName": "all deps",
"separateMajorMinor": true,
"groupSlug": "all",
"packagePatterns": [
"*"
]
}
]
}

View File

@ -1,20 +0,0 @@
name: "autoapprove"
on:
pull_request_target:
types: [assigned, opened, synchronize, reopened]
permissions:
pull-requests: write
contents: write
jobs:
autoapprove:
runs-on: ubuntu-latest
steps:
- name: approve
uses: hmarr/auto-approve-action@v3
if: github.actor == 'vtolstov' || github.actor == 'dependabot[bot]'
id: approve
with:
github-token: ${{ secrets.GITHUB_TOKEN }}

View File

@ -1,21 +0,0 @@
name: "automerge"
on:
pull_request_target:
types: [assigned, opened, synchronize, reopened]
permissions:
pull-requests: write
contents: write
jobs:
automerge:
runs-on: ubuntu-latest
if: github.actor == 'vtolstov'
steps:
- name: merge
id: merge
run: gh pr merge --auto --merge "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GITHUB_TOKEN: ${{secrets.TOKEN}}

View File

@ -1,47 +0,0 @@
name: build
on:
push:
branches:
- master
- v3
jobs:
test:
name: test
runs-on: ubuntu-latest
steps:
- name: setup
uses: actions/setup-go@v3
with:
go-version: 1.17
- name: checkout
uses: actions/checkout@v3
- name: cache
uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: ${{ runner.os }}-go-
- name: deps
run: go get -v -t -d ./...
- name: test
env:
INTEGRATION_TESTS: yes
run: go test -mod readonly -v ./...
lint:
name: lint
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v3
- name: lint
uses: golangci/golangci-lint-action@v3.4.0
continue-on-error: true
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.30
# Optional: working directory, useful for monorepos
# working-directory: somedir
# Optional: golangci-lint command line arguments.
# args: --issues-exit-code=0
# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true

View File

@ -1,78 +0,0 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "codeql"
on:
workflow_run:
workflows: ["prbuild"]
types:
- completed
push:
branches: [ master, v3 ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ master, v3 ]
schedule:
- cron: '34 1 * * 0'
jobs:
analyze:
name: analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write
strategy:
fail-fast: false
matrix:
language: [ 'go' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
# Learn more:
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
steps:
- name: checkout
uses: actions/checkout@v3
- name: setup
uses: actions/setup-go@v3
with:
go-version: 1.17
# Initializes the CodeQL tools for scanning.
- name: init
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: autobuild
uses: github/codeql-action/autobuild@v2
# Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language
#- run: |
# make bootstrap
# make release
- name: analyze
uses: github/codeql-action/analyze@v2

View File

@ -1,27 +0,0 @@
name: "dependabot-automerge"
on:
pull_request_target:
types: [assigned, opened, synchronize, reopened]
permissions:
pull-requests: write
contents: write
jobs:
automerge:
runs-on: ubuntu-latest
if: github.actor == 'dependabot[bot]'
steps:
- name: metadata
id: metadata
uses: dependabot/fetch-metadata@v1.3.6
with:
github-token: "${{ secrets.TOKEN }}"
- name: merge
id: merge
if: ${{contains(steps.metadata.outputs.dependency-names, 'go.unistack.org')}}
run: gh pr merge --auto --merge "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GITHUB_TOKEN: ${{secrets.TOKEN}}

View File

@ -1,47 +0,0 @@
name: prbuild
on:
pull_request:
branches:
- master
- v3
jobs:
test:
name: test
runs-on: ubuntu-latest
steps:
- name: setup
uses: actions/setup-go@v3
with:
go-version: 1.17
- name: checkout
uses: actions/checkout@v3
- name: cache
uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: ${{ runner.os }}-go-
- name: deps
run: go get -v -t -d ./...
- name: test
env:
INTEGRATION_TESTS: yes
run: go test -mod readonly -v ./...
lint:
name: lint
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v3
- name: lint
uses: golangci/golangci-lint-action@v3.4.0
continue-on-error: true
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.30
# Optional: working directory, useful for monorepos
# working-directory: somedir
# Optional: golangci-lint command line arguments.
# args: --issues-exit-code=0
# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true

37
.gitignore vendored
View File

@ -1,38 +1,27 @@
# Develop tools
/.vscode/
/.idea/
# Temporary files
*~
*#
.#*
coverage.txt
# Vendors
package-lock.json
node_modules/
vendor/
# Binaries for programs and plugins
dist/
gin-bin
*.exe
*.exe~
*.dll
*.so
*.dylib
# Folders
_obj
_test
_build
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
# Test binary, build with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# vim temp files
*~
*.swp
*.swo
/protoc-gen-go-micro
/protoc-gen-micro

34
.golangci.yml Normal file
View File

@ -0,0 +1,34 @@
run:
deadline: 1m
tests: false
#skip-files:
# - ".*\\.gen\\.go"
linters-settings:
golint:
min-confidence: 0
maligned:
suggest-new: true
goconst:
min-len: 5
min-occurrences: 4
misspell:
locale: US
linters:
disable-all: true
enable:
- goconst
- misspell
- deadcode
- misspell
- structcheck
- errcheck
- unused
- varcheck
- staticcheck
- unconvert
- gofmt
- goimports
- golint
- ineffassign

28
AUTHORS Normal file
View File

@ -0,0 +1,28 @@
# This file lists all individuals having contributed content to the repository.
Alexandre Beslic <abeslic@abronan.com>
Anastasia DERUELLE <anastasia.deruelle@gmail.com>
Gero <geronimo@eclypsium.com>
gfanton <guilhem.fanton@gmail.com>
Guilhem Fanton <guilhem.fanton@gmail.com>
Jan Weitz <jan@iosphere.de>
jhayotte <julien.hayotte@gmail.com>
Julien Hayotte <julien.hayotte@gmail.com>
Manfred Touron <m@42.am>
Mathieu Acthernoene <zoontek@gmail.com>
Mike Lee <mike.lee@safeguardproperties.com>
moul-bot <41326314+moul-bot@users.noreply.github.com>
Pat Moroney <pat@pat.email>
Pat Moroney <pmoroney@name.com>
Peter Monko <piotrek.monko@gmail.com>
Pierre Roullon <pierre.roullon@gmail.com>
Quentin Perez <qperez@ocs.online.net>
Renovate Bot <bot@renovateapp.com>
Sacha Froment <sfroment42@gmail.com>
Shogo Iwano <shiwano@gmail.com>
Thomas KERAMBLOCH <tkerambloch@vente-privee.com>
Tommy PAGEARD <tpageard@vente-privee.com>
Valerio Gheri <valerio.gheri@gmail.com>
Victor Login <batazor111@gmail.com>
webii <michal.jaglewicz@gmail.com>
Vasiliy Tolstov <v.tolstov@unistack.org>

204
LICENSE
View File

@ -1,191 +1,23 @@
MIT License
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
Copyright (c) 2016-2021 Manfred Touron
Copyright (c) 2021 Unistack LLC
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
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:
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
Copyright 2021 Unistack LLC.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
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.

125
README.md
View File

@ -1,5 +1,5 @@
# `protoc-gen-go-micro`
protobuf plugin to generate helper code for micro framework
# `protoc-gen-micro`
:open_file_folder: protocol generator + golang text/template (protobuf)
A generic **code**/script/data generator based on [Protobuf](https://developers.google.com/protocol-buffers/).
@ -7,20 +7,129 @@ A generic **code**/script/data generator based on [Protobuf](https://developers.
This project is a generator plugin for the Google Protocol Buffers compiler (`protoc`).
The plugin parses **protobuf** files, generates an **ast**, and walks a local **templates directory** to generate files using the [Golang's `text/template` engine](https://golang.org/pkg/text/template/).
## Philosophy
* protobuf-first
* no built-in template, only user defined templates
* kiss, *keep it stupid simple*
## Under the hood
1. the *user* `protobuf` files are parsed by [`protoc`](https://github.com/google/protobuf/releases)
2. the `ast` is generated by [`protoc-gen-go` helpers](https://github.com/golang/protobuf/tree/master/protoc-gen-go)
3. the `ast` is given to [Golang's `text/template` engine](https://golang.org/pkg/text/template/) for each *user* template files
4. the *funcmap* enriching the template engine is based on [Masterminds/sprig](https://github.com/Masterminds/sprig), and contains type-manipulation, iteration and language-specific helpers
## Usage
`protoc-gen-micro` requires a **template_dir** directory *(by default `./templates`)*.
Every file ending with `.tmpl` will be processed and written to the destination folder, following the file hierarchy of the `template_dir`, and remove the `.tmpl` extension.
---
```console
$> protoc --go_micro_out=debug=true,components="micro|http":. input.proto
$> ls -R
input.proto templates/doc.txt.tmpl templates/config.json.tmpl
$> protoc --micro_out=. input.proto
$> ls -R
input.proto templates/doc.txt.tmpl templates/config.json.tmpl
doc.txt config.json
```
### Options
You can specify custom options, as follow:
```console
$> protoc --micro_out=debug=true,template_dir=/path/to/template/directory:. input.proto
```
| Option | Default Value | Accepted Values | Description
|-----------------------|---------------|---------------------------|-----------------------
| `tag_path` | `.` | `any local path` | path contains generated protobuf code that needs to be tagged
| `template_repo` | `` | url in form schema://domain | path to repo with optional branch or revision after @ sign
| `template_dir`       | `./template` | absolute or relative path | path to look for templates
| `destination_dir`     | `.`           | absolute or relative path | base path to write output
| `single-package-mode` | *false* | `true` or `false` | if *true*, `protoc` won't accept multiple packages to be compiled at once (*!= from `all`*), but will support `Message` lookup across the imported protobuf dependencies
| `debug`               | *false*       | `true` or `false` | if *true*, `protoc` will generate a more verbose output
| `components` | `micro` | `micro rpc http chi gorilla client server` | some values can't coexists like gorilla/chi or rpc/http, values must be concatinated with pipe symbol
| `all`                 | *false*       | `true` or `false`         | if *true*, protobuf files without `Service` will also be parsed
| `components` | `micro` | `micro|grpc|http|chi|gorilla` | some values cant coexists like gorilla/chi or grpc/http
##### Hints
Shipping the templates with your project is very smart and useful when contributing on git-based projects.
Another workflow consists in having a dedicated repository for generic templates which is then versioned and vendored with multiple projects (npm package, golang vendor package, ...)
## Funcmap
This project uses [Masterminds/sprig](https://github.com/Masterminds/sprig) library and additional functions to extend the builtin [text/template](https://golang.org/pkg/text/template) helpers.
Non-exhaustive list of new helpers:
* **all the functions from [sprig](https://github.com/Masterminds/sprig)**
* `add`
* `boolFieldExtension`
* `camelCase`
* `contains`
* `divide`
* `fieldMapKeyType`
* `fieldMapValueType`
* `first`
* `getEnumValue`
* `getMessageType`
* `getProtoFile`
* `goNormalize`
* `goTypeWithPackage`
* `goType`
* `goZeroValue`
* `haskellType`
* `httpBody`
* `httpPath`
* `httpPathsAdditionalBindings`
* `httpVerb`
* `index`
* `int64FieldExtension`
* `isFieldMap`
* `isFieldMessageTimeStamp`
* `isFieldMessage`
* `isFieldRepeated`
* `jsSuffixReserved`
* `jsType`
* `json`
* `kebabCase`
* `last`
* `leadingComment`
* `leadingDetachedComments`
* `lowerCamelCase`
* `lowerFirst`
* `lowerGoNormalize`
* `multiply`
* `namespacedFlowType`
* `prettyjson`
* `replaceDict`
* `shortType`
* `snakeCase`
* `splitArray`
* `stringFieldExtension`
* `stringMethodOptionsExtension`
* `string`
* `subtract`
* `trailingComment`
* `trimstr`
* `upperFirst`
* `urlHasVarsFromMessage`
See the project helpers for the complete list.
## Install
* Install the **go** compiler and tools from https://golang.org/doc/install
* Install **protoc-gen-go**: `go install google.golang.org/protobuf/cmd/protoc-gen-go`
* Install **protoc-gen-go-micro**: `go install go.unistack.org/protoc-gen-go-micro/v3`
* Install the **Go** compiler and tools from https://golang.org/doc/install
* Install **protobuf**: `go get -u github.com/golang/protobuf/{proto,protoc-gen-go}`
* Install **protoc-gen-micro**: `go get -u github.com/unistack-org/protoc-gen-micro`
## License
MIT

22
assets.go Normal file
View File

@ -0,0 +1,22 @@
// +build dev
package main
import (
"log"
"github.com/shurcooL/vfsgen"
"github.com/unistack-org/protoc-gen-micro/v3/assets"
)
func main() {
err := vfsgen.Generate(assets.Assets, vfsgen.Options{
PackageName: "assets",
BuildTags: "!dev",
VariableName: "Assets",
Filename: "assets/vfsdata.go",
})
if err != nil {
log.Fatal(err)
}
}

20
assets/assets.go Normal file
View File

@ -0,0 +1,20 @@
// +build dev
package assets
import (
"go/build"
"log"
"net/http"
)
func importPathToDir(importPath string) string {
p, err := build.Import(importPath, "", build.FindOnly)
if err != nil {
log.Fatal(err)
}
return p.Dir
}
// Assets contains the project's assets.
var Assets http.FileSystem = http.Dir("./templates")

1
assets/stub.go Normal file
View File

@ -0,0 +1 @@
package assets

218
assets/vfsdata.go Normal file

File diff suppressed because one or more lines are too long

170
ast.go
View File

@ -1,170 +0,0 @@
package main
import (
"go/ast"
"go/format"
"go/parser"
"go/token"
"os"
"path/filepath"
"strings"
"github.com/fatih/structtag"
tag_options "go.unistack.org/micro-proto/v3/tag"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
)
var astFields = make(map[string]map[string]map[string]*structtag.Tags) // map proto file with proto message ast struct
func (g *Generator) astFill(file *protogen.File, message *protogen.Message) error {
for _, field := range message.Fields {
if field.Desc.Options() == nil {
continue
}
if !proto.HasExtension(field.Desc.Options(), tag_options.E_Tags) {
continue
}
opts := proto.GetExtension(field.Desc.Options(), tag_options.E_Tags)
if opts != nil {
fpath := filepath.Join(g.tagPath, file.GeneratedFilenamePrefix+".pb.go")
mp, ok := astFields[fpath]
if !ok {
mp = make(map[string]map[string]*structtag.Tags)
}
nmp, ok := mp[message.GoIdent.GoName]
if !ok {
nmp = make(map[string]*structtag.Tags)
}
tags, err := structtag.Parse(opts.(string))
if err != nil {
return err
}
nmp[field.GoName] = tags
mp[message.GoIdent.GoName] = nmp
astFields[fpath] = mp
}
}
for _, nmessage := range message.Messages {
if err := g.astFill(file, nmessage); err != nil {
return err
}
}
return nil
}
func (g *Generator) astGenerate(plugin *protogen.Plugin) error {
if g.tagPath == "" {
return nil
}
for _, file := range plugin.Files {
if !file.Generate {
continue
}
for _, message := range file.Messages {
if err := g.astFill(file, message); err != nil {
return err
}
}
}
for file, mp := range astFields {
fset := token.NewFileSet()
pf, err := parser.ParseFile(fset, file, nil, parser.AllErrors|parser.ParseComments)
if err != nil {
return err
}
r := retag{}
f := func(n ast.Node) ast.Visitor {
if r.err != nil {
return nil
}
if v, ok := n.(*ast.TypeSpec); ok {
r.fields = mp[v.Name.Name]
return r
}
return nil
}
ast.Walk(structVisitor{f}, pf)
if r.err != nil {
return err
}
fp, err := os.OpenFile(file, os.O_WRONLY|os.O_TRUNC, os.FileMode(0644))
if err != nil {
return err
}
if err = format.Node(fp, fset, pf); err != nil {
fp.Close()
return err
}
if err = fp.Close(); err != nil {
return err
}
}
return nil
}
type retag struct {
err error
fields map[string]*structtag.Tags
}
func (v retag) Visit(n ast.Node) ast.Visitor {
if v.err != nil {
return nil
}
if f, ok := n.(*ast.Field); ok {
if len(f.Names) == 0 {
return nil
}
newTags := v.fields[f.Names[0].String()]
if newTags == nil {
return nil
}
if f.Tag == nil {
f.Tag = &ast.BasicLit{
Kind: token.STRING,
}
}
oldTags, err := structtag.Parse(strings.Trim(f.Tag.Value, "`"))
if err != nil {
v.err = err
return nil
}
for _, t := range newTags.Tags() {
oldTags.Set(t)
}
f.Tag.Value = "`" + oldTags.String() + "`"
return nil
}
return v
}
type structVisitor struct {
visitor func(n ast.Node) ast.Visitor
}
func (v structVisitor) Visit(n ast.Node) ast.Visitor {
if tp, ok := n.(*ast.TypeSpec); ok {
if _, ok := tp.Type.(*ast.StructType); ok {
ast.Walk(v.visitor(n), n)
return nil // This will ensure this struct is no longer traversed
}
}
return v
}

78
chi.go
View File

@ -1,78 +0,0 @@
package main
import (
"google.golang.org/protobuf/compiler/protogen"
)
var chiPackageFiles map[protogen.GoPackageName]struct{}
func (g *Generator) chiGenerate(component string, plugin *protogen.Plugin) error {
chiPackageFiles = make(map[protogen.GoPackageName]struct{})
for _, file := range plugin.Files {
if !file.Generate {
continue
}
if len(file.Services) == 0 {
continue
}
if _, ok := chiPackageFiles[file.GoPackageName]; ok {
continue
}
chiPackageFiles[file.GoPackageName] = struct{}{}
gname := "micro" + "_" + component + ".pb.go"
path := file.GoImportPath
if g.standalone {
path = "."
}
gfile := plugin.NewGeneratedFile(gname, path)
gfile.P("// Code generated by protoc-gen-go-micro. DO NOT EDIT.")
gfile.P("// protoc-gen-go-micro version: " + versionComment)
gfile.P()
gfile.P("package ", file.GoPackageName)
gfile.P()
gfile.Import(contextPackage)
gfile.Import(fmtPackage)
gfile.Import(httpPackage)
gfile.Import(reflectPackage)
gfile.Import(stringsPackage)
gfile.Import(chiPackage)
gfile.Import(chiMiddlewarePackage)
gfile.P("type routeKey struct{}")
gfile.P("func RouteName(ctx ", contextPackage.Ident("Context"), ") (string, bool) {")
gfile.P("value, ok := ctx.Value(routeKey{}).(string)")
gfile.P("return value, ok")
gfile.P("}")
gfile.P()
gfile.P("func RegisterHandlers(r *", chiPackage.Ident("Mux"), ", h interface{}, eps []", microServerHttpPackage.Ident("EndpointMetadata"), ") error {")
gfile.P("v := ", reflectPackage.Ident("ValueOf"), "(h)")
gfile.P("if v.NumMethod() < 1 {")
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("handler has no methods: %T", h)`)
gfile.P("}")
gfile.P("for _, ep := range eps {")
gfile.P(`idx := `, stringsPackage.Ident("Index"), `(ep.Name, ".")`)
gfile.P(`if idx < 1 || len(ep.Name) <= idx {`)
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid endpoint name: %s", ep.Name)`)
gfile.P("}")
gfile.P(`name := ep.Name[idx+1:]`)
gfile.P("m := v.MethodByName(name)")
gfile.P("if !m.IsValid() || m.IsZero() {")
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler, method %s not found", name)`)
gfile.P("}")
gfile.P("rh, ok := m.Interface().(func(", httpPackage.Ident("ResponseWriter"), ", *", httpPackage.Ident("Request"), "))")
gfile.P("if !ok {")
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler: %#+v", m.Interface())`)
gfile.P("}")
gfile.P("r.With(", chiMiddlewarePackage.Ident("WithValue"), `(routeKey{}, ep.Name)).MethodFunc(ep.Method, ep.Path, rh)`)
gfile.P("}")
gfile.P("return nil")
gfile.P("}")
}
return nil
}

50
clean.go Normal file
View File

@ -0,0 +1,50 @@
package main
import (
"go/parser"
"go/token"
"os"
"path/filepath"
"strings"
)
func isGenerated(name string) bool {
const (
genCodeGenerated = "code generated"
genDoNotEdit = "do not edit"
genAutoFile = "autogenerated file"
)
markers := []string{genCodeGenerated, genDoNotEdit, genAutoFile}
fileset := token.NewFileSet()
syntax, err := parser.ParseFile(fileset, name, nil, parser.PackageClauseOnly|parser.ParseComments)
if err != nil {
return false
}
for _, comment := range syntax.Comments {
for _, marker := range markers {
if strings.Contains(strings.ToLower(comment.Text()), marker) {
return true
}
}
}
return false
}
func cleanDir(dir string) error {
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if dir == "vendor" {
return filepath.SkipDir
}
if isGenerated(path) {
err = os.Remove(path)
}
return err
})
}

104
clone.go Normal file
View File

@ -0,0 +1,104 @@
package main
import (
"context"
"fmt"
"go/types"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/filemode"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/storage/memory"
)
func clone(srcRepo string, dstDir string) error {
if !strings.HasPrefix(srcRepo, "https://") {
srcRepo = "https://" + srcRepo
}
u, err := url.Parse(srcRepo)
if err != nil {
return err
}
var rev string
if idx := strings.Index(u.Path, "@"); idx > 0 {
rev = u.Path[idx+1:]
}
cloneOpts := &git.CloneOptions{
URL: srcRepo,
// Progress: os.Stdout,
}
if len(rev) == 0 {
cloneOpts.SingleBranch = true
cloneOpts.Depth = 1
}
if err := cloneOpts.Validate(); err != nil {
return err
}
repo, err := git.CloneContext(context.Background(), memory.NewStorage(), nil, cloneOpts)
if err != nil {
return err
}
ref, err := repo.Head()
if err != nil {
return err
}
commit, err := repo.CommitObject(ref.Hash())
if err != nil {
return err
}
tree, err := commit.Tree()
if err != nil {
return err
}
if err := os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
return err
}
if err := cleanDir(dstDir); err != nil {
return err
}
err = tree.Files().ForEach(func(file *object.File) error {
if file == nil {
return types.Error{Msg: "file pointer is empty"}
}
fmode, err := file.Mode.ToOSFileMode()
if err != nil {
return err
}
switch file.Mode {
case filemode.Executable:
return writeFile(file, dstDir, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, fmode)
case filemode.Dir:
return os.MkdirAll(filepath.Join(dstDir, file.Name), fmode)
case filemode.Regular:
return writeFile(file, dstDir, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, fmode)
default:
return fmt.Errorf("unsupported filetype %v for %s", file.Mode, file.Name)
}
return nil
})
if err != nil {
return err
}
return nil
}

46
copy.go Normal file
View File

@ -0,0 +1,46 @@
package main
import (
"io"
"log"
"os"
"path/filepath"
"github.com/go-git/go-git/v5/plumbing/object"
)
func writeFile(file *object.File, dir string, flag int, mode os.FileMode) error {
path := filepath.Join(dir, file.Name)
if err := os.MkdirAll(filepath.Dir(path), os.FileMode(0755)); err != nil {
return err
}
w, err := os.OpenFile(path, flag, mode)
if err != nil {
return err
}
defer func() {
if err := w.Close(); err != nil {
log.Printf("Err: failed to close file: %v", err)
}
}()
r, err := file.Reader()
if err != nil {
return err
}
defer func() {
if err := r.Close(); err != nil {
log.Printf("Err: failed to close file: %v", err)
}
}()
if _, err = io.Copy(w, r); err != nil {
return err
}
return nil
}

291
encoder.go Normal file
View File

@ -0,0 +1,291 @@
package main
import (
"bytes"
"encoding/base64"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"path/filepath"
"strings"
"text/template"
"time"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
plugin_go "github.com/golang/protobuf/protoc-gen-go/plugin"
"github.com/unistack-org/protoc-gen-micro/v3/assets"
pgghelpers "github.com/unistack-org/protoc-gen-micro/v3/helpers"
)
type GenericTemplateBasedEncoder struct {
templateDir string
service *descriptor.ServiceDescriptorProto
file *descriptor.FileDescriptorProto
enum []*descriptor.EnumDescriptorProto
debug bool
destinationDir string
}
type Ast struct {
BuildDate time.Time `json:"build-date"`
BuildHostname string `json:"build-hostname"`
BuildUser string `json:"build-user"`
GoPWD string `json:"go-pwd,omitempty"`
PWD string `json:"pwd"`
Debug bool `json:"debug"`
DestinationDir string `json:"destination-dir"`
File *descriptor.FileDescriptorProto `json:"file"`
RawFilename string `json:"raw-filename"`
Filename string `json:"filename"`
TemplateDir string `json:"template-dir"`
Service *descriptor.ServiceDescriptorProto `json:"service"`
Enum []*descriptor.EnumDescriptorProto `json:"enum"`
}
func NewGenericServiceTemplateBasedEncoder(templateDir string, service *descriptor.ServiceDescriptorProto, file *descriptor.FileDescriptorProto, debug bool, destinationDir string) (e *GenericTemplateBasedEncoder) {
e = &GenericTemplateBasedEncoder{
service: service,
file: file,
templateDir: templateDir,
debug: debug,
destinationDir: destinationDir,
enum: file.GetEnumType(),
}
if debug {
log.Printf("new encoder: file=%q service=%q template-dir=%q", file.GetName(), service.GetName(), templateDir)
}
pgghelpers.InitPathMap(file)
return
}
func NewGenericTemplateBasedEncoder(templateDir string, file *descriptor.FileDescriptorProto, debug bool, destinationDir string) (e *GenericTemplateBasedEncoder) {
e = &GenericTemplateBasedEncoder{
service: nil,
file: file,
templateDir: templateDir,
enum: file.GetEnumType(),
debug: debug,
destinationDir: destinationDir,
}
if debug {
log.Printf("new encoder: file=%q template-dir=%q", file.GetName(), templateDir)
}
pgghelpers.InitPathMap(file)
return
}
func (e *GenericTemplateBasedEncoder) templates() ([]string, error) {
filenames := []string{}
if e.templateDir == "" {
dir, err := assets.Assets.Open("/")
if err != nil {
return nil, fmt.Errorf("failed to open assets dir")
}
fi, err := dir.Readdir(-1)
if err != nil {
return nil, fmt.Errorf("failed to get assets files")
}
if debug {
log.Printf("components to generate: %v", components)
}
for _, f := range fi {
name := f.Name()
skip := true
if dname, err := base64.StdEncoding.DecodeString(name); err == nil {
name = string(dname)
}
for _, component := range components {
if component == "all" || strings.Contains(name, "_"+component+".pb.go.tmpl") {
skip = false
}
}
if skip {
if debug {
log.Printf("skip template %s", name)
}
continue
}
if f.IsDir() {
continue
}
if filepath.Ext(name) != ".tmpl" {
continue
}
if e.debug {
log.Printf("new template: %q", name)
}
filenames = append(filenames, name)
}
return filenames, nil
}
err := filepath.Walk(e.templateDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if filepath.Ext(path) != ".tmpl" {
return nil
}
rel, err := filepath.Rel(e.templateDir, path)
if err != nil {
return err
}
if e.debug {
log.Printf("new template: %q", rel)
}
filenames = append(filenames, rel)
return nil
})
return filenames, err
}
func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, error) {
// prepare the ast passed to the template engine
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
pwd, err := os.Getwd()
if err != nil {
return nil, err
}
goPwd := ""
if os.Getenv("GOPATH") != "" {
goPwd, err = filepath.Rel(os.Getenv("GOPATH")+"/src", pwd)
if err != nil {
return nil, err
}
if strings.Contains(goPwd, "../") {
goPwd = ""
}
}
ast := Ast{
BuildDate: time.Now(),
BuildHostname: hostname,
BuildUser: os.Getenv("USER"),
PWD: pwd,
GoPWD: goPwd,
File: e.file,
TemplateDir: e.templateDir,
DestinationDir: e.destinationDir,
RawFilename: templateFilename,
Filename: "",
Service: e.service,
Enum: e.enum,
}
buffer := new(bytes.Buffer)
unescaped, err := url.QueryUnescape(templateFilename)
if err != nil {
log.Printf("failed to unescape filepath %q: %v", templateFilename, err)
} else {
templateFilename = unescaped
}
tmpl, err := template.New("").Funcs(pgghelpers.ProtoHelpersFuncMap).Parse(templateFilename)
if err != nil {
return nil, err
}
if err := tmpl.Execute(buffer, ast); err != nil {
return nil, err
}
ast.Filename = buffer.String()
return &ast, nil
}
func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (string, string, error) {
var tmpl *template.Template
var err error
if e.templateDir == "" {
fs, err := assets.Assets.Open("/" + string(base64.StdEncoding.EncodeToString([]byte(templateFilename))))
if err != nil {
fs, err = assets.Assets.Open("/" + templateFilename)
}
if err != nil {
return "", "", err
}
buf, err := ioutil.ReadAll(fs)
if err != nil {
return "", "", err
}
if err = fs.Close(); err == nil {
tmpl, err = template.New("/" + templateFilename).Funcs(pgghelpers.ProtoHelpersFuncMap).Parse(string(buf))
}
} else {
// initialize template engine
fullPath := filepath.Join(e.templateDir, templateFilename)
templateName := filepath.Base(fullPath)
tmpl, err = template.New(templateName).Funcs(pgghelpers.ProtoHelpersFuncMap).ParseFiles(fullPath)
}
if err != nil {
return "", "", err
} else if tmpl == nil {
return "", "", fmt.Errorf("template for %s is nil", templateFilename)
}
ast, err := e.genAst(templateFilename)
if err != nil {
return "", "", err
}
// generate the content
buffer := new(bytes.Buffer)
if err := tmpl.Execute(buffer, ast); err != nil {
return "", "", err
}
return buffer.String(), ast.Filename, nil
}
func (e *GenericTemplateBasedEncoder) Files() []*plugin_go.CodeGeneratorResponse_File {
templates, err := e.templates()
if err != nil {
log.Fatalf("cannot get templates from %q: %v", e.templateDir, err)
}
length := len(templates)
files := make([]*plugin_go.CodeGeneratorResponse_File, 0, length)
errChan := make(chan error, length)
resultChan := make(chan *plugin_go.CodeGeneratorResponse_File, length)
for _, templateFilename := range templates {
go func(tmpl string) {
var translatedFilename, content string
content, translatedFilename, err = e.buildContent(tmpl)
if err != nil {
errChan <- err
return
}
filename := translatedFilename[:len(translatedFilename)-len(".tmpl")]
resultChan <- &plugin_go.CodeGeneratorResponse_File{
Content: &content,
Name: &filename,
}
}(templateFilename)
}
for i := 0; i < length; i++ {
select {
case f := <-resultChan:
files = append(files, f)
case err = <-errChan:
panic(err)
}
}
return files
}

View File

@ -1,40 +0,0 @@
syntax = "proto3";
package example;
option go_package = "github.com/unistack-org/protoc-gen-go-micro/v3/example;examplepb";
import "tag/tag.proto";
import "api/annotations.proto";
import "openapiv3/annotations.proto";
//import "google/protobuf/wrappers.proto";
import "graphql/graphql.proto";
service Example {
rpc Call(CallReq) returns (CallRsp) {
option (micro.graphql.rpc) = {type: QUERY};
option (micro.openapiv3.openapiv3_operation) = {
operation_id: "Call";
responses: {
default: {
reference: {_ref: ".example.Error"};
};
};
};
option (micro.api.http) = { post: "/v1/example/call/{name}"; body: "*"; };
option (micro.api.micro_method) = { timeout: "5s"; };
};
};
message CallReq {
string name = 1 [(micro.graphql.field) = {required: true}];
string req = 2;
};
message CallRsp {
string rsp = 2;
};
message Error {
string msg = 1;
};

View File

@ -1,11 +0,0 @@
directive @Example on FIELD_DEFINITION
input CallReqInput {
name: String!
req: String
}
type CallRsp {
rsp: String
}
type Query {
exampleCall(in: CallReqInput): CallRsp
}

View File

@ -1,24 +0,0 @@
package main
import (
"log"
"os"
"golang.org/x/tools/go/analysis/passes/fieldalignment"
"golang.org/x/tools/go/analysis/singlechecker"
"google.golang.org/protobuf/compiler/protogen"
)
func (g *Generator) fieldAlign(plugin *protogen.Plugin) error {
if !g.fieldaligment {
return nil
}
log.Printf("%v\n", []string{"fieldalignment", "-fix", g.tagPath})
origArgs := os.Args
os.Args = []string{"fieldalignment", "-fix", g.tagPath}
singlechecker.Main(fieldalignment.Analyzer)
os.Args = origArgs
return nil
}

View File

@ -1,3 +1,3 @@
package main
//go:generate sh -xc "protoc -I./example -I. -I$(go list -f '{{ .Dir }}' -m go.unistack.org/micro-proto/v3) --go-micro_out=components=graphqls,graphql_file=./schema.graphql:./example example/example.proto"
//go:generate go run -tags dev assets.go

42
go.mod generated
View File

@ -1,31 +1,19 @@
module go.unistack.org/protoc-gen-go-micro/v3
module github.com/unistack-org/protoc-gen-micro/v3
go 1.22.0
toolchain go1.23.4
go 1.15
require (
github.com/fatih/structtag v1.2.0
github.com/jhump/protoreflect v1.17.0
github.com/vektah/gqlparser/v2 v2.5.20
go.unistack.org/micro-proto/v3 v3.4.1
golang.org/x/tools v0.28.0
google.golang.org/protobuf v1.35.2
)
require (
github.com/bufbuild/protocompile v0.14.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic v0.7.0 // indirect
github.com/google/gnostic-models v0.6.9 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rogpeppe/go-internal v1.13.1 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/text v0.21.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241209162323-e6fa225c2576 // indirect
google.golang.org/grpc v1.68.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.2
github.com/go-git/go-git/v5 v5.2.0
github.com/golang/protobuf v1.4.3
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.2.0
github.com/huandu/xstrings v1.3.2
github.com/mitchellh/reflectwalk v1.0.1 // indirect
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 // indirect
github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a // indirect
golang.org/x/net v0.0.0-20200904194848-62affa334b73 // indirect
google.golang.org/genproto v0.0.0-20210218151259-fe80b386bf06
google.golang.org/protobuf v1.25.0
)

1322
go.sum generated

File diff suppressed because it is too large Load Diff

View File

@ -1,69 +0,0 @@
package main
import (
"google.golang.org/protobuf/compiler/protogen"
)
var gorillaPackageFiles map[protogen.GoPackageName]struct{}
func (g *Generator) gorillaGenerate(component string, plugin *protogen.Plugin) error {
gorillaPackageFiles = make(map[protogen.GoPackageName]struct{})
for _, file := range plugin.Files {
if !file.Generate {
continue
}
if len(file.Services) == 0 {
continue
}
if _, ok := gorillaPackageFiles[file.GoPackageName]; ok {
continue
}
gorillaPackageFiles[file.GoPackageName] = struct{}{}
gname := "micro" + "_" + component + ".pb.go"
path := file.GoImportPath
if g.standalone {
path = "."
}
gfile := plugin.NewGeneratedFile(gname, path)
gfile.P("// Code generated by protoc-gen-go-micro. DO NOT EDIT.")
gfile.P("// protoc-gen-go-micro version: " + versionComment)
gfile.P()
gfile.P("package ", file.GoPackageName)
gfile.P()
gfile.Import(fmtPackage)
gfile.Import(httpPackage)
gfile.Import(reflectPackage)
gfile.Import(stringsPackage)
gfile.Import(gorillaMuxPackage)
gfile.P("func RegisterHandlers(r *", gorillaMuxPackage.Ident("Router"), ", h interface{}, eps []", microServerHttpPackage.Ident("EndpointMetadata"), ") error {")
gfile.P("v := ", reflectPackage.Ident("ValueOf"), "(h)")
gfile.P("if v.NumMethod() < 1 {")
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("handler has no methods: %T", h)`)
gfile.P("}")
gfile.P("for _, ep := range eps {")
gfile.P(`idx := `, stringsPackage.Ident("Index"), `(ep.Name, ".")`)
gfile.P(`if idx < 1 || len(ep.Name) <= idx {`)
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid endpoint name: %s", ep.Name)`)
gfile.P("}")
gfile.P(`name := ep.Name[idx+1:]`)
gfile.P("m := v.MethodByName(name)")
gfile.P("if !m.IsValid() || m.IsZero() {")
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler, method %s not found", name)`)
gfile.P("}")
gfile.P("rh, ok := m.Interface().(func(", httpPackage.Ident("ResponseWriter"), ", *", httpPackage.Ident("Request"), "))")
gfile.P("if !ok {")
gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler: %#+v", m.Interface())`)
gfile.P("}")
gfile.P(`r.HandleFunc(ep.Path, rh).Methods(ep.Method).Name(ep.Name)`)
gfile.P("}")
gfile.P("return nil")
gfile.P("}")
}
return nil
}

View File

@ -1,9 +0,0 @@
package main
import (
"google.golang.org/protobuf/compiler/protogen"
)
func (g *Generator) graphqlGenerate(plugin *protogen.Plugin) error {
return nil
}

View File

@ -1,32 +0,0 @@
package generator
type Callstack interface {
Push(entry interface{})
Pop(entry interface{})
Has(entry interface{}) bool
}
func NewCallstack() Callstack {
return &callstack{stack: make(map[interface{}]int), index: 0}
}
type callstack struct {
stack map[interface{}]int
sorted []string
index int
}
func (c *callstack) Pop(entry interface{}) {
delete(c.stack, entry)
c.index--
}
func (c *callstack) Push(entry interface{}) {
c.stack[entry] = c.index
c.index++
}
func (c *callstack) Has(entry interface{}) bool {
_, ok := c.stack[entry]
return ok
}

View File

@ -1,77 +0,0 @@
package generator
import (
"strings"
"github.com/jhump/protoreflect/desc"
"github.com/vektah/gqlparser/v2/ast"
any "google.golang.org/protobuf/types/known/anypb"
)
type ObjectDescriptor struct {
*ast.Definition
desc.Descriptor
types []*ObjectDescriptor
fields []*FieldDescriptor
fieldNames map[string]*FieldDescriptor
}
func (o *ObjectDescriptor) AsGraphql() *ast.Definition {
return o.Definition
}
func (o *ObjectDescriptor) uniqueName(f *desc.FieldDescriptor) string {
return strings.Title(f.GetName())
}
func (o *ObjectDescriptor) IsInput() bool {
return o.Kind == ast.InputObject
}
func (o *ObjectDescriptor) GetFields() []*FieldDescriptor {
return o.fields
}
func (o *ObjectDescriptor) GetTypes() []*ObjectDescriptor {
return o.types
}
func (o *ObjectDescriptor) IsMessage() bool {
_, ok := o.Descriptor.(*desc.MessageDescriptor)
return ok
}
// same isEmpty but for mortals
func IsEmpty(o *desc.MessageDescriptor) bool { return isEmpty(o, NewCallstack()) }
// make sure objects are fulled with all objects
func isEmpty(o *desc.MessageDescriptor, callstack Callstack) bool {
callstack.Push(o)
defer callstack.Pop(o)
if len(o.GetFields()) == 0 {
return true
}
for _, f := range o.GetFields() {
objType := f.GetMessageType()
if objType == nil {
return false
}
// check if the call stack already contains a reference to this type and prevent it from calling itself again
if callstack.Has(objType) {
return true
}
if !isEmpty(objType, callstack) {
return false
}
}
return true
}
// TODO maybe not compare by strings
func IsAny(o *desc.MessageDescriptor) bool {
return string((&any.Any{}).ProtoReflect().Descriptor().FullName()) == o.GetFullyQualifiedName()
}

View File

@ -1,761 +0,0 @@
package generator
import (
"fmt"
"strconv"
"strings"
"github.com/jhump/protoreflect/desc"
"github.com/vektah/gqlparser/v2/ast"
"google.golang.org/protobuf/compiler/protogen"
descriptor "google.golang.org/protobuf/types/descriptorpb"
gqlpb "go.unistack.org/micro-proto/v3/graphql"
)
const (
fieldPrefix = "Field"
inputSuffix = "Input"
typeSep = "_"
packageSep = "."
anyTypeDescription = "Any is any json type"
scalarBytes = "Bytes"
goFieldDirective = "goField"
DefaultExtension = "graphql"
)
func NewSchemas(descs []*desc.FileDescriptor, mergeSchemas, genServiceDesc bool, plugin *protogen.Plugin) (schemas SchemaDescriptorList, err error) {
var files []*descriptor.FileDescriptorProto
for _, d := range descs {
files = append(files, d.AsFileDescriptorProto())
}
var goref GoRef
if plugin != nil {
goref, err = NewGoRef(plugin)
if err != nil {
return nil, err
}
}
if mergeSchemas {
schema := NewSchemaDescriptor(genServiceDesc, goref)
for _, file := range descs {
err := generateFile(file, schema)
if err != nil {
return nil, err
}
}
return []*SchemaDescriptor{schema}, nil
}
for _, file := range descs {
schema := NewSchemaDescriptor(genServiceDesc, goref)
err := generateFile(file, schema)
if err != nil {
return nil, err
}
schemas = append(schemas, schema)
}
return
}
func generateFile(file *desc.FileDescriptor, schema *SchemaDescriptor) error {
schema.FileDescriptors = append(schema.FileDescriptors, file)
for _, svc := range file.GetServices() {
svcOpts := GraphqlServiceOptions(svc.AsServiceDescriptorProto().GetOptions())
if svcOpts != nil && svcOpts.Ignore != nil && *svcOpts.Ignore {
continue
}
for _, rpc := range svc.GetMethods() {
rpcOpts := GraphqlMethodOptions(rpc.AsMethodDescriptorProto().GetOptions())
if rpcOpts != nil && rpcOpts.Ignore != nil && *rpcOpts.Ignore {
continue
}
in, err := schema.CreateObjects(rpc.GetInputType(), true)
if err != nil {
return err
}
out, err := schema.CreateObjects(rpc.GetOutputType(), false)
if err != nil {
return err
}
if rpc.IsServerStreaming() && rpc.IsClientStreaming() {
schema.GetMutation().addMethod(svc, rpc, in, out)
}
if rpc.IsServerStreaming() {
schema.GetSubscription().addMethod(svc, rpc, in, out)
} else {
switch GetRequestType(rpcOpts, svcOpts) {
case gqlpb.Type_QUERY:
schema.GetQuery().addMethod(svc, rpc, in, out)
default:
schema.GetMutation().addMethod(svc, rpc, in, out)
}
}
}
}
return nil
}
type SchemaDescriptorList []*SchemaDescriptor
func (s SchemaDescriptorList) AsGraphql() (astSchema []*ast.Schema) {
for _, ss := range s {
astSchema = append(astSchema, ss.AsGraphql())
}
return
}
func (s SchemaDescriptorList) GetForDescriptor(file *protogen.File) *SchemaDescriptor {
for _, schema := range s {
for _, d := range schema.FileDescriptors {
if d.AsFileDescriptorProto() == file.Proto {
return schema
}
}
}
return nil
}
func NewSchemaDescriptor(genServiceDesc bool, goref GoRef) *SchemaDescriptor {
sd := &SchemaDescriptor{
Directives: map[string]*ast.DirectiveDefinition{},
reservedNames: map[string]desc.Descriptor{},
createdObjects: map[createdObjectKey]*ObjectDescriptor{},
generateServiceDescriptors: genServiceDesc,
goRef: goref,
}
for _, name := range graphqlReservedNames {
sd.reservedNames[name] = nil
}
return sd
}
type SchemaDescriptor struct {
Directives map[string]*ast.DirectiveDefinition
FileDescriptors []*desc.FileDescriptor
files []*desc.FileDescriptor
query *RootDefinition
mutation *RootDefinition
subscription *RootDefinition
objects []*ObjectDescriptor
reservedNames map[string]desc.Descriptor
createdObjects map[createdObjectKey]*ObjectDescriptor
generateServiceDescriptors bool
goRef GoRef
}
type createdObjectKey struct {
desc desc.Descriptor
input bool
}
func (s *SchemaDescriptor) AsGraphql() *ast.Schema {
queryDef := *s.GetQuery().Definition
mutationDef := *s.GetMutation().Definition
subscriptionsDef := *s.GetSubscription().Definition
schema := &ast.Schema{Types: map[string]*ast.Definition{}, Directives: s.Directives}
schema.Query = &queryDef
schema.Types["Query"] = &queryDef
if s.query.methods == nil {
schema.Query.Fields = append(schema.Query.Fields, &ast.FieldDefinition{
Name: "dummy",
Type: ast.NamedType("Boolean", &ast.Position{}),
})
}
if s.mutation.methods != nil {
schema.Mutation = &mutationDef
schema.Types["Mutation"] = &mutationDef
}
if s.subscription.methods != nil {
schema.Subscription = &subscriptionsDef
schema.Types["Subscription"] = &subscriptionsDef
}
for _, o := range s.objects {
def := o.AsGraphql()
schema.Types[def.Name] = def
}
return schema
}
func (s *SchemaDescriptor) Objects() []*ObjectDescriptor {
return s.objects
}
func (s *SchemaDescriptor) GetMutation() *RootDefinition {
if s.mutation == nil {
s.mutation = NewRootDefinition(Mutation, s)
}
return s.mutation
}
func (s *SchemaDescriptor) GetSubscription() *RootDefinition {
if s.subscription == nil {
s.subscription = NewRootDefinition(Subscription, s)
}
return s.subscription
}
func (s *SchemaDescriptor) GetQuery() *RootDefinition {
if s.query == nil {
s.query = NewRootDefinition(Query, s)
}
return s.query
}
// make name be unique
// just create a map and register every name
func (s *SchemaDescriptor) uniqueName(d desc.Descriptor, input bool) (name string) {
var collisionPrefix string
var suffix string
if _, ok := d.(*desc.MessageDescriptor); input && ok {
suffix = inputSuffix
}
name = strings.Title(CamelCaseSlice(strings.Split(strings.TrimPrefix(d.GetFullyQualifiedName(), d.GetFile().GetPackage()+packageSep), packageSep)) + suffix)
if _, ok := d.(*desc.FieldDescriptor); ok {
collisionPrefix = fieldPrefix
name = CamelCaseSlice(strings.Split(strings.Trim(d.GetParent().GetName()+packageSep+strings.Title(d.GetName()), packageSep), packageSep))
} else {
collisionPrefix = CamelCaseSlice(strings.Split(d.GetFile().GetPackage(), packageSep))
}
originalName := name
for uniqueSuffix := 0; ; uniqueSuffix++ {
d2, ok := s.reservedNames[name]
if !ok {
break
}
if d2 == d {
return name
}
if uniqueSuffix == 0 {
name = collisionPrefix + typeSep + originalName
continue
}
name = collisionPrefix + typeSep + originalName + strconv.Itoa(uniqueSuffix)
}
s.reservedNames[name] = d
return
}
func (s *SchemaDescriptor) CreateObjects(d desc.Descriptor, input bool) (obj *ObjectDescriptor, err error) {
// the case if trying to resolve a primitive as a object. In this case we just return nil
if d == nil {
return
}
if obj, ok := s.createdObjects[createdObjectKey{d, input}]; ok {
return obj, nil
}
obj = &ObjectDescriptor{
Definition: &ast.Definition{
Description: getDescription(d),
Name: s.uniqueName(d, input),
Position: &ast.Position{},
},
Descriptor: d,
}
s.createdObjects[createdObjectKey{d, input}] = obj
switch dd := d.(type) {
case *desc.MessageDescriptor:
if IsEmpty(dd) {
return obj, nil
}
if IsAny(dd) {
// TODO find a better way to handle any types
delete(s.createdObjects, createdObjectKey{d, input})
any := s.createScalar(s.uniqueName(dd, false), anyTypeDescription)
return any, nil
}
kind := ast.Object
if input {
kind = ast.InputObject
}
fields := FieldDescriptorList{}
outputOneofRegistrar := map[*desc.OneOfDescriptor]struct{}{}
for _, df := range dd.GetFields() {
fieldOpts := GraphqlFieldOptions(df.AsFieldDescriptorProto().GetOptions())
if fieldOpts != nil && fieldOpts.Ignore != nil && *fieldOpts.Ignore {
continue
}
var fieldDirective []*ast.Directive
if df.GetType() == descriptor.FieldDescriptorProto_TYPE_MESSAGE && IsEmpty(df.GetMessageType()) {
continue
}
// Internally `optional` fields are represented as a oneof, and as such should be skipped.
if oneof := df.GetOneOf(); oneof != nil && !df.AsFieldDescriptorProto().GetProto3Optional() {
opts := GraphqlOneofOptions(oneof.AsOneofDescriptorProto().GetOptions())
if opts.GetIgnore() {
continue
}
if !input {
if _, ok := outputOneofRegistrar[oneof]; ok {
continue
}
outputOneofRegistrar[oneof] = struct{}{}
field, err := s.createUnion(oneof)
if err != nil {
return nil, err
}
fields = append(fields, field)
continue
}
// create oneofs as directives for input objects
directive := &ast.DirectiveDefinition{
Description: getDescription(oneof),
Name: s.uniqueName(oneof, input),
Locations: []ast.DirectiveLocation{ast.LocationInputFieldDefinition},
Position: &ast.Position{Src: &ast.Source{}},
}
s.Directives[directive.Name] = directive
fieldDirective = append(fieldDirective, &ast.Directive{
Name: directive.Name,
Position: &ast.Position{Src: &ast.Source{}},
// ParentDefinition: obj.Definition, TODO
Definition: directive,
Location: ast.LocationInputFieldDefinition,
})
}
fieldObj, err := s.CreateObjects(resolveFieldType(df), input)
if err != nil {
return nil, err
}
if fieldObj == nil && df.GetMessageType() != nil {
continue
}
f, err := s.createField(df, fieldObj)
if err != nil {
return nil, err
}
f.Directives = append(f.Directives, fieldDirective...)
fields = append(fields, f)
}
obj.Definition.Fields = fields.AsGraphql()
obj.Definition.Kind = kind
obj.fields = fields
case *desc.EnumDescriptor:
obj.Definition.Kind = ast.Enum
obj.Definition.EnumValues = enumValues(dd.GetValues())
default:
panic(fmt.Sprintf("received unexpected value %v of type %T", dd, dd))
}
s.objects = append(s.objects, obj)
return obj, nil
}
func resolveFieldType(field *desc.FieldDescriptor) desc.Descriptor {
msgType := field.GetMessageType()
enumType := field.GetEnumType()
if msgType != nil {
return msgType
}
if enumType != nil {
return enumType
}
return nil
}
func enumValues(evals []*desc.EnumValueDescriptor) (vlist ast.EnumValueList) {
for _, eval := range evals {
vlist = append(vlist, &ast.EnumValueDefinition{
Description: getDescription(eval),
Name: eval.GetName(),
Position: &ast.Position{},
})
}
return vlist
}
type FieldDescriptorList []*FieldDescriptor
func (fl FieldDescriptorList) AsGraphql() (dl []*ast.FieldDefinition) {
for _, f := range fl {
dl = append(dl, f.FieldDefinition)
}
return dl
}
type FieldDescriptor struct {
*ast.FieldDefinition
*desc.FieldDescriptor
typ *ObjectDescriptor
}
func (f *FieldDescriptor) GetType() *ObjectDescriptor {
return f.typ
}
type MethodDescriptor struct {
*desc.ServiceDescriptor
*desc.MethodDescriptor
*ast.FieldDefinition
input *ObjectDescriptor
output *ObjectDescriptor
}
func (m *MethodDescriptor) AsGraphql() *ast.FieldDefinition {
return m.FieldDefinition
}
func (m *MethodDescriptor) GetInput() *ObjectDescriptor {
return m.input
}
func (m *MethodDescriptor) GetOutput() *ObjectDescriptor {
return m.output
}
type RootDefinition struct {
*ast.Definition
Parent *SchemaDescriptor
methods []*MethodDescriptor
reservedNames map[string]ServiceAndMethod
}
type ServiceAndMethod struct {
svc *descriptor.ServiceDescriptorProto
rpc *descriptor.MethodDescriptorProto
}
func (r *RootDefinition) UniqueName(svc *descriptor.ServiceDescriptorProto, rpc *descriptor.MethodDescriptorProto) (name string) {
rpcOpts := GraphqlMethodOptions(rpc.GetOptions())
svcOpts := GraphqlServiceOptions(svc.GetOptions())
if rpcOpts != nil && rpcOpts.Name != nil {
name = *rpcOpts.Name
} else if svcOpts != nil && svcOpts.Name != nil {
if *svcOpts.Name == "" {
name = ToLowerFirst(rpc.GetName())
} else {
name = *svcOpts.Name + strings.Title(rpc.GetName())
}
} else {
name = ToLowerFirst(svc.GetName()) + strings.Title(rpc.GetName())
}
originalName := name
for uniqueSuffix := 0; ; uniqueSuffix++ {
snm, ok := r.reservedNames[name]
if !ok {
break
}
if svc == snm.svc && snm.rpc == rpc {
return name
}
name = originalName + strconv.Itoa(uniqueSuffix)
}
r.reservedNames[name] = ServiceAndMethod{svc, rpc}
return
}
func (r *RootDefinition) Methods() []*MethodDescriptor {
return r.methods
}
func (r *RootDefinition) addMethod(svc *desc.ServiceDescriptor, rpc *desc.MethodDescriptor, in, out *ObjectDescriptor) {
var args ast.ArgumentDefinitionList
if in != nil && (in.Descriptor != nil && !IsEmpty(in.Descriptor.(*desc.MessageDescriptor)) || in.Definition.Kind == ast.Scalar) {
args = append(args, &ast.ArgumentDefinition{
Name: "in",
Type: ast.NamedType(in.Name, &ast.Position{}),
Position: &ast.Position{},
})
}
objType := ast.NamedType("Boolean", &ast.Position{})
if out != nil && (out.Descriptor != nil && !IsEmpty(out.Descriptor.(*desc.MessageDescriptor)) || in.Definition.Kind == ast.Scalar) {
objType = ast.NamedType(out.Name, &ast.Position{})
}
svcDir := &ast.DirectiveDefinition{
Description: getDescription(svc),
Name: svc.GetName(),
Locations: []ast.DirectiveLocation{ast.LocationFieldDefinition},
Position: &ast.Position{Src: &ast.Source{}},
}
r.Parent.Directives[svcDir.Name] = svcDir
m := &MethodDescriptor{
ServiceDescriptor: svc,
MethodDescriptor: rpc,
FieldDefinition: &ast.FieldDefinition{
Description: getDescription(rpc),
Name: r.UniqueName(svc.AsServiceDescriptorProto(), rpc.AsMethodDescriptorProto()),
Arguments: args,
Type: objType,
Position: &ast.Position{},
},
input: in,
output: out,
}
if r.Parent.generateServiceDescriptors {
m.Directives = []*ast.Directive{{
Name: svcDir.Name,
Position: &ast.Position{},
Definition: svcDir,
Location: svcDir.Locations[0],
}}
}
r.methods = append(r.methods, m)
// TODO maybe not do it here?
r.Definition.Fields = append(r.Definition.Fields, m.FieldDefinition)
}
type rootName string
const (
Mutation rootName = "Mutation"
Query rootName = "Query"
Subscription rootName = "Subscription"
)
func NewRootDefinition(name rootName, parent *SchemaDescriptor) *RootDefinition {
return &RootDefinition{Definition: &ast.Definition{
Kind: ast.Object,
Name: string(name),
Position: &ast.Position{},
}, Parent: parent, reservedNames: map[string]ServiceAndMethod{}}
}
func getDescription(descs ...desc.Descriptor) string {
var description []string
for _, d := range descs {
info := d.GetSourceInfo()
if info == nil {
continue
}
if info.LeadingComments != nil {
description = append(description, *info.LeadingComments)
}
if info.TrailingComments != nil {
description = append(description, *info.TrailingComments)
}
}
return strings.Join(description, "\n")
}
func (s *SchemaDescriptor) createField(field *desc.FieldDescriptor, obj *ObjectDescriptor) (_ *FieldDescriptor, err error) {
fieldAst := &ast.FieldDefinition{
Description: getDescription(field),
Name: ToLowerFirst(CamelCase(field.GetName())),
Type: &ast.Type{Position: &ast.Position{}},
Position: &ast.Position{},
}
fieldOpts := GraphqlFieldOptions(field.AsFieldDescriptorProto().GetOptions())
if fieldOpts != nil && fieldOpts.Name != nil {
fieldAst.Name = *fieldOpts.Name
directive := &ast.DirectiveDefinition{
Name: goFieldDirective,
Arguments: []*ast.ArgumentDefinition{{
Name: "forceResolver",
Type: ast.NamedType("Boolean", &ast.Position{}),
Position: &ast.Position{},
}, {
Name: "name",
Type: ast.NamedType("String", &ast.Position{}),
Position: &ast.Position{},
}},
Locations: []ast.DirectiveLocation{ast.LocationInputFieldDefinition, ast.LocationFieldDefinition},
Position: &ast.Position{Src: &ast.Source{}},
}
s.Directives[directive.Name] = directive
if s.goRef != nil {
fieldAst.Directives = []*ast.Directive{{
Name: directive.Name,
Arguments: []*ast.Argument{{
Name: "name",
Value: &ast.Value{
Raw: s.goRef.FindGoField(field.GetFullyQualifiedName()).GoName,
Kind: ast.StringValue,
Position: &ast.Position{},
},
Position: &ast.Position{},
}},
Position: &ast.Position{},
// ParentDefinition: nil, TODO
Definition: directive,
}}
}
}
switch field.GetType() {
case descriptor.FieldDescriptorProto_TYPE_DOUBLE,
descriptor.FieldDescriptorProto_TYPE_FLOAT:
fieldAst.Type.NamedType = ScalarFloat
case descriptor.FieldDescriptorProto_TYPE_BYTES:
scalar := s.createScalar(scalarBytes, "")
fieldAst.Type.NamedType = scalar.Name
case descriptor.FieldDescriptorProto_TYPE_INT64,
descriptor.FieldDescriptorProto_TYPE_SINT64,
descriptor.FieldDescriptorProto_TYPE_SFIXED64,
descriptor.FieldDescriptorProto_TYPE_INT32,
descriptor.FieldDescriptorProto_TYPE_SINT32,
descriptor.FieldDescriptorProto_TYPE_SFIXED32,
descriptor.FieldDescriptorProto_TYPE_UINT32,
descriptor.FieldDescriptorProto_TYPE_FIXED32,
descriptor.FieldDescriptorProto_TYPE_UINT64,
descriptor.FieldDescriptorProto_TYPE_FIXED64:
fieldAst.Type.NamedType = ScalarInt
case descriptor.FieldDescriptorProto_TYPE_BOOL:
fieldAst.Type.NamedType = ScalarBoolean
case descriptor.FieldDescriptorProto_TYPE_STRING:
fieldAst.Type.NamedType = ScalarString
case descriptor.FieldDescriptorProto_TYPE_GROUP:
return nil, fmt.Errorf("proto2 groups are not supported please use proto3 syntax")
case descriptor.FieldDescriptorProto_TYPE_ENUM:
fieldAst.Type.NamedType = obj.Name
case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
fieldAst.Type.NamedType = obj.Name
default:
panic("unknown proto field type")
}
if isRepeated(field) {
fieldAst.Type = ast.ListType(fieldAst.Type, &ast.Position{})
fieldAst.Type.Elem.NonNull = true
}
if isRequired(field) {
fieldAst.Type.NonNull = true
}
return &FieldDescriptor{
FieldDefinition: fieldAst,
FieldDescriptor: field,
typ: obj,
}, nil
}
func (s *SchemaDescriptor) createScalar(name string, description string) *ObjectDescriptor {
obj := &ObjectDescriptor{
Definition: &ast.Definition{
Kind: ast.Scalar,
Description: description,
Name: name,
Position: &ast.Position{},
},
}
s.objects = append(s.objects, obj)
return obj
}
func (s *SchemaDescriptor) createUnion(oneof *desc.OneOfDescriptor) (*FieldDescriptor, error) {
var types []string
var objTypes []*ObjectDescriptor
for _, choice := range oneof.GetChoices() {
obj, err := s.CreateObjects(resolveFieldType(choice), false)
if err != nil {
return nil, err
}
f, err := s.createField(choice, obj)
if err != nil {
return nil, err
}
obj = &ObjectDescriptor{
Definition: &ast.Definition{
Kind: ast.Object,
Description: getDescription(f),
Name: s.uniqueName(choice, false),
Fields: ast.FieldList{f.FieldDefinition},
Position: &ast.Position{},
},
Descriptor: f,
fields: []*FieldDescriptor{f},
fieldNames: map[string]*FieldDescriptor{},
}
s.objects = append(s.objects, obj)
types = append(types, obj.Name)
objTypes = append(objTypes, obj)
}
obj := &ObjectDescriptor{
Definition: &ast.Definition{
Kind: ast.Union,
Description: getDescription(oneof),
Name: s.uniqueName(oneof, false),
Types: types,
Position: &ast.Position{},
},
Descriptor: oneof,
types: objTypes,
}
s.objects = append(s.objects, obj)
name := ToLowerFirst(CamelCase(oneof.GetName()))
opts := GraphqlOneofOptions(oneof.AsOneofDescriptorProto().GetOptions())
if opts.GetName() != "" {
name = opts.GetName()
}
return &FieldDescriptor{
FieldDefinition: &ast.FieldDefinition{
Description: getDescription(oneof),
Name: name,
Type: ast.NamedType(obj.Name, &ast.Position{}),
Position: &ast.Position{},
},
FieldDescriptor: nil,
typ: obj,
}, nil
}
func isRepeated(field *desc.FieldDescriptor) bool {
return field.GetLabel() == descriptor.FieldDescriptorProto_LABEL_REPEATED
}
func isRequired(field *desc.FieldDescriptor) bool {
if v := GraphqlFieldOptions(field.AsFieldDescriptorProto().GetOptions()); v != nil {
return v.GetRequired()
}
return false
}
const (
ScalarInt = "Int"
ScalarFloat = "Float"
ScalarString = "String"
ScalarBoolean = "Boolean"
ScalarID = "ID"
)
var graphqlReservedNames = []string{"__Directive", "__Type", "__Field", "__EnumValue", "__InputValue", "__Schema", "Int", "Float", "String", "Boolean", "ID"}

View File

@ -1,30 +0,0 @@
package generator
import (
"google.golang.org/protobuf/compiler/protogen"
)
type GoRef interface {
FindGoField(field string) *protogen.Field
}
func NewGoRef(p *protogen.Plugin) (GoRef, error) {
return goRef{p}, nil
}
type goRef struct {
*protogen.Plugin
}
func (g goRef) FindGoField(field string) *protogen.Field {
for _, file := range g.Files {
for _, msg := range file.Messages {
for _, f := range msg.Fields {
if string(f.Desc.FullName()) == field {
return f
}
}
}
}
return nil
}

7111
graphql/l

File diff suppressed because it is too large Load Diff

View File

@ -1,139 +0,0 @@
package generator
import (
"sync"
"github.com/jhump/protoreflect/desc"
"github.com/vektah/gqlparser/v2/ast"
)
type Registry interface {
FindMethodByName(op ast.Operation, name string) *desc.MethodDescriptor
FindObjectByName(name string) *desc.MessageDescriptor
// FindObjectByFullyQualifiedName TODO maybe find a better way to get ast definition
FindObjectByFullyQualifiedName(fqn string) (*desc.MessageDescriptor, *ast.Definition)
// FindFieldByName given the proto Descriptor and the graphql field name get the the proto FieldDescriptor
FindFieldByName(msg desc.Descriptor, name string) *desc.FieldDescriptor
// FindUnionFieldByMessageFQNAndName given the proto Descriptor and the graphql field name get the the proto FieldDescriptor
FindUnionFieldByMessageFQNAndName(fqn, name string) *desc.FieldDescriptor
FindGraphqlFieldByProtoField(msg *ast.Definition, name string) *ast.FieldDefinition
}
func NewRegistry(files SchemaDescriptorList) Registry {
v := &repository{
mu: &sync.RWMutex{},
methodsByName: map[ast.Operation]map[string]*desc.MethodDescriptor{},
objectsByName: map[string]*desc.MessageDescriptor{},
objectsByFQN: map[string]*ObjectDescriptor{},
graphqlFieldsByName: map[desc.Descriptor]map[string]*desc.FieldDescriptor{},
graphqlUnionFieldsByName: map[string]map[string]*desc.FieldDescriptor{},
protoFieldsByName: map[*ast.Definition]map[string]*ast.FieldDefinition{},
}
for _, f := range files {
v.methodsByName[ast.Mutation] = map[string]*desc.MethodDescriptor{}
for _, m := range f.GetMutation().Methods() {
v.methodsByName[ast.Mutation][m.Name] = m.MethodDescriptor
}
v.methodsByName[ast.Query] = map[string]*desc.MethodDescriptor{}
for _, m := range f.GetQuery().Methods() {
v.methodsByName[ast.Query][m.Name] = m.MethodDescriptor
}
v.methodsByName[ast.Subscription] = map[string]*desc.MethodDescriptor{}
for _, m := range f.GetSubscription().Methods() {
v.methodsByName[ast.Subscription][m.Name] = m.MethodDescriptor
}
}
for _, f := range files {
for _, m := range f.Objects() {
switch m.Kind {
case ast.Union:
fqn := m.Descriptor.GetParent().GetFullyQualifiedName()
if _, ok := v.graphqlUnionFieldsByName[fqn]; !ok {
v.graphqlUnionFieldsByName[fqn] = map[string]*desc.FieldDescriptor{}
}
for _, tt := range m.GetTypes() {
for _, f := range tt.GetFields() {
v.graphqlUnionFieldsByName[fqn][f.Name] = f.FieldDescriptor
}
}
case ast.Object:
v.protoFieldsByName[m.Definition] = map[string]*ast.FieldDefinition{}
for _, f := range m.GetFields() {
if f.FieldDescriptor != nil {
v.protoFieldsByName[m.Definition][f.FieldDescriptor.GetName()] = f.FieldDefinition
}
}
case ast.InputObject:
v.graphqlFieldsByName[m.Descriptor] = map[string]*desc.FieldDescriptor{}
for _, f := range m.GetFields() {
v.graphqlFieldsByName[m.Descriptor][f.Name] = f.FieldDescriptor
}
}
switch msgDesc := m.Descriptor.(type) {
case *desc.MessageDescriptor:
v.objectsByFQN[m.GetFullyQualifiedName()] = m
v.objectsByName[m.Name] = msgDesc
}
}
}
return v
}
type repository struct {
files SchemaDescriptorList
mu *sync.RWMutex
methodsByName map[ast.Operation]map[string]*desc.MethodDescriptor
objectsByName map[string]*desc.MessageDescriptor
objectsByFQN map[string]*ObjectDescriptor
graphqlFieldsByName map[desc.Descriptor]map[string]*desc.FieldDescriptor
protoFieldsByName map[*ast.Definition]map[string]*ast.FieldDefinition
graphqlUnionFieldsByName map[string]map[string]*desc.FieldDescriptor
}
func (r repository) FindMethodByName(op ast.Operation, name string) *desc.MethodDescriptor {
r.mu.RLock()
defer r.mu.RUnlock()
m, _ := r.methodsByName[op][name]
return m
}
func (r repository) FindObjectByName(name string) *desc.MessageDescriptor {
r.mu.RLock()
defer r.mu.RUnlock()
o, _ := r.objectsByName[name]
return o
}
func (r repository) FindObjectByFullyQualifiedName(fqn string) (*desc.MessageDescriptor, *ast.Definition) {
r.mu.RLock()
defer r.mu.RUnlock()
o, _ := r.objectsByFQN[fqn]
msg, _ := o.Descriptor.(*desc.MessageDescriptor)
return msg, o.Definition
}
func (r repository) FindFieldByName(msg desc.Descriptor, name string) *desc.FieldDescriptor {
r.mu.RLock()
defer r.mu.RUnlock()
f, _ := r.graphqlFieldsByName[msg][name]
return f
}
func (r repository) FindUnionFieldByMessageFQNAndName(fqn, name string) *desc.FieldDescriptor {
r.mu.RLock()
defer r.mu.RUnlock()
f, _ := r.graphqlUnionFieldsByName[fqn][name]
return f
}
func (r repository) FindGraphqlFieldByProtoField(msg *ast.Definition, name string) *ast.FieldDefinition {
r.mu.RLock()
defer r.mu.RUnlock()
f, _ := r.protoFieldsByName[msg][name]
return f
}

View File

@ -1,291 +0,0 @@
package generator
import (
"strings"
"unicode"
"unicode/utf8"
"github.com/jhump/protoreflect/desc"
"google.golang.org/protobuf/proto"
descriptor "google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/pluginpb"
gqlpb "go.unistack.org/micro-proto/v3/graphql"
)
func GraphqlMethodOptions(opts proto.Message) *gqlpb.Rpc {
if opts != nil {
v := proto.GetExtension(opts, gqlpb.E_Rpc)
if v != nil {
return v.(*gqlpb.Rpc)
}
}
return nil
}
func GraphqlServiceOptions(opts proto.Message) *gqlpb.Svc {
if opts != nil {
v := proto.GetExtension(opts, gqlpb.E_Svc)
if v != nil {
return v.(*gqlpb.Svc)
}
}
return nil
}
func GraphqlFieldOptions(opts proto.Message) *gqlpb.Field {
if opts != nil {
v := proto.GetExtension(opts, gqlpb.E_Field)
if v != nil && v.(*gqlpb.Field) != nil {
return v.(*gqlpb.Field)
}
}
return nil
}
func GraphqlOneofOptions(opts proto.Message) *gqlpb.Oneof {
if opts != nil {
v := proto.GetExtension(opts, gqlpb.E_Oneof)
if v != nil && v.(*gqlpb.Oneof) != nil {
return v.(*gqlpb.Oneof)
}
}
return nil
}
// GoCamelCase camel-cases a protobuf name for use as a Go identifier.
//
// If there is an interior underscore followed by a lower case letter,
// drop the underscore and convert the letter to upper case.
func GoCamelCase(s string) string {
// Invariant: if the next letter is lower case, it must be converted
// to upper case.
// That is, we process a word at a time, where words are marked by _ or
// upper case letter. Digits are treated as words.
var b []byte
for i := 0; i < len(s); i++ {
c := s[i]
switch {
case c == '.' && i+1 < len(s) && isASCIILower(s[i+1]):
// Skip over '.' in ".{{lowercase}}".
case c == '.':
b = append(b, '_') // convert '.' to '_'
case c == '_' && (i == 0 || s[i-1] == '.'):
// Convert initial '_' to ensure we start with a capital letter.
// Do the same for '_' after '.' to match historic behavior.
b = append(b, 'X') // convert '_' to 'X'
case c == '_' && i+1 < len(s) && isASCIILower(s[i+1]):
// Skip over '_' in "_{{lowercase}}".
case isASCIIDigit(c):
b = append(b, c)
default:
// Assume we have a letter now - if not, it's a bogus identifier.
// The next word is a sequence of characters that must start upper case.
if isASCIILower(c) {
c -= 'a' - 'A' // convert lowercase to uppercase
}
b = append(b, c)
// Accept lower case sequence that follows.
for ; i+1 < len(s) && isASCIILower(s[i+1]); i++ {
b = append(b, s[i+1])
}
}
}
return string(b)
}
func GetRequestType(rpcOpts *gqlpb.Rpc, svcOpts *gqlpb.Svc) gqlpb.Type {
if rpcOpts != nil && rpcOpts.Type != nil {
return *rpcOpts.Type
}
if svcOpts != nil && svcOpts.Type != nil {
return *svcOpts.Type
}
return gqlpb.Type_DEFAULT
}
func CreateDescriptorsFromProto(req *pluginpb.CodeGeneratorRequest) (descs []*desc.FileDescriptor, err error) {
dd, err := desc.CreateFileDescriptorsFromSet(&descriptor.FileDescriptorSet{
File: req.GetProtoFile(),
})
if err != nil {
return nil, err
}
for _, d := range dd {
for _, filename := range req.FileToGenerate {
if filename == d.GetName() {
descs = append(descs, d)
}
}
}
return
}
func ResolveProtoFilesRecursively(descs []*desc.FileDescriptor) (files FileDescriptors) {
for _, f := range descs {
files = append(files, ResolveProtoFilesRecursively(f.GetDependencies())...)
files = append(files, f)
}
return files
}
type FileDescriptors []*desc.FileDescriptor
func (ds FileDescriptors) AsFileDescriptorProto() (files []*descriptor.FileDescriptorProto) {
for _, d := range ds {
files = append(files, d.AsFileDescriptorProto())
}
return
}
// Split splits the camelcase word and returns a list of words. It also
// supports digits. Both lower camel case and upper camel case are supported.
// For more info please check: http://en.wikipedia.org/wiki/CamelCase
//
// Examples
//
// "" => [""]
// "lowercase" => ["lowercase"]
// "Class" => ["Class"]
// "MyClass" => ["My", "Class"]
// "MyC" => ["My", "C"]
// "HTML" => ["HTML"]
// "PDFLoader" => ["PDF", "Loader"]
// "AString" => ["A", "String"]
// "SimpleXMLParser" => ["Simple", "XML", "Parser"]
// "vimRPCPlugin" => ["vim", "RPC", "Plugin"]
// "GL11Version" => ["GL", "11", "Version"]
// "99Bottles" => ["99", "Bottles"]
// "May5" => ["May", "5"]
// "BFG9000" => ["BFG", "9000"]
// "BöseÜberraschung" => ["Böse", "Überraschung"]
// "Two spaces" => ["Two", " ", "spaces"]
// "BadUTF8\xe2\xe2\xa1" => ["BadUTF8\xe2\xe2\xa1"]
//
// Splitting rules
//
// 1. If string is not valid UTF-8, return it without splitting as
// single item array.
// 2. Assign all unicode characters into one of 4 sets: lower case
// letters, upper case letters, numbers, and all other characters.
// 3. Iterate through characters of string, introducing splits
// between adjacent characters that belong to different sets.
// 4. Iterate through array of split strings, and if a given string
// is upper case:
// if subsequent string is lower case:
// move last character of upper case string to beginning of
// lower case string
func SplitCamelCase(src string) (entries []string) {
// don't split invalid utf8
if !utf8.ValidString(src) {
return []string{src}
}
entries = []string{}
var runes [][]rune
lastClass := 0
class := 0
// split into fields based on class of unicode character
for _, r := range src {
switch true {
case unicode.IsLower(r):
class = 1
case unicode.IsUpper(r):
class = 2
case unicode.IsDigit(r):
class = 3
default:
class = 4
}
if class == lastClass {
runes[len(runes)-1] = append(runes[len(runes)-1], r)
} else {
runes = append(runes, []rune{r})
}
lastClass = class
}
// handle upper case -> lower case sequences, e.g.
// "PDFL", "oader" -> "PDF", "Loader"
for i := 0; i < len(runes)-1; i++ {
if unicode.IsUpper(runes[i][0]) && unicode.IsLower(runes[i+1][0]) {
runes[i+1] = append([]rune{runes[i][len(runes[i])-1]}, runes[i+1]...)
runes[i] = runes[i][:len(runes[i])-1]
}
}
// construct []string from results
for _, s := range runes {
if len(s) > 0 {
entries = append(entries, string(s))
}
}
return
}
// CamelCase returns the CamelCased name.
// If there is an interior underscore followed by a lower case letter,
// drop the underscore and convert the letter to upper case.
// There is a remote possibility of this rewrite causing a name collision,
// but it's so remote we're prepared to pretend it's nonexistent - since the
// C++ generator lowercases names, it's extremely unlikely to have two fields
// with different capitalizations.
// In short, _my_field_name_2 becomes XMyFieldName_2.
func CamelCase(s string) string {
if s == "" {
return ""
}
t := make([]byte, 0, 32)
i := 0
if s[0] == '_' {
// Need a capital letter; drop the '_'.
t = append(t, 'X')
i++
}
// Invariant: if the next letter is lower case, it must be converted
// to upper case.
// That is, we process a word at a time, where words are marked by _ or
// upper case letter. Digits are treated as words.
for ; i < len(s); i++ {
c := s[i]
if c == '_' && i+1 < len(s) && isASCIILower(s[i+1]) {
continue // Skip the underscore in s.
}
if isASCIIDigit(c) {
t = append(t, c)
continue
}
// Assume we have a letter now - if not, it's a bogus identifier.
// The next word is a sequence of characters that must start upper case.
if isASCIILower(c) {
c ^= ' ' // Make it a capital letter.
}
t = append(t, c) // Guaranteed not lower case.
// Accept lower case sequence that follows.
for i+1 < len(s) && isASCIILower(s[i+1]) {
i++
t = append(t, s[i])
}
}
return string(t)
}
// CamelCaseSlice is like CamelCase, but the argument is a slice of strings to
// be joined with "_".
func CamelCaseSlice(elem []string) string { return CamelCase(strings.Join(elem, "_")) }
// Is c an ASCII lower-case letter?
func isASCIILower(c byte) bool {
return 'a' <= c && c <= 'z'
}
// Is c an ASCII digit?
func isASCIIDigit(c byte) bool {
return '0' <= c && c <= '9'
}
func ToLowerFirst(s string) string {
if len(s) > 0 {
return string(unicode.ToLower(rune(s[0]))) + s[1:]
}
return ""
}

View File

@ -1,56 +0,0 @@
package main
import (
"bytes"
"os"
"github.com/vektah/gqlparser/v2/formatter"
generator "go.unistack.org/protoc-gen-go-micro/v3/graphql"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/pluginpb"
)
func (g *Generator) graphqlsGenerate(plugin *protogen.Plugin) error {
descs, err := generator.CreateDescriptorsFromProto(plugin.Request)
if err != nil {
return err
}
gqlDesc, err := generator.NewSchemas(descs, false, false, plugin)
if err != nil {
return err
}
var outFiles []*pluginpb.CodeGeneratorResponse_File
for _, schema := range gqlDesc {
buf := &bytes.Buffer{}
formatter.NewFormatter(buf).FormatSchema(schema.AsGraphql())
outFiles = append(outFiles, &pluginpb.CodeGeneratorResponse_File{
Name: proto.String(g.graphqlFile),
Content: proto.String(buf.String()),
})
}
res := &pluginpb.CodeGeneratorResponse{
File: outFiles,
SupportedFeatures: proto.Uint64(uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)),
}
if err != nil {
res.Error = proto.String(err.Error())
}
out, err := proto.Marshal(res)
if err != nil {
return err
}
if _, err := os.Stdout.Write(out); err != nil {
return err
}
return nil
}

1564
helpers/helpers.go Normal file

File diff suppressed because it is too large Load Diff

55
http.go
View File

@ -1,55 +0,0 @@
package main
import (
"google.golang.org/protobuf/compiler/protogen"
)
func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin, genClient bool, genServer bool) error {
for _, file := range plugin.Files {
if !file.Generate {
continue
}
if len(file.Services) == 0 {
continue
}
gname := file.GeneratedFilenamePrefix + "_micro_" + component + ".pb.go"
path := file.GoImportPath
if g.standalone {
path = "."
}
gfile := plugin.NewGeneratedFile(gname, path)
gfile.P("// Code generated by protoc-gen-go-micro. DO NOT EDIT.")
gfile.P("// protoc-gen-go-micro version: " + versionComment)
gfile.P("// source: ", file.Proto.GetName())
gfile.P()
gfile.P("package ", file.GoPackageName)
gfile.P()
gfile.Import(contextPackage)
if genClient {
gfile.Import(microClientPackage)
gfile.Import(microClientHttpPackage)
}
if genServer {
gfile.Import(microServerPackage)
}
for _, service := range file.Services {
g.generateServiceEndpoints(gfile, service, component)
if genClient {
g.generateServiceClient(gfile, file, service)
g.generateServiceClientMethods(gfile, file, service, component)
}
if genServer {
g.generateServiceServer(gfile, file, service)
g.generateServiceServerMethods(gfile, service)
g.generateServiceRegister(gfile, file, service, component)
}
}
}
return nil
}

294
main.go
View File

@ -1,137 +1,217 @@
package main
import (
"flag"
"fmt"
"go/format"
"io/ioutil"
"log"
"net/url"
"os"
"strings"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/types/pluginpb"
"github.com/golang/protobuf/protoc-gen-go/generator"
plugin_go "github.com/golang/protobuf/protoc-gen-go/plugin"
ggdescriptor "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
pgghelpers "github.com/unistack-org/protoc-gen-micro/v3/helpers"
"google.golang.org/protobuf/proto"
)
var (
flagSet = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
flagDebug = flagSet.Bool("debug", false, "debug output")
flagStandalone = flagSet.Bool("standalone", false, "generate file to standalone dir")
flagFieldaligment = flagSet.Bool("fieldaligment", false, "align struct fields in generated code")
flagComponents = flagSet.String("components", "micro|rpc|http|client|server|openapiv3|graphql", "specify components to generate")
flagTagPath = flagSet.String("tag_path", "", "tag rewriting dir")
flagOpenapiFile = flagSet.String("openapi_file", "apidocs.swagger.yaml", "openapi file name")
flagGraphqlFile = flagSet.String("graphql_file", "schema.graphqls", "graphql file name")
flagReflection = flagSet.Bool("reflection", false, "enable server reflection support")
flagHelp = flagSet.Bool("help", false, "display help message")
registry *ggdescriptor.Registry // some helpers need access to registry
)
const (
boolTrue = "true"
boolFalse = "false"
)
var (
templateDir = ""
templateRepo = ""
destinationDir = "."
debug = false
all = false
singlePackageMode = false
fileMode = false
components = []string{"micro", "grpc"}
)
func main() {
opts := &protogen.Options{
ParamFunc: flagSet.Set,
g := generator.New()
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
g.Error(err, "reading input")
}
_ = flagSet.Parse(os.Args[1:])
if *flagHelp {
flagSet.PrintDefaults()
return
if err = proto.Unmarshal(data, g.Request); err != nil {
g.Error(err, "parsing input proto")
}
g := &Generator{}
opts.Run(g.Generate)
}
type Generator struct {
components string
standalone bool
debug bool
fieldaligment bool
tagPath string
openapiFile string
graphqlFile string
reflection bool
plugin *protogen.Plugin
}
func (g *Generator) Generate(plugin *protogen.Plugin) error {
var err error
g.plugin = plugin
g.standalone = *flagStandalone
g.debug = *flagDebug
g.components = *flagComponents
g.fieldaligment = *flagFieldaligment
g.tagPath = *flagTagPath
g.openapiFile = *flagOpenapiFile
g.graphqlFile = *flagGraphqlFile
g.reflection = *flagReflection
plugin.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
genClient := true
genServer := true
var genNone bool
if strings.Contains(g.components, "server") {
genServer = true
}
if strings.Contains(g.components, "client") {
genClient = true
}
if strings.Contains(g.components, "none") {
genNone = true
}
if strings.Contains(g.components, "rpc") || strings.Contains(g.components, "http") {
if !genServer && !genClient && !genNone {
genServer = true
genClient = true
}
if len(g.Request.FileToGenerate) == 0 {
g.Fail("no files to generate")
}
// Protoc passes a slice of File structs for us to process
for _, component := range strings.Split(g.components, "|") {
switch component {
case "server", "client":
continue
case "micro":
err = g.microGenerate(component, plugin, genClient, genServer)
if err == nil {
err = g.writeErrors(plugin)
g.CommandLineParameters(g.Request.GetParameter())
// Parse parameters
if parameter := g.Request.GetParameter(); parameter != "" {
for _, param := range strings.Split(parameter, ",") {
parts := strings.Split(param, "=")
switch parts[0] {
case "template_dir":
templateDir = parts[1]
case "destination_dir":
destinationDir = parts[1]
case "single-package-mode":
switch strings.ToLower(parts[1]) {
case boolTrue, "t":
singlePackageMode = true
case boolFalse, "f":
default:
log.Printf("Err: invalid value for single-package-mode: %q", parts[1])
}
case "debug":
switch strings.ToLower(parts[1]) {
case boolTrue, "t":
debug = true
case boolFalse, "f":
default:
log.Printf("Err: invalid value for debug: %q", parts[1])
}
case "all":
switch strings.ToLower(parts[1]) {
case boolTrue, "t":
all = true
case boolFalse, "f":
default:
log.Printf("Err: invalid value for debug: %q", parts[1])
}
case "file-mode":
switch strings.ToLower(parts[1]) {
case boolTrue, "t":
fileMode = true
case boolFalse, "f":
default:
log.Printf("Err: invalid value for file-mode: %q", parts[1])
}
case "template_repo":
_, err := url.Parse(parts[1])
if err != nil {
log.Printf("Err: invalid value for template_repo: %q", parts[1])
}
templateRepo = parts[1]
case "components":
_, err := url.Parse(parts[1])
if err != nil {
log.Printf("Err: invalid value for components: %q", parts[1])
}
components = strings.Split(parts[1], "|")
case "paths":
// TODO: handle paths=source_relative
default:
log.Printf("Err: unknown parameter: %q", param)
}
case "http":
err = g.httpGenerate(component, plugin, genClient, genServer)
case "grpc", "drpc", "rpc":
err = g.rpcGenerate(component, plugin, genClient, genServer)
case "gorilla":
err = g.gorillaGenerate(component, plugin)
case "chi":
err = g.chiGenerate(component, plugin)
case "openapiv3":
err = g.openapiv3Generate(plugin)
case "graphqls":
err = g.graphqlsGenerate(plugin)
case "graphql":
err = g.graphqlGenerate(plugin)
case "none":
break
default:
err = fmt.Errorf("unknown component: %s", component)
}
}
tmplMap := make(map[string]*plugin_go.CodeGeneratorResponse_File)
concatOrAppend := func(file *plugin_go.CodeGeneratorResponse_File) {
if val, ok := tmplMap[file.GetName()]; ok {
*val.Content += file.GetContent()
} else {
tmplMap[file.GetName()] = file
g.Response.File = append(g.Response.File, file)
}
}
if singlePackageMode {
registry = ggdescriptor.NewRegistry()
pgghelpers.SetRegistry(registry)
if err = registry.Load(g.Request); err != nil {
g.Error(err, "registry: failed to load the request")
}
}
if templateDir == "" && templateRepo != "" {
if templateDir, err = ioutil.TempDir("", "gen-*"); err != nil {
g.Error(err, "failed to create tmp dir")
}
defer func() {
if err := os.RemoveAll(templateDir); err != nil {
g.Error(err, "failed to remove tmp dir")
}
}()
if templateRepo != "" {
if err = clone(templateRepo, templateDir); err != nil {
g.Error(err, "failed to clone repo")
}
}
}
filesToGenerate := map[string]struct{}{}
for _, file := range g.Request.FileToGenerate {
filesToGenerate[file] = struct{}{}
}
// Generate the encoders
for _, file := range g.Request.GetProtoFile() {
// Ignore files not in g.Request.FileToGenerate.
if _, ok := filesToGenerate[*file.Name]; !ok {
continue
}
if all {
if singlePackageMode {
if _, err = registry.LookupFile(file.GetName()); err != nil {
g.Error(err, "registry: failed to lookup file %q", file.GetName())
}
}
encoder := NewGenericTemplateBasedEncoder(templateDir, file, debug, destinationDir)
for _, tmpl := range encoder.Files() {
concatOrAppend(tmpl)
}
continue
}
if fileMode {
if s := file.GetService(); s != nil && len(s) > 0 {
encoder := NewGenericTemplateBasedEncoder(templateDir, file, debug, destinationDir)
for _, tmpl := range encoder.Files() {
concatOrAppend(tmpl)
}
}
continue
}
for _, service := range file.GetService() {
encoder := NewGenericServiceTemplateBasedEncoder(templateDir, service, file, debug, destinationDir)
for _, tmpl := range encoder.Files() {
concatOrAppend(tmpl)
}
}
}
// Generate the protobufs
g.GenerateAllFiles()
for _, f := range g.Response.File {
fdata, err := format.Source([]byte(*f.Content))
if err != nil {
plugin.Error(err)
return err
g.Error(err, fmt.Sprintf("failed to format output: %s", *f.Content))
}
*f.Content = string(fdata)
}
if err = g.astGenerate(plugin); err != nil {
plugin.Error(err)
return err
data, err = proto.Marshal(g.Response)
if err != nil {
g.Error(err, "failed to marshal output proto")
}
if err = g.fieldAlign(plugin); err != nil {
plugin.Error(err)
return err
_, err = os.Stdout.Write(data)
if err != nil {
g.Error(err, "failed to write output proto")
}
return nil
}

View File

@ -1,68 +0,0 @@
package main
import (
"fmt"
"google.golang.org/protobuf/compiler/protogen"
)
func (g *Generator) microGenerate(component string, plugin *protogen.Plugin, genClient bool, genServer bool) error {
for _, file := range plugin.Files {
if !file.Generate {
continue
}
if len(file.Services) == 0 {
continue
}
gname := file.GeneratedFilenamePrefix + "_" + component + ".pb.go"
path := file.GoImportPath
if g.standalone {
path = "."
}
gfile := plugin.NewGeneratedFile(gname, path)
gfile.P("// Code generated by protoc-gen-go-micro. DO NOT EDIT.")
gfile.P("// versions:")
gfile.P("// - protoc-gen-go-micro " + versionComment)
gfile.P("// - protoc ", protocVersion(plugin))
gfile.P("// source: ", file.Desc.Path())
gfile.P()
gfile.P("package ", file.GoPackageName)
gfile.P()
gfile.Import(contextPackage)
if genClient {
gfile.Import(microClientPackage)
}
// generate services
for _, service := range file.Services {
g.generateServiceName(gfile, service)
if genClient {
g.generateServiceClientInterface(gfile, service)
g.generateServiceClientStreamInterface(gfile, service)
}
if genServer {
g.generateServiceServerInterface(gfile, service)
g.generateServiceServerStreamInterface(gfile, service)
}
}
}
return nil
}
func protocVersion(plugin *protogen.Plugin) string {
v := plugin.Request.GetCompilerVersion()
if v == nil {
return "(unknown)"
}
var suffix string
if s := v.GetSuffix(); s != "" {
suffix = "-" + s
}
return fmt.Sprintf("v%d.%d.%d%s", v.GetMajor(), v.GetMinor(), v.GetPatch(), suffix)
}

File diff suppressed because it is too large Load Diff

View File

@ -1,39 +0,0 @@
package main
import v3 "go.unistack.org/micro-proto/v3/openapiv3"
func getMediaType(eopt interface{}) string {
ctype := "application/json"
if eopt == nil {
return ctype
}
if eopt == v3.E_Openapiv3Operation.InterfaceOf(v3.E_Openapiv3Operation.Zero()) {
return ctype
}
opt, ok := eopt.(*v3.Operation)
if !ok || opt.RequestBody == nil {
return ctype
}
if opt.GetRequestBody() == nil {
return ctype
}
if opt.GetRequestBody().GetRequestBody() == nil {
return ctype
}
c := opt.GetRequestBody().GetRequestBody().GetContent()
if c == nil {
return ctype
}
for _, prop := range c.GetAdditionalProperties() {
ctype = prop.Name
}
return ctype
}

55
rpc.go
View File

@ -1,55 +0,0 @@
package main
import (
"google.golang.org/protobuf/compiler/protogen"
)
func (g *Generator) rpcGenerate(component string, plugin *protogen.Plugin, genClient bool, genServer bool) error {
for _, file := range plugin.Files {
if !file.Generate {
continue
}
if len(file.Services) == 0 {
continue
}
gname := file.GeneratedFilenamePrefix + "_micro_" + component + ".pb.go"
path := file.GoImportPath
if g.standalone {
path = "."
}
gfile := plugin.NewGeneratedFile(gname, path)
gfile.P("// Code generated by protoc-gen-go-micro. DO NOT EDIT.")
gfile.P("// protoc-gen-go-micro version: " + versionComment)
gfile.P("// source: ", file.Proto.GetName())
gfile.P()
gfile.P("package ", file.GoPackageName)
gfile.P()
gfile.Import(contextPackage)
if genClient {
gfile.Import(microClientPackage)
}
if genServer {
gfile.Import(microServerPackage)
}
for _, service := range file.Services {
if genClient {
g.generateServiceClient(gfile, file, service)
g.generateServiceClientMethods(gfile, file, service, component)
}
if genServer {
g.generateServiceServer(gfile, file, service)
g.generateServiceServerMethods(gfile, service)
g.generateServiceRegister(gfile, file, service, component)
}
if component == "grpc" && g.reflection {
g.generateServiceDesc(gfile, file, service)
}
}
}
return nil
}

View File

@ -0,0 +1,51 @@
// Code generated by protoc-gen-micro
// source: {{.File.Name}}
package {{goPkgLastElement .File | splitArray ";" | last | replace "." "_"}}
import (
"context"
"fmt"
"net/http"
"reflect"
"strings"
"github.com/go-chi/chi/v4"
middleware "github.com/go-chi/chi/v4/middleware"
micro_api "github.com/unistack-org/micro/v3/api"
)
type routeKey struct{}
func RouteName(ctx context.Context) (string, bool) {
value, ok := ctx.Value(routeKey{}).(string)
return value, ok
}
func {{.Service.Name | trimSuffix "Service"}}ServiceRegister(r *chi.Mux, h interface{}, eps []*micro_api.Endpoint) error {
v := reflect.ValueOf(h)
if v.NumMethod() < 1 {
return fmt.Errorf("handler has no methods: %T", h)
}
for _, ep := range eps {
idx := strings.Index(ep.Name, ".")
if idx < 1 || len(ep.Name) <= idx {
return fmt.Errorf("invalid api.Endpoint name: %s", ep.Name)
}
name := ep.Name[idx+1:]
m := v.MethodByName(name)
if !m.IsValid() || m.IsZero() {
return fmt.Errorf("invalid handler, method %s not found", name)
}
rh, ok := m.Interface().(func(http.ResponseWriter, *http.Request))
if !ok {
return fmt.Errorf("invalid handler: %#+v", m.Interface())
}
for _, method := range ep.Method {
r.With(middleware.WithValue(routeKey{}, ep.Name)).MethodFunc(method, ep.Path[0], rh)
}
}
return nil
}

View File

@ -0,0 +1,40 @@
// Code generated by protoc-gen-micro
// source: {{.File.Name}}
package {{goPkgLastElement .File | splitArray ";" | last | replace "." "_"}}
import (
"fmt"
"net/http"
"reflect"
"strings"
"github.com/gorilla/mux"
micro_api "github.com/unistack-org/micro/v3/api"
)
func {{.Service.Name | trimSuffix "Service"}}ServiceRegister(r *mux.Router, h interface{}, eps []*micro_api.Endpoint) error {
v := reflect.ValueOf(h)
if v.NumMethod() < 1 {
return fmt.Errorf("handler has no methods: %T", h)
}
for _, ep := range eps {
idx := strings.Index(ep.Name, ".")
if idx < 1 || len(ep.Name) <= idx {
return fmt.Errorf("invalid api.Endpoint name: %s", ep.Name)
}
name := ep.Name[idx+1:]
m := v.MethodByName(name)
if !m.IsValid() || m.IsZero() {
return fmt.Errorf("invalid handler, method %s not found", name)
}
rh, ok := m.Interface().(func(http.ResponseWriter, *http.Request))
if !ok {
return fmt.Errorf("invalid handler: %#+v", m.Interface())
}
r.HandleFunc(ep.Path[0], rh).Methods(ep.Method...).Name(ep.Name)
}
return nil
}

View File

@ -0,0 +1,215 @@
// Code generated by protoc-gen-micro
// source: {{.File.Name}}
package {{goPkgLastElement .File | splitArray ";" | last | replace "." "_"}}
import (
"context"
micro_api "github.com/unistack-org/micro/v3/api"
micro_client "github.com/unistack-org/micro/v3/client"
micro_server "github.com/unistack-org/micro/v3/server"
)
var (
_ micro_server.Option
_ micro_client.Option
)
{{- $File := .File }}
{{- $ServiceName := .Service.Name | trimSuffix "Service" }}
type {{$ServiceName | lowerFirst}}Service struct {
c micro_client.Client
name string
}
// Micro client stuff
// New{{$ServiceName}}Service create new service client
func New{{$ServiceName}}Service(name string, c micro_client.Client) {{$ServiceName}}Service {
return &{{$ServiceName | lowerFirst}}Service{c: c, name: name}
}
{{range .Service.Method}}
{{- $reqMethod := .InputType | splitArray "." | last}}
{{- $rspMethod := .OutputType | splitArray "." | last}}
{{- if and (not .ClientStreaming) (.ServerStreaming) -}}
func (c *{{$ServiceName | lowerFirst}}Service) {{.Name}}(ctx context.Context, req *{{$reqMethod}}, opts ...micro_client.CallOption) ({{$ServiceName}}_{{.Name}}Service, error) {
{{- else if .ClientStreaming -}}
func (c *{{$ServiceName | lowerFirst}}Service) {{.Name}}(ctx context.Context, opts ...micro_client.CallOption) ({{$ServiceName}}_{{.Name}}Service, error) {
{{- else -}}
func (c *{{$ServiceName | lowerFirst}}Service) {{.Name}}(ctx context.Context, req *{{$reqMethod}}, opts ...micro_client.CallOption) (*{{$rspMethod}}, error) {
{{- end -}}
{{- if or (.ServerStreaming) (.ClientStreaming)}}
stream, err := c.c.Stream(ctx, c.c.NewRequest(c.name, "{{$ServiceName}}.{{.Name}}", &{{$reqMethod}}{}), opts...)
if err != nil {
return nil, err
}
{{- if not .ClientStreaming }}
if err := stream.Send(req); err != nil {
return nil, err
}
{{- end}}
return &{{$ServiceName | lowerFirst}}Service{{.Name}}{stream}, nil
{{- else}}
rsp := &{{$rspMethod}}{}
err := c.c.Call(ctx, c.c.NewRequest(c.name, "{{$ServiceName}}.{{.Name}}", req), rsp, opts...)
if err != nil {
return nil, err
}
return rsp, nil
{{- end}}
}
{{if or (.ServerStreaming) (.ClientStreaming)}}
type {{$ServiceName | lowerFirst}}Service{{.Name}} struct {
stream micro_client.Stream
}
{{if and (.ClientStreaming) (not .ServerStreaming)}}
func (x *{{$ServiceName | lowerFirst}}Service{{.Name}}) RecvAndClose() (*{{$rspMethod}}, error) {
m := &{{$rspMethod}}{}
err := x.RecvMsg(m)
if err == nil {
err = x.Close()
}
if err != nil {
return nil, err
}
return m, nil
}
{{- end}}
func (x *{{$ServiceName | lowerFirst}}Service{{.Name}}) Close() error {
return x.stream.Close()
}
func (x *{{$ServiceName | lowerFirst}}Service{{.Name}}) Context() context.Context {
return x.stream.Context()
}
func (x *{{$ServiceName | lowerFirst}}Service{{.Name}}) SendMsg(m interface{}) error {
return x.stream.Send(m)
}
func (x *{{$ServiceName | lowerFirst}}Service{{.Name}}) RecvMsg(m interface{}) error {
return x.stream.Recv(m)
}
{{ if .ClientStreaming -}}
func (x *{{$ServiceName | lowerFirst}}Service{{.Name}}) Send(m *{{$reqMethod}}) error {
return x.stream.Send(m)
}
{{end}}
{{ if .ServerStreaming -}}
func (x *{{$ServiceName | lowerFirst}}Service{{.Name}}) Recv() (*{{$rspMethod}}, error) {
m := &{{$rspMethod}}{}
if err := x.stream.Recv(m); err != nil {
return nil, err
}
return m, nil
}
{{ end }}
{{- end}}
{{- end}}
// Micro server stuff
type {{$ServiceName | lowerFirst}}Handler struct {
{{$ServiceName}}Handler
}
{{range .Service.Method }}
{{- $reqMethod := .InputType | splitArray "." | last}}
{{- $rspMethod := .OutputType | splitArray "." | last}}
{{- if or (.ServerStreaming) (.ClientStreaming)}}
func (h *{{$ServiceName | lowerFirst}}Handler) {{.Name}}(ctx context.Context, stream micro_server.Stream) error {
{{- if not .ClientStreaming}}
m := &{{$reqMethod}}{}
if err := stream.Recv(m); err != nil {
return err
}
return h.{{$ServiceName}}Handler.{{.Name}}(ctx, m, &{{$ServiceName | lowerFirst}}{{.Name}}Stream{stream})
{{- else}}
return h.{{$ServiceName}}Handler.{{.Name}}(ctx, &{{$ServiceName | lowerFirst}}{{.Name}}Stream{stream})
{{- end}}
}
{{- else}}
func (h *{{$ServiceName | lowerFirst}}Handler) {{.Name}}(ctx context.Context, req *{{$reqMethod}}, rsp *{{$rspMethod}}) error {
return h.{{$ServiceName}}Handler.{{.Name}}(ctx, req, rsp)
}
{{- end}}
{{if or (.ServerStreaming) (.ClientStreaming)}}
type {{$ServiceName | lowerFirst}}{{.Name}}Stream struct {
stream micro_server.Stream
}
{{if and (.ClientStreaming) (not .ServerStreaming)}}
func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) SendAndClose(m *{{$rspMethod}}) error {
err := x.SendMsg(m)
if err == nil {
err = x.stream.Close()
}
return err
}
{{- end}}
func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) Close() error {
return x.stream.Close()
}
func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) Context() context.Context {
return x.stream.Context()
}
func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) SendMsg(m interface{}) error {
return x.stream.Send(m)
}
func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) RecvMsg(m interface{}) error {
return x.stream.Recv(m)
}
{{- if .ServerStreaming}}
func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) Send(m *{{$rspMethod}}) error {
return x.stream.Send(m)
}
{{end}}
{{if .ClientStreaming}}
func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) Recv() (*{{$reqMethod}}, error) {
m := &{{$reqMethod}}{}
if err := x.stream.Recv(m); err != nil {
return nil, err
}
return m, nil
}
{{end}}
{{end}}
{{end}}
// Register{{$ServiceName}}Handler registers server handler
func Register{{$ServiceName}}Handler(s micro_server.Server, sh {{$ServiceName}}Handler, opts ...micro_server.HandlerOption) error {
type {{$ServiceName | lowerFirst}} interface {
{{- range .Service.Method}}
{{- $reqMethod := .InputType | splitArray "." | last}}
{{- $rspMethod := .OutputType | splitArray "." | last}}
{{- if or (.ServerStreaming) (.ClientStreaming)}}
{{.Name}}(context.Context, micro_server.Stream) error
{{- else}}
{{.Name}}(context.Context, *{{$reqMethod}}, *{{$rspMethod}}) error
{{- end}}
{{- end}}
}
type {{$ServiceName}} struct {
{{$ServiceName | lowerFirst}}
}
h := &{{$ServiceName | lowerFirst}}Handler{sh}
for _, endpoint := range New{{$ServiceName}}Endpoints() {
opts = append(opts, micro_api.WithEndpoint(endpoint))
}
return s.Handle(s.NewHandler(&{{$ServiceName}}{h}, opts...))
}

View File

@ -0,0 +1,267 @@
// Code generated by protoc-gen-micro
// source: {{.File.Name}}
package {{goPkgLastElement .File | splitArray ";" | last | replace "." "_"}}
{{- $File := .File }}
{{- $ServiceName := .Service.Name | trimSuffix "Service" }}
{{- $errmsg := list }}
{{range .Service.Method}}
{{- if not (contains (json (openapiOption .)) "null") }}
{{- if (openapiOption .).Responses }}
{{ range $k, $v := (openapiOption .).Responses }}
{{- $msgtype := (getMessageType $File $v.Schema.JsonSchema.Ref) }}
{{- $errmsg = append $errmsg $msgtype.Name }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
import (
"context"
{{- if ne (lenght $errmsg) 0 }}
"fmt"
{{- end }}
// "net/http"
micro_client "github.com/unistack-org/micro/v3/client"
micro_server "github.com/unistack-org/micro/v3/server"
micro_api "github.com/unistack-org/micro/v3/api"
micro_client_http "github.com/unistack-org/micro-client-http/v3"
)
var (
_ micro_server.Option
_ micro_client.Option
)
type {{$ServiceName | lowerFirst}}Service struct {
c micro_client.Client
name string
}
// Micro client stuff
// New{{$ServiceName}}Service create new service client
func New{{$ServiceName}}Service(name string, c micro_client.Client) {{$ServiceName}}Service {
return &{{$ServiceName | lowerFirst}}Service{c: c, name: name}
}
{{range .Service.Method}}
{{- $reqMethod := .InputType | splitArray "." | last}}
{{- $rspMethod := .OutputType | splitArray "." | last}}
{{ if and (not .ClientStreaming) (.ServerStreaming) }}
func (c *{{$ServiceName | lowerFirst}}Service) {{.Name}}(ctx context.Context, req *{{$reqMethod}}, opts ...micro_client.CallOption) ({{$ServiceName}}_{{.Name}}Service, error) {
{{ else if .ClientStreaming }}
func (c *{{$ServiceName | lowerFirst}}Service) {{.Name}}(ctx context.Context, opts ...micro_client.CallOption) ({{$ServiceName}}_{{.Name}}Service, error) {
{{ else }}
func (c *{{$ServiceName | lowerFirst}}Service) {{.Name}}(ctx context.Context, req *{{$reqMethod}}, opts ...micro_client.CallOption) (*{{$rspMethod}}, error) {
{{- end }}
{{- if not (contains (json (openapiOption .)) "null") }}
{{- if (openapiOption .).Responses }}
errmap := make(map[string]interface{}, {{ len (openapiOption .).Responses}})
{{- range $k, $v := (openapiOption .).Responses }}
errmap["{{$k}}"] = &{{- (getMessageType $File $v.Schema.JsonSchema.Ref).Name }}{}
{{- end }}
{{- end }}
{{ end }}
nopts := append(opts,
micro_client_http.Method("{{httpVerb .}}"),
micro_client_http.Path("{{httpPath .}}"),
{{- if not (contains (httpBody .) "GET") }}
micro_client_http.Body("{{httpBody .}}"),
{{- end }}
{{- if not (contains (json (openapiOption .)) "null") }}
{{- if (openapiOption .).Responses }}
micro_client_http.ErrorMap(errmap),
{{- end }}
{{- end }}
)
{{- if or (.ServerStreaming) (.ClientStreaming)}}
stream, err := c.c.Stream(ctx, c.c.NewRequest(c.name, "{{$ServiceName}}.{{.Name}}", &{{$reqMethod}}{}), nopts...)
if err != nil {
return nil, err
}
{{- if not .ClientStreaming }}
if err := stream.Send(req); err != nil {
return nil, err
}
{{- end}}
return &{{$ServiceName | lowerFirst}}Service{{.Name}}{stream}, nil
{{- else}}
rsp := &{{$rspMethod}}{}
err := c.c.Call(ctx, c.c.NewRequest(c.name, "{{$ServiceName}}.{{.Name}}", req), rsp, nopts...)
if err != nil {
return nil, err
}
return rsp, nil
{{- end}}
}
{{if or (.ServerStreaming) (.ClientStreaming)}}
type {{$ServiceName | lowerFirst}}Service{{.Name}} struct {
stream micro_client.Stream
}
{{if and (.ClientStreaming) (not .ServerStreaming)}}
func (x *{{$ServiceName | lowerFirst}}Service{{.Name}}) RecvAndClose() (*{{$rspMethod}}, error) {
m := &{{$rspMethod}}{}
err := x.RecvMsg(m)
if err == nil {
err = x.Close()
}
if err != nil {
return nil, err
}
return m, nil
}
{{- end}}
func (x *{{$ServiceName | lowerFirst}}Service{{.Name}}) Close() error {
return x.stream.Close()
}
func (x *{{$ServiceName | lowerFirst}}Service{{.Name}}) Context() context.Context {
return x.stream.Context()
}
func (x *{{$ServiceName | lowerFirst}}Service{{.Name}}) SendMsg(m interface{}) error {
return x.stream.Send(m)
}
func (x *{{$ServiceName | lowerFirst}}Service{{.Name}}) RecvMsg(m interface{}) error {
return x.stream.Recv(m)
}
{{ if .ClientStreaming -}}
func (x *{{$ServiceName | lowerFirst}}Service{{.Name}}) Send(m *{{$reqMethod}}) error {
return x.stream.Send(m)
}
{{- end}}
{{ if .ServerStreaming -}}
func (x *{{$ServiceName | lowerFirst}}Service{{.Name}}) Recv() (*{{$rspMethod}}, error) {
m := &{{$rspMethod}}{}
if err := x.stream.Recv(m); err != nil {
return nil, err
}
return m, nil
}
{{- end }}
{{- end}}
{{end}}
// Micro server stuff
type {{$ServiceName | lowerFirst}}Handler struct {
{{$ServiceName}}Handler
}
{{range .Service.Method }}
{{- $reqMethod := .InputType | splitArray "." | last}}
{{- $rspMethod := .OutputType | splitArray "." | last}}
{{- if or (.ServerStreaming) (.ClientStreaming)}}
func (h *{{$ServiceName | lowerFirst}}Handler) {{.Name}}(ctx context.Context, stream micro_server.Stream) error {
{{- if not .ClientStreaming}}
m := &{{$reqMethod}}{}
if err := stream.Recv(m); err != nil {
return err
}
return h.{{$ServiceName}}Handler.{{.Name}}(ctx, m, &{{$ServiceName | lowerFirst}}{{.Name}}Stream{stream})
{{- else}}
return h.{{$ServiceName}}Handler.{{.Name}}(ctx, &{{$ServiceName | lowerFirst}}{{.Name}}Stream{stream})
{{- end}}
}
{{- else}}
func (h *{{$ServiceName | lowerFirst}}Handler) {{.Name}}(ctx context.Context, req *{{$reqMethod}}, rsp *{{$rspMethod}}) error {
return h.{{$ServiceName}}Handler.{{.Name}}(ctx, req, rsp)
}
{{- end}}
/*
func (h *{{$ServiceName | lowerFirst}}Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Printf("new request: %#+v\n", r)
// HANDLE ALL STUFF
}
*/
{{if or (.ServerStreaming) (.ClientStreaming)}}
type {{$ServiceName | lowerFirst}}{{.Name}}Stream struct {
stream micro_server.Stream
}
{{if and (.ClientStreaming) (not .ServerStreaming)}}
func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) SendAndClose(m *{{$rspMethod}}) error {
err := x.SendMsg(m)
if err == nil {
err = x.stream.Close()
}
return err
}
{{- end}}
func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) Close() error {
return x.stream.Close()
}
func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) Context() context.Context {
return x.stream.Context()
}
func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) SendMsg(m interface{}) error {
return x.stream.Send(m)
}
func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) RecvMsg(m interface{}) error {
return x.stream.Recv(m)
}
{{if .ServerStreaming}}
func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) Send(m *{{$rspMethod}}) error {
return x.stream.Send(m)
}
{{end}}
{{if .ClientStreaming}}
func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) Recv() (*{{$reqMethod}}, error) {
m := &{{$reqMethod}}{}
if err := x.stream.Recv(m); err != nil {
return nil, err
}
return m, nil
}
{{end}}
{{end}}
{{end}}
{{range $k, $v := ($errmsg | uniq) }}
// Error method to satisfy error interface
func (e *{{- $v }}) Error() string {
return fmt.Sprintf("%#v", e)
}
{{- end }}
// Register{{$ServiceName}}Handler registers server handler
func Register{{$ServiceName}}Handler(s micro_server.Server, sh {{$ServiceName}}Handler, opts ...micro_server.HandlerOption) error {
type {{$ServiceName | lowerFirst}} interface {
{{- range .Service.Method}}
{{- $reqMethod := .InputType | splitArray "." | last}}
{{- $rspMethod := .OutputType | splitArray "." | last}}
{{- if or (.ServerStreaming) (.ClientStreaming)}}
{{.Name}}(context.Context, micro_server.Stream) error
{{- else}}
{{.Name}}(context.Context, *{{$reqMethod}}, *{{$rspMethod}}) error
{{- end}}
{{- end}}
// ServeHTTP(http.ResponseWriter, *http.Request)
}
type {{$ServiceName}} struct {
{{$ServiceName | lowerFirst}}
}
h := &{{$ServiceName | lowerFirst}}Handler{sh}
for _, endpoint := range New{{$ServiceName}}Endpoints() {
opts = append(opts, micro_api.WithEndpoint(endpoint))
}
return s.Handle(s.NewHandler(&{{$ServiceName}}{h}, opts...))
}

View File

@ -0,0 +1,142 @@
// Code generated by protoc-gen-micro
// source: {{.File.Name}}
package {{goPkgLastElement .File | splitArray ";" | last | replace "." "_"}}
import (
"context"
micro_api "github.com/unistack-org/micro/v3/api"
micro_client "github.com/unistack-org/micro/v3/client"
)
{{- $ServiceName := .Service.Name | trimSuffix "Service" }}
{{- $epnum := 0 }}
{{- range .Service.Method}}
{{- if not (contains (json (httpOption .)) "null") }}
{{- if ne (httpVerb .) "" }}
{{- $epnum = add $epnum 1 }}
{{- range httpPathsAdditionalBindings . }}
{{- $epnum = add $epnum 1 }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
// New{{$ServiceName}}Endpoints provides api endpoints metdata for {{$ServiceName}} service
func New{{$ServiceName}}Endpoints() []*micro_api.Endpoint {
endpoints := make([]*micro_api.Endpoint, 0, {{ $epnum }})
{{- if ne $epnum 0 }}
var endpoint *micro_api.Endpoint
{{- end }}
{{- range .Service.Method}}
{{- if not (contains (json (httpOption .)) "null") }}
{{- $httpOption := (httpOption .) }}
{{- if ne $httpOption.Method "" }}
endpoint = &micro_api.Endpoint{
Name: "{{$ServiceName}}.{{.Name}}",
Path: []string{"{{$httpOption.Path}}"},
Method: []string{"{{$httpOption.Method}}"},
Body: "{{$httpOption.Body}}",
{{- if or (.ClientStreaming) (.ServerStreaming)}}
Stream: true,
{{- end}}
Handler: "rpc",
}
endpoints = append(endpoints, endpoint)
{{- range $index, $element := $httpOption.Additional }}
endpoint = &micro_api.Endpoint{
Name: "{{$ServiceName}}.{{.Name}}",
Path: []string{"{{$element.Path}}"},
Method: []string{"{{$element.Method}}"},
Body: "{{$element.Body}}",
{{- if or (.ClientStreaming) (.ServerStreaming)}}
Stream: true,
{{- end}}
Handler: "rpc",
}
endpoints = append(endpoints, endpoint)
{{- end}}
{{- end}}
{{- end}}
{{- end}}
return endpoints
}
// {{$ServiceName}}Service interface
type {{$ServiceName}}Service interface {
{{- range .Service.Method}}
{{- $reqMethod := .InputType | splitArray "." | last }}
{{- $rspMethod := .OutputType | splitArray "." | last }}
{{- if or (.ServerStreaming) (.ClientStreaming)}}
{{- if and (.ServerStreaming) (not .ClientStreaming)}}
{{.Name}}(context.Context, *{{$reqMethod}}, ...micro_client.CallOption) ({{$ServiceName}}_{{.Name}}Service, error)
{{- else}}
{{.Name}}(context.Context, ...micro_client.CallOption) ({{$ServiceName}}_{{.Name}}Service, error)
{{- end}}
{{- else}}
{{.Name}}(context.Context, *{{$reqMethod}}, ...micro_client.CallOption) (*{{$rspMethod}}, error)
{{- end}}
{{- end}}
}
{{range .Service.Method}}
{{- $reqMethod := .InputType | splitArray "." | last}}
{{- $rspMethod := .OutputType | splitArray "." | last}}
{{if or (.ServerStreaming) (.ClientStreaming)}}
type {{$ServiceName}}_{{.Name}}Service interface {
Context() context.Context
SendMsg(interface{}) error
RecvMsg(interface{}) error
{{- if and (.ClientStreaming) (not .ServerStreaming)}}
RecvAndClose() (*{{$rspMethod}}, error)
{{- end}}
Close() error
{{- if .ClientStreaming}}
Send(*{{$reqMethod}}) error
{{- end}}
{{- if .ServerStreaming}}
Recv() (*{{$rspMethod}}, error)
{{- end}}
}
{{- end}}
{{- end}}
// Micro server stuff
// {{$ServiceName}}Handler server handler
type {{$ServiceName}}Handler interface {
{{- range .Service.Method}}
{{- $reqMethod := .InputType | splitArray "." | last}}
{{- $rspMethod := .OutputType | splitArray "." | last}}
{{- if or (.ServerStreaming) (.ClientStreaming)}}
{{- if not .ClientStreaming}}
{{.Name}}(context.Context, *{{$reqMethod}}, {{$ServiceName}}_{{.Name}}Stream) error
{{- else}}
{{.Name}}(context.Context, {{$ServiceName}}_{{.Name}}Stream) error
{{- end}}
{{- else}}
{{.Name}}(context.Context, *{{$reqMethod}}, *{{$rspMethod}}) error
{{- end}}
{{- end}}
}
{{- range .Service.Method}}
{{- $reqMethod := .InputType | splitArray "." | last}}
{{- $rspMethod := .OutputType | splitArray "." | last}}
{{if or (.ServerStreaming) (.ClientStreaming)}}
type {{$ServiceName}}_{{.Name}}Stream interface {
Context() context.Context
SendMsg(interface{}) error
RecvMsg(interface{}) error
{{- if and (.ClientStreaming) (not .ServerStreaming)}}
SendAndClose(*{{$rspMethod}}) error
{{- end}}
Close() error
{{- if .ServerStreaming}}
Send(*{{$rspMethod}}) error
{{- end}}
{{- if .ClientStreaming}}
Recv() (*{{$reqMethod}}, error)
{{- end}}
}
{{- end}}
{{- end}}

View File

@ -1,7 +0,0 @@
//go:build ignore
package tools
import (
_ "go.unistack.org/micro-proto/v3/graphql"
)

1051
util.go

File diff suppressed because it is too large Load Diff

View File

@ -1,28 +0,0 @@
package main
import "google.golang.org/protobuf/compiler/protogen"
var (
ioPackage = protogen.GoImportPath("io")
graphqlPackage = protogen.GoImportPath("github.com/99designs/gqlgen/graphql")
reflectPackage = protogen.GoImportPath("reflect")
stringsPackage = protogen.GoImportPath("strings")
fmtPackage = protogen.GoImportPath("fmt")
contextPackage = protogen.GoImportPath("context")
httpPackage = protogen.GoImportPath("net/http")
gorillaMuxPackage = protogen.GoImportPath("github.com/gorilla/mux")
chiPackage = protogen.GoImportPath("github.com/go-chi/chi/v5")
chiMiddlewarePackage = protogen.GoImportPath("github.com/go-chi/chi/v5/middleware")
microMetadataPackage = protogen.GoImportPath("go.unistack.org/micro/v3/metadata")
microClientPackage = protogen.GoImportPath("go.unistack.org/micro/v3/client")
microServerPackage = protogen.GoImportPath("go.unistack.org/micro/v3/server")
microClientHttpPackage = protogen.GoImportPath("go.unistack.org/micro-client-http/v3")
microServerHttpPackage = protogen.GoImportPath("go.unistack.org/micro-server-http/v3")
microCodecPackage = protogen.GoImportPath("go.unistack.org/micro-proto/v3/codec")
microErrorsPackage = protogen.GoImportPath("go.unistack.org/micro/v3/errors")
grpcPackage = protogen.GoImportPath("google.golang.org/grpc")
protojsonPackage = protogen.GoImportPath("google.golang.org/protobuf/encoding/protojson")
timePackage = protogen.GoImportPath("time")
deprecationComment = "// Deprecated: Do not use."
versionComment = "v3.10.4"
)