parent
e96ca8a00d
commit
bfa0593eab
@ -1,16 +0,0 @@
|
|||||||
package dorm
|
|
||||||
|
|
||||||
import (
|
|
||||||
"entgo.io/ent"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ConfigEntClient struct {
|
|
||||||
Dns string // 地址
|
|
||||||
}
|
|
||||||
|
|
||||||
// EntClient
|
|
||||||
// https://entgo.io/
|
|
||||||
type EntClient struct {
|
|
||||||
Db *ent.Edge // 驱动
|
|
||||||
config *ConfigEntClient // 配置
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
package dorm
|
|
||||||
|
|
||||||
import (
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
)
|
|
||||||
|
|
||||||
func NewEntMysqlClient(config *ConfigEntClient) (*EntClient, error) {
|
|
||||||
|
|
||||||
//var err error
|
|
||||||
//c := &EntClient{config: config}
|
|
||||||
|
|
||||||
//client, err := ent.Open("mysql", c.config.Dns)
|
|
||||||
//if err != nil {
|
|
||||||
// return nil, errors.New(fmt.Sprintf("连接失败:%v", err))
|
|
||||||
//}
|
|
||||||
//defer client.Close()
|
|
||||||
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
@ -1,75 +0,0 @@
|
|||||||
run:
|
|
||||||
go: '1.17'
|
|
||||||
timeout: 5m
|
|
||||||
|
|
||||||
linters-settings:
|
|
||||||
errcheck:
|
|
||||||
ignore: fmt:.*,Read|Write|Close|Exec,io:Copy
|
|
||||||
dupl:
|
|
||||||
threshold: 100
|
|
||||||
funlen:
|
|
||||||
lines: 115
|
|
||||||
statements: 115
|
|
||||||
goheader:
|
|
||||||
template: |-
|
|
||||||
Copyright 2019-present Facebook Inc. All rights reserved.
|
|
||||||
This source code is licensed under the Apache 2.0 license found
|
|
||||||
in the LICENSE file in the root directory of this source tree.
|
|
||||||
linters:
|
|
||||||
disable-all: true
|
|
||||||
enable:
|
|
||||||
- bodyclose
|
|
||||||
- deadcode
|
|
||||||
- depguard
|
|
||||||
- dogsled
|
|
||||||
- dupl
|
|
||||||
- errcheck
|
|
||||||
- funlen
|
|
||||||
- gocritic
|
|
||||||
- gofmt
|
|
||||||
- goheader
|
|
||||||
- gosec
|
|
||||||
- gosimple
|
|
||||||
- govet
|
|
||||||
- ineffassign
|
|
||||||
- misspell
|
|
||||||
- staticcheck
|
|
||||||
- structcheck
|
|
||||||
- stylecheck
|
|
||||||
- typecheck
|
|
||||||
- unconvert
|
|
||||||
- unused
|
|
||||||
- varcheck
|
|
||||||
- whitespace
|
|
||||||
|
|
||||||
issues:
|
|
||||||
exclude-rules:
|
|
||||||
- path: _test\.go
|
|
||||||
linters:
|
|
||||||
- dupl
|
|
||||||
- funlen
|
|
||||||
- gosec
|
|
||||||
- gocritic
|
|
||||||
- linters:
|
|
||||||
- unused
|
|
||||||
source: ent.Schema
|
|
||||||
- path: dialect/sql/schema
|
|
||||||
linters:
|
|
||||||
- dupl
|
|
||||||
- gosec
|
|
||||||
- text: "Expect WriteFile permissions to be 0600 or less"
|
|
||||||
linters:
|
|
||||||
- gosec
|
|
||||||
- path: privacy/privacy.go
|
|
||||||
linters:
|
|
||||||
- stylecheck
|
|
||||||
- path: entc/load/schema.go
|
|
||||||
linters:
|
|
||||||
- staticcheck
|
|
||||||
- path: entc/gen/graph.go
|
|
||||||
linters:
|
|
||||||
- gocritic
|
|
||||||
- path: entc/integration/multischema/multischema_test.go
|
|
||||||
linters:
|
|
||||||
- staticcheck
|
|
||||||
text: SA1019
|
|
@ -1,77 +0,0 @@
|
|||||||
# Code of Conduct
|
|
||||||
|
|
||||||
## Our Pledge
|
|
||||||
|
|
||||||
In the interest of fostering an open and welcoming environment, we as
|
|
||||||
contributors and maintainers pledge to make participation in our project and
|
|
||||||
our community a harassment-free experience for everyone, regardless of age, body
|
|
||||||
size, disability, ethnicity, sex characteristics, gender identity and expression,
|
|
||||||
level of experience, education, socio-economic status, nationality, personal
|
|
||||||
appearance, race, religion, or sexual identity and orientation.
|
|
||||||
|
|
||||||
## Our Standards
|
|
||||||
|
|
||||||
Examples of behavior that contributes to creating a positive environment
|
|
||||||
include:
|
|
||||||
|
|
||||||
* Using welcoming and inclusive language
|
|
||||||
* Being respectful of differing viewpoints and experiences
|
|
||||||
* Gracefully accepting constructive criticism
|
|
||||||
* Focusing on what is best for the community
|
|
||||||
* Showing empathy towards other community members
|
|
||||||
|
|
||||||
Examples of unacceptable behavior by participants include:
|
|
||||||
|
|
||||||
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
||||||
advances
|
|
||||||
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
||||||
* Public or private harassment
|
|
||||||
* Publishing others' private information, such as a physical or electronic
|
|
||||||
address, without explicit permission
|
|
||||||
* Other conduct which could reasonably be considered inappropriate in a
|
|
||||||
professional setting
|
|
||||||
|
|
||||||
## Our Responsibilities
|
|
||||||
|
|
||||||
Project maintainers are responsible for clarifying the standards of acceptable
|
|
||||||
behavior and are expected to take appropriate and fair corrective action in
|
|
||||||
response to any instances of unacceptable behavior.
|
|
||||||
|
|
||||||
Project maintainers have the right and responsibility to remove, edit, or
|
|
||||||
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
||||||
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
||||||
permanently any contributor for other behaviors that they deem inappropriate,
|
|
||||||
threatening, offensive, or harmful.
|
|
||||||
|
|
||||||
## Scope
|
|
||||||
|
|
||||||
This Code of Conduct applies within all project spaces, and it also applies when
|
|
||||||
an individual is representing the project or its community in public spaces.
|
|
||||||
Examples of representing a project or community include using an official
|
|
||||||
project e-mail address, posting via an official social media account, or acting
|
|
||||||
as an appointed representative at an online or offline event. Representation of
|
|
||||||
a project may be further defined and clarified by project maintainers.
|
|
||||||
|
|
||||||
## Enforcement
|
|
||||||
|
|
||||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
||||||
reported by contacting the project team at <opensource-conduct@fb.com>. All
|
|
||||||
complaints will be reviewed and investigated and will result in a response that
|
|
||||||
is deemed necessary and appropriate to the circumstances. The project team is
|
|
||||||
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
||||||
Further details of specific enforcement policies may be posted separately.
|
|
||||||
|
|
||||||
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
||||||
faith may face temporary or permanent repercussions as determined by other
|
|
||||||
members of the project's leadership.
|
|
||||||
|
|
||||||
## Attribution
|
|
||||||
|
|
||||||
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
||||||
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
|
|
||||||
|
|
||||||
[homepage]: https://www.contributor-covenant.org
|
|
||||||
|
|
||||||
For answers to common questions about this code of conduct, see
|
|
||||||
https://www.contributor-covenant.org/faq
|
|
||||||
|
|
@ -1,69 +0,0 @@
|
|||||||
# Contributing to ent
|
|
||||||
We want to make contributing to this project as easy and transparent as
|
|
||||||
possible.
|
|
||||||
|
|
||||||
# Project structure
|
|
||||||
|
|
||||||
- `dialect` - Contains SQL and Gremlin code used by the generated code.
|
|
||||||
- `dialect/sql/schema` - Auto migration logic resides there.
|
|
||||||
|
|
||||||
- `schema` - User schema API.
|
|
||||||
- `schema/{field, edge, index, mixin}` - provides schema builders API.
|
|
||||||
- `schema/field/gen` - Templates and codegen for numeric builders.
|
|
||||||
|
|
||||||
- `entc` - Codegen of `ent`.
|
|
||||||
- `entc/load` - `entc` loader API for loading user schemas into a Go objects at runtime.
|
|
||||||
- `entc/gen` - The actual code generation logic resides in this package (and its `templates` package).
|
|
||||||
- `integration` - Integration tests for `entc`.
|
|
||||||
|
|
||||||
- `privacy` - Runtime code for [privacy layer](https://entgo.io/docs/privacy/).
|
|
||||||
|
|
||||||
- `doc` - Documentation code for `entgo.io` (uses [Docusaurus](https://docusaurus.io)).
|
|
||||||
- `doc/md` - Markdown files for documentation.
|
|
||||||
- `doc/website` - Website code and assets.
|
|
||||||
|
|
||||||
In order to test your documentation changes, run `npm start` from the `doc/website` directory, and open [localhost:3000](http://localhost:3000/).
|
|
||||||
|
|
||||||
# Run integration tests
|
|
||||||
If you touch any file in `entc`, run the following command in `entc`:
|
|
||||||
|
|
||||||
```
|
|
||||||
go generate ./...
|
|
||||||
```
|
|
||||||
|
|
||||||
Then, in `entc/integration` run `docker-compose` in order to spin-up all database containers:
|
|
||||||
|
|
||||||
```
|
|
||||||
docker-compose -f docker-compose.yaml up -d
|
|
||||||
```
|
|
||||||
|
|
||||||
Then, run `go test ./...` to run all integration tests.
|
|
||||||
|
|
||||||
|
|
||||||
## Pull Requests
|
|
||||||
We actively welcome your pull requests.
|
|
||||||
|
|
||||||
1. Fork the repo and create your branch from `master`.
|
|
||||||
2. If you've added code that should be tested, add tests.
|
|
||||||
3. If you've changed APIs, update the documentation.
|
|
||||||
4. Ensure the test suite passes.
|
|
||||||
5. Make sure your code lints.
|
|
||||||
6. If you haven't already, complete the Contributor License Agreement ("CLA").
|
|
||||||
|
|
||||||
## Contributor License Agreement ("CLA")
|
|
||||||
In order to accept your pull request, we need you to submit a CLA. You only need
|
|
||||||
to do this once to work on any of Facebook's open source projects.
|
|
||||||
|
|
||||||
Complete your CLA here: <https://code.facebook.com/cla>
|
|
||||||
|
|
||||||
## Issues
|
|
||||||
We use GitHub issues to track public bugs. Please ensure your description is
|
|
||||||
clear and has sufficient instructions to be able to reproduce the issue.
|
|
||||||
|
|
||||||
Facebook has a [bounty program](https://www.facebook.com/whitehat/) for the safe
|
|
||||||
disclosure of security bugs. In those cases, please go through the process
|
|
||||||
outlined on that page and do not file a public issue.
|
|
||||||
|
|
||||||
## License
|
|
||||||
By contributing to ent, you agree that your contributions will be licensed
|
|
||||||
under the LICENSE file in the root directory of this source tree.
|
|
@ -1,202 +0,0 @@
|
|||||||
|
|
||||||
Apache License
|
|
||||||
Version 2.0, January 2004
|
|
||||||
http://www.apache.org/licenses/
|
|
||||||
|
|
||||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
||||||
|
|
||||||
1. Definitions.
|
|
||||||
|
|
||||||
"License" shall mean the terms and conditions for use, reproduction,
|
|
||||||
and distribution as defined by Sections 1 through 9 of this document.
|
|
||||||
|
|
||||||
"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
|
|
||||||
|
|
||||||
APPENDIX: How to apply the Apache License to your work.
|
|
||||||
|
|
||||||
To apply the Apache License to your work, attach the following
|
|
||||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
||||||
replaced with your own identifying information. (Don't include
|
|
||||||
the brackets!) The text should be enclosed in the appropriate
|
|
||||||
comment syntax for the file format. We also recommend that a
|
|
||||||
file or class name and description of purpose be included on the
|
|
||||||
same "printed page" as the copyright notice for easier
|
|
||||||
identification within third-party archives.
|
|
||||||
|
|
||||||
Copyright [yyyy] [name of copyright owner]
|
|
||||||
|
|
||||||
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.
|
|
@ -1,58 +0,0 @@
|
|||||||
## ent - An Entity Framework For Go
|
|
||||||
|
|
||||||
[![Twitter](https://img.shields.io/twitter/url/https/twitter.com/entgo_io.svg?style=social&label=Follow%20%40entgo_io)](https://twitter.com/entgo_io)
|
|
||||||
[![Discord](https://img.shields.io/discord/885059418646003782?label=discord&logo=discord&style=flat-square&logoColor=white)](https://discord.gg/qZmPgTE6RX)
|
|
||||||
|
|
||||||
[English](README.md) | [中文](README_zh.md) | [日本語](README_jp.md)
|
|
||||||
|
|
||||||
<img width="50%"
|
|
||||||
align="right"
|
|
||||||
style="display: block; margin:40px auto;"
|
|
||||||
src="https://s3.eu-central-1.amazonaws.com/entgo.io/assets/gopher_graph.png"/>
|
|
||||||
|
|
||||||
Simple, yet powerful entity framework for Go, that makes it easy to build and maintain applications
|
|
||||||
with large data-models.
|
|
||||||
|
|
||||||
- **Schema As Code** - model any database schema as Go objects.
|
|
||||||
- **Easily Traverse Any Graph** - run queries, aggregations and traverse any graph structure easily.
|
|
||||||
- **Statically Typed And Explicit API** - 100% statically typed and explicit API using code generation.
|
|
||||||
- **Multi Storage Driver** - supports MySQL, MariaDB, TiDB, PostgreSQL, CockroachDB, SQLite and Gremlin.
|
|
||||||
- **Extendable** - simple to extend and customize using Go templates.
|
|
||||||
|
|
||||||
## Quick Installation
|
|
||||||
```console
|
|
||||||
go install entgo.io/ent/cmd/ent@latest
|
|
||||||
```
|
|
||||||
|
|
||||||
For proper installation using [Go modules], visit [entgo.io website][entgo instal].
|
|
||||||
|
|
||||||
## Docs and Support
|
|
||||||
The documentation for developing and using ent is available at: https://entgo.io
|
|
||||||
|
|
||||||
For discussion and support, [open an issue](https://github.com/ent/ent/issues/new/choose) or join our [channel](https://gophers.slack.com/archives/C01FMSQDT53) in the gophers Slack.
|
|
||||||
|
|
||||||
## Join the ent Community
|
|
||||||
Building `ent` would not have been possible without the collective work of our entire community. We maintain a [contributors page](doc/md/contributors.md)
|
|
||||||
which lists the contributors to this `ent`.
|
|
||||||
|
|
||||||
In order to contribute to `ent`, see the [CONTRIBUTING](CONTRIBUTING.md) file for how to go get started.
|
|
||||||
If your company or your product is using `ent`, please let us know by adding yourself to the [ent users page](https://github.com/ent/ent/wiki/ent-users).
|
|
||||||
|
|
||||||
For updates, follow us on Twitter at https://twitter.com/entgo_io
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## About the Project
|
|
||||||
The `ent` project was inspired by Ent, an entity framework we use internally. It is developed and maintained
|
|
||||||
by [a8m](https://github.com/a8m) and [alexsn](https://github.com/alexsn)
|
|
||||||
from the [Facebook Connectivity][fbc] team. It is used by multiple teams and projects in production,
|
|
||||||
and the roadmap for its v1 release is described [here](https://github.com/ent/ent/issues/46).
|
|
||||||
Read more about the motivation of the project [here](https://entgo.io/blog/2019/10/03/introducing-ent).
|
|
||||||
|
|
||||||
## License
|
|
||||||
ent is licensed under Apache 2.0 as found in the [LICENSE file](LICENSE).
|
|
||||||
|
|
||||||
|
|
||||||
[entgo instal]: https://entgo.io/docs/code-gen/#version-compatibility-between-entc-and-ent
|
|
||||||
[Go modules]: https://github.com/golang/go/wiki/Modules#quick-start
|
|
||||||
[fbc]: https://connectivity.fb.com
|
|
@ -1,54 +0,0 @@
|
|||||||
## ent - Goのエンティティーフレームワーク
|
|
||||||
|
|
||||||
[![Twitter](https://img.shields.io/twitter/url/https/twitter.com/entgo_io.svg?style=social&label=Follow%20%40entgo_io)](https://twitter.com/entgo_io)
|
|
||||||
|
|
||||||
[English](README.md) | [中文](README_zh.md) | [日本語](README_jp.md)
|
|
||||||
|
|
||||||
<img width="50%"
|
|
||||||
align="right"
|
|
||||||
style="display: block; margin:40px auto;"
|
|
||||||
src="https://s3.eu-central-1.amazonaws.com/entgo.io/assets/gopher_graph.png"/>
|
|
||||||
|
|
||||||
シンプルながらもパワフルなGoのエンティティフレームワークであり、大規模なデータモデルを持つアプリケーションを容易に構築・保守できるようにします。
|
|
||||||
|
|
||||||
- **Schema As Code(コードとしてのスキーマ)** - あらゆるデータベーススキーマをGoオブジェクトとしてモデル化します。
|
|
||||||
- **任意のグラフを簡単にトラバースできます** - クエリや集約の実行、任意のグラフ構造の走査を容易に実行できます。
|
|
||||||
- **100%静的に型付けされた明示的なAPI** - コード生成により、100%静的に型付けされた曖昧さのないAPIを提供します。
|
|
||||||
- **マルチストレージドライバ** - MySQL、PostgreSQL、SQLite、Gremlinをサポートしています。
|
|
||||||
- **拡張性** - Goテンプレートを使用して簡単に拡張、カスタマイズできます。
|
|
||||||
|
|
||||||
## クイックインストール
|
|
||||||
```console
|
|
||||||
go install entgo.io/ent/cmd/ent@latest
|
|
||||||
```
|
|
||||||
|
|
||||||
[Go modules]を使ったインストールについては、[entgo.io website][entgo instal]をご覧ください。
|
|
||||||
|
|
||||||
## ドキュメントとサポート
|
|
||||||
entを開発・使用するためのドキュメントは、こちら。: https://entgo.io
|
|
||||||
|
|
||||||
議論やサポートについては、[Issueを立てる](https://github.com/ent/ent/issues/new/choose)か、gophers Slackの[チャンネル](https://gophers.slack.com/archives/C01FMSQDT53)に参加してください。
|
|
||||||
|
|
||||||
## entコミュニティへの参加
|
|
||||||
`ent`の構築は、コミュニティ全体の協力なしには実現できませんでした。 私たちは、この`ent`の貢献者をリストアップした[contributorsページ](doc/md/contributors.md)を管理しています。
|
|
||||||
|
|
||||||
`ent`に貢献するときは、まず[CONTRIBUTING](CONTRIBUTING.md)を参照してください。
|
|
||||||
もし、あなたの会社や製品で`ent`を利用している場合は、[ent usersページ](https://github.com/ent/ent/wiki/ent-users)に追記する形で、そのことをぜひ教えて下さい。
|
|
||||||
|
|
||||||
最新情報については、Twitter(<https://twitter.com/entgo_io>)をフォローしてください。
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## プロジェクトについて
|
|
||||||
`ent`プロジェクトは、私たちが社内で使用しているエンティティフレームワークである`Ent`からインスピレーションを得ています。
|
|
||||||
entは、[Facebook Connectivity][fbc]チームの[a8m](https://github.com/a8m)と[alexsn](https://github.com/alexsn)が開発・保守しています。
|
|
||||||
本番環境では複数のチームやプロジェクトで使用されており、v1リリースまでのロードマップは[こちら](https://github.com/ent/ent/issues/46)に記載されています。
|
|
||||||
このプロジェクトの動機については[こちら](https://entgo.io/blog/2019/10/03/introducing-ent)をご覧ください。
|
|
||||||
|
|
||||||
## ライセンス
|
|
||||||
entは、[LICENSEファイル](LICENSE)にもある通り、Apache 2.0でライセンスされています。
|
|
||||||
|
|
||||||
|
|
||||||
[entgo instal]: https://entgo.io/docs/code-gen/#version-compatibility-between-entc-and-ent
|
|
||||||
[Go modules]: https://github.com/golang/go/wiki/Modules#quick-start
|
|
||||||
[fbc]: https://connectivity.fb.com
|
|
@ -1,365 +0,0 @@
|
|||||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
|
||||||
// This source code is licensed under the Apache 2.0 license found
|
|
||||||
// in the LICENSE file in the root directory of this source tree.
|
|
||||||
|
|
||||||
// Package ent is the interface between end-user schemas and entc (ent codegen).
|
|
||||||
package ent
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"entgo.io/ent/schema"
|
|
||||||
"entgo.io/ent/schema/edge"
|
|
||||||
"entgo.io/ent/schema/field"
|
|
||||||
"entgo.io/ent/schema/index"
|
|
||||||
)
|
|
||||||
|
|
||||||
type (
|
|
||||||
// The Interface type describes the requirements for an exported type defined in the schema package.
|
|
||||||
// It functions as the interface between the user's schema types and codegen loader.
|
|
||||||
// Users should use the Schema type for embedding as follows:
|
|
||||||
//
|
|
||||||
// type T struct {
|
|
||||||
// ent.Schema
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
Interface interface {
|
|
||||||
// Type is a dummy method, that is used in edge declaration.
|
|
||||||
//
|
|
||||||
// The Type method should be used as follows:
|
|
||||||
//
|
|
||||||
// type S struct { ent.Schema }
|
|
||||||
//
|
|
||||||
// type T struct { ent.Schema }
|
|
||||||
//
|
|
||||||
// func (T) Edges() []ent.Edge {
|
|
||||||
// return []ent.Edge{
|
|
||||||
// edge.To("S", S.Type),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
Type()
|
|
||||||
// Fields returns the fields of the schema.
|
|
||||||
Fields() []Field
|
|
||||||
// Edges returns the edges of the schema.
|
|
||||||
Edges() []Edge
|
|
||||||
// Indexes returns the indexes of the schema.
|
|
||||||
Indexes() []Index
|
|
||||||
// Config returns an optional config for the schema.
|
|
||||||
//
|
|
||||||
// Deprecated: the Config method predates the Annotations method, and it
|
|
||||||
// is planned be removed in v0.5.0. New code should use Annotations instead.
|
|
||||||
//
|
|
||||||
// func (T) Annotations() []schema.Annotation {
|
|
||||||
// return []schema.Annotation{
|
|
||||||
// entsql.Annotation{Table: "Name"},
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
Config() Config
|
|
||||||
// Mixin returns an optional list of Mixin to extends
|
|
||||||
// the schema.
|
|
||||||
Mixin() []Mixin
|
|
||||||
// Hooks returns an optional list of Hook to apply on
|
|
||||||
// mutations.
|
|
||||||
Hooks() []Hook
|
|
||||||
// Policy returns the privacy policy of the schema.
|
|
||||||
Policy() Policy
|
|
||||||
// Annotations returns a list of schema annotations to be used by
|
|
||||||
// codegen extensions.
|
|
||||||
Annotations() []schema.Annotation
|
|
||||||
}
|
|
||||||
|
|
||||||
// A Field interface returns a field descriptor for vertex fields/properties.
|
|
||||||
// The usage for the interface is as follows:
|
|
||||||
//
|
|
||||||
// func (T) Fields() []ent.Field {
|
|
||||||
// return []ent.Field{
|
|
||||||
// field.Int("int"),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
Field interface {
|
|
||||||
Descriptor() *field.Descriptor
|
|
||||||
}
|
|
||||||
|
|
||||||
// An Edge interface returns an edge descriptor for vertex edges.
|
|
||||||
// The usage for the interface is as follows:
|
|
||||||
//
|
|
||||||
// func (T) Edges() []ent.Edge {
|
|
||||||
// return []ent.Edge{
|
|
||||||
// edge.To("S", S.Type),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
Edge interface {
|
|
||||||
Descriptor() *edge.Descriptor
|
|
||||||
}
|
|
||||||
|
|
||||||
// An Index interface returns an index descriptor for vertex indexes.
|
|
||||||
// The usage for the interface is as follows:
|
|
||||||
//
|
|
||||||
// func (T) Indexes() []ent.Index {
|
|
||||||
// return []ent.Index{
|
|
||||||
// index.Fields("f1", "f2").
|
|
||||||
// Unique(),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
Index interface {
|
|
||||||
Descriptor() *index.Descriptor
|
|
||||||
}
|
|
||||||
|
|
||||||
// A Config structure is used to configure an entity schema.
|
|
||||||
// The usage of this structure is as follows:
|
|
||||||
//
|
|
||||||
// func (T) Config() ent.Config {
|
|
||||||
// return ent.Config{
|
|
||||||
// Table: "Name",
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// Deprecated: the Config object predates the schema.Annotation method and it
|
|
||||||
// is planned be removed in v0.5.0. New code should use Annotations instead.
|
|
||||||
//
|
|
||||||
// func (T) Annotations() []schema.Annotation {
|
|
||||||
// return []schema.Annotation{
|
|
||||||
// entsql.Annotation{Table: "Name"},
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
Config struct {
|
|
||||||
// A Table is an optional table name defined for the schema.
|
|
||||||
Table string
|
|
||||||
}
|
|
||||||
|
|
||||||
// The Mixin type describes a set of methods that can extend
|
|
||||||
// other methods in the schema without calling them directly.
|
|
||||||
//
|
|
||||||
// type TimeMixin struct {}
|
|
||||||
//
|
|
||||||
// func (TimeMixin) Fields() []ent.Field {
|
|
||||||
// return []ent.Field{
|
|
||||||
// field.Time("created_at").
|
|
||||||
// Immutable().
|
|
||||||
// Default(time.Now),
|
|
||||||
// field.Time("updated_at").
|
|
||||||
// Default(time.Now).
|
|
||||||
// UpdateDefault(time.Now),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// type T struct {
|
|
||||||
// ent.Schema
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// func(T) Mixin() []ent.Mixin {
|
|
||||||
// return []ent.Mixin{
|
|
||||||
// TimeMixin{},
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
Mixin interface {
|
|
||||||
// Fields returns a slice of fields to add to the schema.
|
|
||||||
Fields() []Field
|
|
||||||
// Edges returns a slice of edges to add to the schema.
|
|
||||||
Edges() []Edge
|
|
||||||
// Indexes returns a slice of indexes to add to the schema.
|
|
||||||
Indexes() []Index
|
|
||||||
// Hooks returns a slice of hooks to add to the schema.
|
|
||||||
// Note that mixin hooks are executed before schema hooks.
|
|
||||||
Hooks() []Hook
|
|
||||||
// Policy returns a privacy policy to add to the schema.
|
|
||||||
// Note that mixin policy are executed before schema policy.
|
|
||||||
Policy() Policy
|
|
||||||
// Annotations returns a list of schema annotations to add
|
|
||||||
// to the schema annotations.
|
|
||||||
Annotations() []schema.Annotation
|
|
||||||
}
|
|
||||||
|
|
||||||
// The Policy type defines the write privacy policy of an entity.
|
|
||||||
// The usage for the interface is as follows:
|
|
||||||
//
|
|
||||||
// type T struct {
|
|
||||||
// ent.Schema
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// func(T) Policy() ent.Policy {
|
|
||||||
// return privacy.AlwaysAllowReadWrite()
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
Policy interface {
|
|
||||||
EvalMutation(context.Context, Mutation) error
|
|
||||||
EvalQuery(context.Context, Query) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// Schema is the default implementation for the schema Interface.
|
|
||||||
// It can be embedded in end-user schemas as follows:
|
|
||||||
//
|
|
||||||
// type T struct {
|
|
||||||
// ent.Schema
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
Schema struct {
|
|
||||||
Interface
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
// Fields of the schema.
|
|
||||||
func (Schema) Fields() []Field { return nil }
|
|
||||||
|
|
||||||
// Edges of the schema.
|
|
||||||
func (Schema) Edges() []Edge { return nil }
|
|
||||||
|
|
||||||
// Indexes of the schema.
|
|
||||||
func (Schema) Indexes() []Index { return nil }
|
|
||||||
|
|
||||||
// Config of the schema.
|
|
||||||
func (Schema) Config() Config { return Config{} }
|
|
||||||
|
|
||||||
// Mixin of the schema.
|
|
||||||
func (Schema) Mixin() []Mixin { return nil }
|
|
||||||
|
|
||||||
// Hooks of the schema.
|
|
||||||
func (Schema) Hooks() []Hook { return nil }
|
|
||||||
|
|
||||||
// Policy of the schema.
|
|
||||||
func (Schema) Policy() Policy { return nil }
|
|
||||||
|
|
||||||
// Annotations of the schema.
|
|
||||||
func (Schema) Annotations() []schema.Annotation { return nil }
|
|
||||||
|
|
||||||
type (
|
|
||||||
// Value represents a value returned by ent.
|
|
||||||
Value interface{}
|
|
||||||
// Query represents an ent query builder.
|
|
||||||
Query interface{}
|
|
||||||
// Mutation represents an operation that mutate the graph.
|
|
||||||
// For example, adding a new node, updating many, or dropping
|
|
||||||
// data. The implementation is generated by entc (ent codegen).
|
|
||||||
Mutation interface {
|
|
||||||
// Op returns the operation name generated by entc.
|
|
||||||
Op() Op
|
|
||||||
// Type returns the schema type for this mutation.
|
|
||||||
Type() string
|
|
||||||
|
|
||||||
// Fields returns all fields that were changed during
|
|
||||||
// this mutation. Note that, in order to get all numeric
|
|
||||||
// fields that were in/decremented, call AddedFields().
|
|
||||||
Fields() []string
|
|
||||||
// Field returns the value of a field with the given name.
|
|
||||||
// The second boolean value indicates that this field was
|
|
||||||
// not set, or was not defined in the schema.
|
|
||||||
Field(name string) (Value, bool)
|
|
||||||
// SetField sets the value for the given name. It returns an
|
|
||||||
// error if the field is not defined in the schema, or if the
|
|
||||||
// type mismatch the field type.
|
|
||||||
SetField(name string, value Value) error
|
|
||||||
|
|
||||||
// AddedFields returns all numeric fields that were incremented
|
|
||||||
// or decremented during this mutation.
|
|
||||||
AddedFields() []string
|
|
||||||
// AddedField returns the numeric value that was in/decremented
|
|
||||||
// from a field with the given name. The second value indicates
|
|
||||||
// that this field was not set, or was not define in the schema.
|
|
||||||
AddedField(name string) (Value, bool)
|
|
||||||
// AddField adds the value for the given name. It returns an
|
|
||||||
// error if the field is not defined in the schema, or if the
|
|
||||||
// type mismatch the field type.
|
|
||||||
AddField(name string, value Value) error
|
|
||||||
|
|
||||||
// ClearedFields returns all nullable fields that were cleared
|
|
||||||
// during this mutation.
|
|
||||||
ClearedFields() []string
|
|
||||||
// FieldCleared returns a bool indicates if this field was
|
|
||||||
// cleared in this mutation.
|
|
||||||
FieldCleared(name string) bool
|
|
||||||
// ClearField clears the value for the given name. It returns an
|
|
||||||
// error if the field is not defined in the schema.
|
|
||||||
ClearField(name string) error
|
|
||||||
|
|
||||||
// ResetField resets all changes in the mutation regarding the
|
|
||||||
// given field name. It returns an error if the field is not
|
|
||||||
// defined in the schema.
|
|
||||||
ResetField(name string) error
|
|
||||||
|
|
||||||
// AddedEdges returns all edge names that were set/added in this
|
|
||||||
// mutation.
|
|
||||||
AddedEdges() []string
|
|
||||||
// AddedIDs returns all ids (to other nodes) that were added for
|
|
||||||
// the given edge name.
|
|
||||||
AddedIDs(name string) []Value
|
|
||||||
|
|
||||||
// RemovedEdges returns all edge names that were removed in this
|
|
||||||
// mutation.
|
|
||||||
RemovedEdges() []string
|
|
||||||
// RemovedIDs returns all ids (to other nodes) that were removed for
|
|
||||||
// the given edge name.
|
|
||||||
RemovedIDs(name string) []Value
|
|
||||||
|
|
||||||
// ClearedEdges returns all edge names that were cleared in this
|
|
||||||
// mutation.
|
|
||||||
ClearedEdges() []string
|
|
||||||
// EdgeCleared returns a bool indicates if this edge was
|
|
||||||
// cleared in this mutation.
|
|
||||||
EdgeCleared(name string) bool
|
|
||||||
// ClearEdge clears the value for the given name. It returns an
|
|
||||||
// error if the edge name is not defined in the schema.
|
|
||||||
ClearEdge(name string) error
|
|
||||||
|
|
||||||
// ResetEdge resets all changes in the mutation regarding the
|
|
||||||
// given edge name. It returns an error if the edge is not
|
|
||||||
// defined in the schema.
|
|
||||||
ResetEdge(name string) error
|
|
||||||
|
|
||||||
// OldField returns the old value of the field from the database.
|
|
||||||
// An error is returned if the mutation operation is not UpdateOne,
|
|
||||||
// or the query to the database was failed.
|
|
||||||
OldField(ctx context.Context, name string) (Value, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mutator is the interface that wraps the Mutate method.
|
|
||||||
Mutator interface {
|
|
||||||
// Mutate apply the given mutation on the graph.
|
|
||||||
Mutate(context.Context, Mutation) (Value, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// The MutateFunc type is an adapter to allow the use of ordinary
|
|
||||||
// function as mutator. If f is a function with the appropriate signature,
|
|
||||||
// MutateFunc(f) is a Mutator that calls f.
|
|
||||||
MutateFunc func(context.Context, Mutation) (Value, error)
|
|
||||||
|
|
||||||
// Hook defines the "mutation middleware". A function that gets a Mutator
|
|
||||||
// and returns a Mutator. For example:
|
|
||||||
//
|
|
||||||
// hook := func(next ent.Mutator) ent.Mutator {
|
|
||||||
// return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
|
|
||||||
// fmt.Printf("Type: %s, Operation: %s, ConcreteType: %T\n", m.Type(), m.Op(), m)
|
|
||||||
// return next.Mutate(ctx, m)
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
Hook func(Mutator) Mutator
|
|
||||||
)
|
|
||||||
|
|
||||||
// Mutate calls f(ctx, m).
|
|
||||||
func (f MutateFunc) Mutate(ctx context.Context, m Mutation) (Value, error) {
|
|
||||||
return f(ctx, m)
|
|
||||||
}
|
|
||||||
|
|
||||||
// An Op represents a mutation operation.
|
|
||||||
type Op uint
|
|
||||||
|
|
||||||
// Mutation operations.
|
|
||||||
const (
|
|
||||||
OpCreate Op = 1 << iota // node creation.
|
|
||||||
OpUpdate // update nodes by predicate (if any).
|
|
||||||
OpUpdateOne // update one node.
|
|
||||||
OpDelete // delete nodes by predicate (if any).
|
|
||||||
OpDeleteOne // delete one node.
|
|
||||||
)
|
|
||||||
|
|
||||||
// Is reports whether o is match the given operation.
|
|
||||||
func (i Op) Is(o Op) bool { return i&o != 0 }
|
|
||||||
|
|
||||||
//go:generate go run golang.org/x/tools/cmd/stringer -type Op
|
|
@ -1,43 +0,0 @@
|
|||||||
// Code generated by "stringer -type Op"; DO NOT EDIT.
|
|
||||||
|
|
||||||
package ent
|
|
||||||
|
|
||||||
import "strconv"
|
|
||||||
|
|
||||||
func _() {
|
|
||||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
|
||||||
// Re-run the stringer command to generate them again.
|
|
||||||
var x [1]struct{}
|
|
||||||
_ = x[OpCreate-1]
|
|
||||||
_ = x[OpUpdate-2]
|
|
||||||
_ = x[OpUpdateOne-4]
|
|
||||||
_ = x[OpDelete-8]
|
|
||||||
_ = x[OpDeleteOne-16]
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
|
||||||
_Op_name_0 = "OpCreateOpUpdate"
|
|
||||||
_Op_name_1 = "OpUpdateOne"
|
|
||||||
_Op_name_2 = "OpDelete"
|
|
||||||
_Op_name_3 = "OpDeleteOne"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
_Op_index_0 = [...]uint8{0, 8, 16}
|
|
||||||
)
|
|
||||||
|
|
||||||
func (i Op) String() string {
|
|
||||||
switch {
|
|
||||||
case 1 <= i && i <= 2:
|
|
||||||
i -= 1
|
|
||||||
return _Op_name_0[_Op_index_0[i]:_Op_index_0[i+1]]
|
|
||||||
case i == 4:
|
|
||||||
return _Op_name_1
|
|
||||||
case i == 8:
|
|
||||||
return _Op_name_2
|
|
||||||
case i == 16:
|
|
||||||
return _Op_name_3
|
|
||||||
default:
|
|
||||||
return "Op(" + strconv.FormatInt(int64(i), 10) + ")"
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,49 +0,0 @@
|
|||||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
|
||||||
// This source code is licensed under the Apache 2.0 license found
|
|
||||||
// in the LICENSE file in the root directory of this source tree.
|
|
||||||
|
|
||||||
package edge
|
|
||||||
|
|
||||||
import "entgo.io/ent/schema"
|
|
||||||
|
|
||||||
// Annotation is a builtin schema annotation for
|
|
||||||
// configuring the edges' behavior in codegen.
|
|
||||||
type Annotation struct {
|
|
||||||
// The StructTag option allows overriding the struct-tag
|
|
||||||
// of the `Edges` field in the generated entity. For example:
|
|
||||||
//
|
|
||||||
// edge.Annotation{
|
|
||||||
// StructTag: `json: "pet_edges"`
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
StructTag string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Name describes the annotation name.
|
|
||||||
func (Annotation) Name() string {
|
|
||||||
return "Edges"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Merge implements the schema.Merger interface.
|
|
||||||
func (a Annotation) Merge(other schema.Annotation) schema.Annotation {
|
|
||||||
var ant Annotation
|
|
||||||
switch other := other.(type) {
|
|
||||||
case Annotation:
|
|
||||||
ant = other
|
|
||||||
case *Annotation:
|
|
||||||
if other != nil {
|
|
||||||
ant = *other
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
if tag := ant.StructTag; tag != "" {
|
|
||||||
a.StructTag = tag
|
|
||||||
}
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
_ schema.Annotation = (*Annotation)(nil)
|
|
||||||
_ schema.Merger = (*Annotation)(nil)
|
|
||||||
)
|
|
@ -1,270 +0,0 @@
|
|||||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
|
||||||
// This source code is licensed under the Apache 2.0 license found
|
|
||||||
// in the LICENSE file in the root directory of this source tree.
|
|
||||||
|
|
||||||
package edge
|
|
||||||
|
|
||||||
import (
|
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"entgo.io/ent/schema"
|
|
||||||
)
|
|
||||||
|
|
||||||
// A Descriptor for edge configuration.
|
|
||||||
type Descriptor struct {
|
|
||||||
Tag string // struct tag.
|
|
||||||
Type string // edge type.
|
|
||||||
Name string // edge name.
|
|
||||||
Field string // edge field name (e.g. foreign-key).
|
|
||||||
RefName string // ref name; inverse only.
|
|
||||||
Ref *Descriptor // edge reference; to/from of the same type.
|
|
||||||
Through *struct{ N, T string } // through type and name.
|
|
||||||
Unique bool // unique edge.
|
|
||||||
Inverse bool // inverse edge.
|
|
||||||
Required bool // required on creation.
|
|
||||||
StorageKey *StorageKey // optional storage-key configuration.
|
|
||||||
Annotations []schema.Annotation // edge annotations.
|
|
||||||
Comment string // edge comment.
|
|
||||||
}
|
|
||||||
|
|
||||||
// To defines an association edge between two vertices.
|
|
||||||
func To(name string, t interface{}) *assocBuilder {
|
|
||||||
return &assocBuilder{desc: &Descriptor{Name: name, Type: typ(t)}}
|
|
||||||
}
|
|
||||||
|
|
||||||
// From represents a reversed-edge between two vertices that has a back-reference to its source edge.
|
|
||||||
func From(name string, t interface{}) *inverseBuilder {
|
|
||||||
return &inverseBuilder{desc: &Descriptor{Name: name, Type: typ(t), Inverse: true}}
|
|
||||||
}
|
|
||||||
|
|
||||||
func typ(t interface{}) string {
|
|
||||||
if rt := reflect.TypeOf(t); rt.NumIn() > 0 {
|
|
||||||
return rt.In(0).Name()
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// assocBuilder is the builder for assoc edges.
|
|
||||||
type assocBuilder struct {
|
|
||||||
desc *Descriptor
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unique sets the edge type to be unique. Basically, it limits the edge to be one of the two:
|
|
||||||
// one2one or one2many. one2one applied if the inverse-edge is also unique.
|
|
||||||
func (b *assocBuilder) Unique() *assocBuilder {
|
|
||||||
b.desc.Unique = true
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Required indicates that this edge is a required field on creation.
|
|
||||||
// Unlike fields, edges are optional by default.
|
|
||||||
func (b *assocBuilder) Required() *assocBuilder {
|
|
||||||
b.desc.Required = true
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// StructTag sets the struct tag of the assoc edge.
|
|
||||||
func (b *assocBuilder) StructTag(s string) *assocBuilder {
|
|
||||||
b.desc.Tag = s
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// From creates an inverse-edge with the same type.
|
|
||||||
func (b *assocBuilder) From(name string) *inverseBuilder {
|
|
||||||
return &inverseBuilder{desc: &Descriptor{Name: name, Type: b.desc.Type, Inverse: true, Ref: b.desc}}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Field is used to bind an edge (with a foreign-key) to a field in the schema.
|
|
||||||
//
|
|
||||||
// field.Int("owner_id").
|
|
||||||
// Optional()
|
|
||||||
//
|
|
||||||
// edge.To("owner", User.Type).
|
|
||||||
// Field("owner_id").
|
|
||||||
// Unique(),
|
|
||||||
//
|
|
||||||
func (b *assocBuilder) Field(f string) *assocBuilder {
|
|
||||||
b.desc.Field = f
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Through allows setting an "edge schema" to interact explicitly with M2M edges.
|
|
||||||
//
|
|
||||||
// edge.To("friends", User.Type).
|
|
||||||
// Through("friendships", Friendship.Type)
|
|
||||||
//
|
|
||||||
func (b *assocBuilder) Through(name string, t interface{}) *assocBuilder {
|
|
||||||
b.desc.Through = &struct{ N, T string }{N: name, T: typ(t)}
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Comment used to put annotations on the schema.
|
|
||||||
func (b *assocBuilder) Comment(c string) *assocBuilder {
|
|
||||||
b.desc.Comment = c
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// StorageKey sets the storage key of the edge.
|
|
||||||
//
|
|
||||||
// edge.To("groups", Group.Type).
|
|
||||||
// StorageKey(edge.Table("user_groups"), edge.Columns("user_id", "group_id"))
|
|
||||||
//
|
|
||||||
func (b *assocBuilder) StorageKey(opts ...StorageOption) *assocBuilder {
|
|
||||||
if b.desc.StorageKey == nil {
|
|
||||||
b.desc.StorageKey = &StorageKey{}
|
|
||||||
}
|
|
||||||
for i := range opts {
|
|
||||||
opts[i](b.desc.StorageKey)
|
|
||||||
}
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Annotations adds a list of annotations to the edge object to be used by
|
|
||||||
// codegen extensions.
|
|
||||||
//
|
|
||||||
// edge.To("pets", Pet.Type).
|
|
||||||
// Annotations(entgql.Bind())
|
|
||||||
//
|
|
||||||
func (b *assocBuilder) Annotations(annotations ...schema.Annotation) *assocBuilder {
|
|
||||||
b.desc.Annotations = append(b.desc.Annotations, annotations...)
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Descriptor implements the ent.Descriptor interface.
|
|
||||||
func (b *assocBuilder) Descriptor() *Descriptor {
|
|
||||||
return b.desc
|
|
||||||
}
|
|
||||||
|
|
||||||
// inverseBuilder is the builder for inverse edges.
|
|
||||||
type inverseBuilder struct {
|
|
||||||
desc *Descriptor
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ref sets the referenced-edge of this inverse edge.
|
|
||||||
func (b *inverseBuilder) Ref(ref string) *inverseBuilder {
|
|
||||||
b.desc.RefName = ref
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unique sets the edge type to be unique. Basically, it limits the edge to be one of the two:
|
|
||||||
// one2one or one2many. one2one applied if the inverse-edge is also unique.
|
|
||||||
func (b *inverseBuilder) Unique() *inverseBuilder {
|
|
||||||
b.desc.Unique = true
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Required indicates that this edge is a required field on creation.
|
|
||||||
// Unlike fields, edges are optional by default.
|
|
||||||
func (b *inverseBuilder) Required() *inverseBuilder {
|
|
||||||
b.desc.Required = true
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// StructTag sets the struct tag of the inverse edge.
|
|
||||||
func (b *inverseBuilder) StructTag(s string) *inverseBuilder {
|
|
||||||
b.desc.Tag = s
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Comment used to put annotations on the schema.
|
|
||||||
func (b *inverseBuilder) Comment(c string) *inverseBuilder {
|
|
||||||
b.desc.Comment = c
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Field is used to bind an edge (with a foreign-key) to a field in the schema.
|
|
||||||
//
|
|
||||||
// field.Int("owner_id").
|
|
||||||
// Optional()
|
|
||||||
//
|
|
||||||
// edge.From("owner", User.Type).
|
|
||||||
// Ref("pets").
|
|
||||||
// Field("owner_id").
|
|
||||||
// Unique(),
|
|
||||||
//
|
|
||||||
func (b *inverseBuilder) Field(f string) *inverseBuilder {
|
|
||||||
b.desc.Field = f
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Through allows setting an "edge schema" to interact explicitly with M2M edges.
|
|
||||||
//
|
|
||||||
// edge.From("liked_users", User.Type).
|
|
||||||
// Ref("liked_tweets").
|
|
||||||
// Through("likes", TweetLike.Type)
|
|
||||||
//
|
|
||||||
func (b *inverseBuilder) Through(name string, t interface{}) *inverseBuilder {
|
|
||||||
b.desc.Through = &struct{ N, T string }{N: name, T: typ(t)}
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Annotations adds a list of annotations to the edge object to be used by
|
|
||||||
// codegen extensions.
|
|
||||||
//
|
|
||||||
// edge.From("owner", User.Type).
|
|
||||||
// Ref("pets").
|
|
||||||
// Unique().
|
|
||||||
// Annotations(entgql.Bind())
|
|
||||||
//
|
|
||||||
func (b *inverseBuilder) Annotations(annotations ...schema.Annotation) *inverseBuilder {
|
|
||||||
b.desc.Annotations = append(b.desc.Annotations, annotations...)
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Descriptor implements the ent.Descriptor interface.
|
|
||||||
func (b *inverseBuilder) Descriptor() *Descriptor {
|
|
||||||
return b.desc
|
|
||||||
}
|
|
||||||
|
|
||||||
// StorageKey holds the configuration for edge storage-key.
|
|
||||||
type StorageKey struct {
|
|
||||||
Table string // Table or label.
|
|
||||||
Symbols []string // Symbols/names of the foreign-key constraints.
|
|
||||||
Columns []string // Foreign-key columns.
|
|
||||||
}
|
|
||||||
|
|
||||||
// StorageOption allows for setting the storage configuration using functional options.
|
|
||||||
type StorageOption func(*StorageKey)
|
|
||||||
|
|
||||||
// Table sets the table name option for M2M edges.
|
|
||||||
func Table(name string) StorageOption {
|
|
||||||
return func(key *StorageKey) {
|
|
||||||
key.Table = name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Symbol sets the symbol/name of the foreign-key constraint for O2O, O2M and M2O edges.
|
|
||||||
// Note that, for M2M edges (2 columns and 2 constraints), use the edge.Symbols option.
|
|
||||||
func Symbol(symbol string) StorageOption {
|
|
||||||
return func(key *StorageKey) {
|
|
||||||
key.Symbols = []string{symbol}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Symbols sets the symbol/name of the foreign-key constraints for M2M edges.
|
|
||||||
// The 1st column defines the name of the "To" edge, and the 2nd defines
|
|
||||||
// the name of the "From" edge (inverse edge).
|
|
||||||
// Note that, for O2O, O2M and M2O edges, use the edge.Symbol option.
|
|
||||||
func Symbols(to, from string) StorageOption {
|
|
||||||
return func(key *StorageKey) {
|
|
||||||
key.Symbols = []string{to, from}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Column sets the foreign-key column name option for O2O, O2M and M2O edges.
|
|
||||||
// Note that, for M2M edges (2 columns), use the edge.Columns option.
|
|
||||||
func Column(name string) StorageOption {
|
|
||||||
return func(key *StorageKey) {
|
|
||||||
key.Columns = []string{name}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Columns sets the foreign-key column names option for M2M edges.
|
|
||||||
// The 1st column defines the name of the "To" edge, and the 2nd defines
|
|
||||||
// the name of the "From" edge (inverse edge).
|
|
||||||
// Note that, for O2O, O2M and M2O edges, use the edge.Column option.
|
|
||||||
func Columns(to, from string) StorageOption {
|
|
||||||
return func(key *StorageKey) {
|
|
||||||
key.Columns = []string{to, from}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,81 +0,0 @@
|
|||||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
|
||||||
// This source code is licensed under the Apache 2.0 license found
|
|
||||||
// in the LICENSE file in the root directory of this source tree.
|
|
||||||
|
|
||||||
package field
|
|
||||||
|
|
||||||
import "entgo.io/ent/schema"
|
|
||||||
|
|
||||||
// Annotation is a builtin schema annotation for
|
|
||||||
// configuring the schema fields in codegen.
|
|
||||||
type Annotation struct {
|
|
||||||
// The StructTag option allows overriding the struct-tag
|
|
||||||
// of the fields in the generated entity. For example:
|
|
||||||
//
|
|
||||||
// field.Annotation{
|
|
||||||
// StructTag: map[string]string{
|
|
||||||
// "id": `json:"id,omitempty" yaml:"-"`,
|
|
||||||
// },
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
StructTag map[string]string
|
|
||||||
|
|
||||||
// ID defines a multi-field schema identifier. Note,
|
|
||||||
// the annotation is valid only for edge schemas.
|
|
||||||
//
|
|
||||||
// func (TweetLike) Annotations() []schema.Annotation {
|
|
||||||
// return []schema.Annotation{
|
|
||||||
// field.ID("user_id", "tweet_id"),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
ID []string
|
|
||||||
}
|
|
||||||
|
|
||||||
// ID defines a multi-field schema identifier. Note, the
|
|
||||||
// annotation is valid only for edge schemas.
|
|
||||||
//
|
|
||||||
// func (TweetLike) Annotations() []schema.Annotation {
|
|
||||||
// return []schema.Annotation{
|
|
||||||
// field.ID("user_id", "tweet_id"),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
func ID(first, second string, fields ...string) *Annotation {
|
|
||||||
return &Annotation{ID: append([]string{first, second}, fields...)}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Name describes the annotation name.
|
|
||||||
func (Annotation) Name() string {
|
|
||||||
return "Fields"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Merge implements the schema.Merger interface.
|
|
||||||
func (a Annotation) Merge(other schema.Annotation) schema.Annotation {
|
|
||||||
var ant Annotation
|
|
||||||
switch other := other.(type) {
|
|
||||||
case Annotation:
|
|
||||||
ant = other
|
|
||||||
case *Annotation:
|
|
||||||
if other != nil {
|
|
||||||
ant = *other
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
for k, v := range ant.StructTag {
|
|
||||||
if a.StructTag == nil {
|
|
||||||
a.StructTag = make(map[string]string)
|
|
||||||
}
|
|
||||||
a.StructTag[k] = v
|
|
||||||
}
|
|
||||||
if len(ant.ID) > 0 {
|
|
||||||
a.ID = ant.ID
|
|
||||||
}
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ interface {
|
|
||||||
schema.Annotation
|
|
||||||
schema.Merger
|
|
||||||
} = (*Annotation)(nil)
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,244 +0,0 @@
|
|||||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
|
||||||
// This source code is licensed under the Apache 2.0 license found
|
|
||||||
// in the LICENSE file in the root directory of this source tree.
|
|
||||||
|
|
||||||
package field
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"reflect"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// A Type represents a field type.
|
|
||||||
type Type uint8
|
|
||||||
|
|
||||||
// List of field types.
|
|
||||||
const (
|
|
||||||
TypeInvalid Type = iota
|
|
||||||
TypeBool
|
|
||||||
TypeTime
|
|
||||||
TypeJSON
|
|
||||||
TypeUUID
|
|
||||||
TypeBytes
|
|
||||||
TypeEnum
|
|
||||||
TypeString
|
|
||||||
TypeOther
|
|
||||||
TypeInt8
|
|
||||||
TypeInt16
|
|
||||||
TypeInt32
|
|
||||||
TypeInt
|
|
||||||
TypeInt64
|
|
||||||
TypeUint8
|
|
||||||
TypeUint16
|
|
||||||
TypeUint32
|
|
||||||
TypeUint
|
|
||||||
TypeUint64
|
|
||||||
TypeFloat32
|
|
||||||
TypeFloat64
|
|
||||||
endTypes
|
|
||||||
)
|
|
||||||
|
|
||||||
// String returns the string representation of a type.
|
|
||||||
func (t Type) String() string {
|
|
||||||
if t < endTypes {
|
|
||||||
return typeNames[t]
|
|
||||||
}
|
|
||||||
return typeNames[TypeInvalid]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Numeric reports if the given type is a numeric type.
|
|
||||||
func (t Type) Numeric() bool {
|
|
||||||
return t >= TypeInt8 && t < endTypes
|
|
||||||
}
|
|
||||||
|
|
||||||
// Float reports if the given type is a float type.
|
|
||||||
func (t Type) Float() bool {
|
|
||||||
return t == TypeFloat32 || t == TypeFloat64
|
|
||||||
}
|
|
||||||
|
|
||||||
// Integer reports if the given type is an integral type.
|
|
||||||
func (t Type) Integer() bool {
|
|
||||||
return t.Numeric() && !t.Float()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Valid reports if the given type if known type.
|
|
||||||
func (t Type) Valid() bool {
|
|
||||||
return t > TypeInvalid && t < endTypes
|
|
||||||
}
|
|
||||||
|
|
||||||
// ConstName returns the constant name of an info type.
|
|
||||||
// It's used by entc for printing the constant name in templates.
|
|
||||||
func (t Type) ConstName() string {
|
|
||||||
switch {
|
|
||||||
case !t.Valid():
|
|
||||||
return typeNames[TypeInvalid]
|
|
||||||
case int(t) < len(constNames) && constNames[t] != "":
|
|
||||||
return constNames[t]
|
|
||||||
default:
|
|
||||||
return "Type" + strings.Title(typeNames[t])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TypeInfo holds the information regarding field type.
|
|
||||||
// Used by complex types like JSON and Bytes.
|
|
||||||
type TypeInfo struct {
|
|
||||||
Type Type
|
|
||||||
Ident string
|
|
||||||
PkgPath string // import path.
|
|
||||||
PkgName string // local package name.
|
|
||||||
Nillable bool // slices or pointers.
|
|
||||||
RType *RType
|
|
||||||
}
|
|
||||||
|
|
||||||
// String returns the string representation of a type.
|
|
||||||
func (t TypeInfo) String() string {
|
|
||||||
switch {
|
|
||||||
case t.Ident != "":
|
|
||||||
return t.Ident
|
|
||||||
case t.Type < endTypes:
|
|
||||||
return typeNames[t.Type]
|
|
||||||
default:
|
|
||||||
return typeNames[TypeInvalid]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Valid reports if the given type if known type.
|
|
||||||
func (t TypeInfo) Valid() bool {
|
|
||||||
return t.Type.Valid()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Numeric reports if the given type is a numeric type.
|
|
||||||
func (t TypeInfo) Numeric() bool {
|
|
||||||
return t.Type.Numeric()
|
|
||||||
}
|
|
||||||
|
|
||||||
// ConstName returns the const name of the info type.
|
|
||||||
func (t TypeInfo) ConstName() string {
|
|
||||||
return t.Type.ConstName()
|
|
||||||
}
|
|
||||||
|
|
||||||
// ValueScanner indicates if this type implements the ValueScanner interface.
|
|
||||||
func (t TypeInfo) ValueScanner() bool {
|
|
||||||
return t.RType.Implements(valueScannerType)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Valuer indicates if this type implements the driver.Valuer interface.
|
|
||||||
func (t TypeInfo) Valuer() bool {
|
|
||||||
return t.RType.Implements(valuerType)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Comparable reports whether values of this type are comparable.
|
|
||||||
func (t TypeInfo) Comparable() bool {
|
|
||||||
switch t.Type {
|
|
||||||
case TypeBool, TypeTime, TypeUUID, TypeEnum, TypeString:
|
|
||||||
return true
|
|
||||||
case TypeOther:
|
|
||||||
// Always accept custom types as comparable on the database side.
|
|
||||||
// In the future, we should consider adding an interface to let
|
|
||||||
// custom types tell if they are comparable or not (see #1304).
|
|
||||||
return true
|
|
||||||
default:
|
|
||||||
return t.Numeric()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var stringerType = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
|
|
||||||
|
|
||||||
// Stringer indicates if this type implements the Stringer interface.
|
|
||||||
func (t TypeInfo) Stringer() bool {
|
|
||||||
return t.RType.Implements(stringerType)
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
typeNames = [...]string{
|
|
||||||
TypeInvalid: "invalid",
|
|
||||||
TypeBool: "bool",
|
|
||||||
TypeTime: "time.Time",
|
|
||||||
TypeJSON: "json.RawMessage",
|
|
||||||
TypeUUID: "[16]byte",
|
|
||||||
TypeBytes: "[]byte",
|
|
||||||
TypeEnum: "string",
|
|
||||||
TypeString: "string",
|
|
||||||
TypeOther: "other",
|
|
||||||
TypeInt: "int",
|
|
||||||
TypeInt8: "int8",
|
|
||||||
TypeInt16: "int16",
|
|
||||||
TypeInt32: "int32",
|
|
||||||
TypeInt64: "int64",
|
|
||||||
TypeUint: "uint",
|
|
||||||
TypeUint8: "uint8",
|
|
||||||
TypeUint16: "uint16",
|
|
||||||
TypeUint32: "uint32",
|
|
||||||
TypeUint64: "uint64",
|
|
||||||
TypeFloat32: "float32",
|
|
||||||
TypeFloat64: "float64",
|
|
||||||
}
|
|
||||||
constNames = [...]string{
|
|
||||||
TypeJSON: "TypeJSON",
|
|
||||||
TypeUUID: "TypeUUID",
|
|
||||||
TypeTime: "TypeTime",
|
|
||||||
TypeEnum: "TypeEnum",
|
|
||||||
TypeBytes: "TypeBytes",
|
|
||||||
TypeOther: "TypeOther",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
// RType holds a serializable reflect.Type information of
|
|
||||||
// Go object. Used by the entc package.
|
|
||||||
type RType struct {
|
|
||||||
Name string // reflect.Type.Name
|
|
||||||
Ident string // reflect.Type.String
|
|
||||||
Kind reflect.Kind
|
|
||||||
PkgPath string
|
|
||||||
Methods map[string]struct{ In, Out []*RType }
|
|
||||||
// Used only for in-package checks.
|
|
||||||
rtype reflect.Type
|
|
||||||
}
|
|
||||||
|
|
||||||
// TypeEqual reports if the underlying type is equal to the RType (after pointer indirections).
|
|
||||||
func (r *RType) TypeEqual(t reflect.Type) bool {
|
|
||||||
tv := indirect(t)
|
|
||||||
return r.Name == tv.Name() && r.Kind == t.Kind() && r.PkgPath == tv.PkgPath()
|
|
||||||
}
|
|
||||||
|
|
||||||
// RType returns the string value of the indirect reflect.Type.
|
|
||||||
func (r *RType) String() string {
|
|
||||||
if r.rtype != nil {
|
|
||||||
return r.rtype.String()
|
|
||||||
}
|
|
||||||
return r.Ident
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsPtr reports if the reflect-type is a pointer type.
|
|
||||||
func (r *RType) IsPtr() bool {
|
|
||||||
return r != nil && r.Kind == reflect.Ptr
|
|
||||||
}
|
|
||||||
|
|
||||||
// Implements reports whether the RType ~implements the given interface type.
|
|
||||||
func (r *RType) Implements(typ reflect.Type) bool {
|
|
||||||
if r == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
n := typ.NumMethod()
|
|
||||||
for i := 0; i < n; i++ {
|
|
||||||
m0 := typ.Method(i)
|
|
||||||
m1, ok := r.Methods[m0.Name]
|
|
||||||
if !ok || len(m1.In) != m0.Type.NumIn() || len(m1.Out) != m0.Type.NumOut() {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
in := m0.Type.NumIn()
|
|
||||||
for j := 0; j < in; j++ {
|
|
||||||
if !m1.In[j].TypeEqual(m0.Type.In(j)) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
out := m0.Type.NumOut()
|
|
||||||
for j := 0; j < out; j++ {
|
|
||||||
if !m1.Out[j].TypeEqual(m0.Type.Out(j)) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
@ -1,121 +0,0 @@
|
|||||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
|
||||||
// This source code is licensed under the Apache 2.0 license found
|
|
||||||
// in the LICENSE file in the root directory of this source tree.
|
|
||||||
|
|
||||||
package index
|
|
||||||
|
|
||||||
import "entgo.io/ent/schema"
|
|
||||||
|
|
||||||
// A Descriptor for index configuration.
|
|
||||||
type Descriptor struct {
|
|
||||||
Unique bool // unique index.
|
|
||||||
Edges []string // edge columns.
|
|
||||||
Fields []string // field columns.
|
|
||||||
StorageKey string // custom index name.
|
|
||||||
Annotations []schema.Annotation // index annotations.
|
|
||||||
}
|
|
||||||
|
|
||||||
// Builder for indexes on vertex columns and edges in the graph.
|
|
||||||
type Builder struct {
|
|
||||||
desc *Descriptor
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fields creates an index on the given vertex fields.
|
|
||||||
// Note that indexes are implemented only for SQL dialects, and does not support gremlin.
|
|
||||||
//
|
|
||||||
// func (T) Indexes() []ent.Index {
|
|
||||||
//
|
|
||||||
// // Unique index on 2 fields.
|
|
||||||
// index.Fields("first", "last").
|
|
||||||
// Unique(),
|
|
||||||
//
|
|
||||||
// // Unique index of field under specific edge.
|
|
||||||
// index.Fields("name").
|
|
||||||
// Edges("parent").
|
|
||||||
// Unique(),
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
func Fields(fields ...string) *Builder {
|
|
||||||
return &Builder{desc: &Descriptor{Fields: fields}}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Edges creates an index on the given vertex edge fields.
|
|
||||||
// Note that indexes are implemented only for SQL dialects, and does not support gremlin.
|
|
||||||
//
|
|
||||||
// func (T) Indexes() []ent.Index {
|
|
||||||
//
|
|
||||||
// // Unique index of field under 2 edges.
|
|
||||||
// index.Fields("name").
|
|
||||||
// Edges("parent", "type").
|
|
||||||
// Unique(),
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
func Edges(edges ...string) *Builder {
|
|
||||||
return &Builder{desc: &Descriptor{Edges: edges}}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fields sets the fields of the index.
|
|
||||||
//
|
|
||||||
// func (T) Indexes() []ent.Index {
|
|
||||||
//
|
|
||||||
// // Unique "name" and "age" fields under the "parent" edge.
|
|
||||||
// index.Edges("parent").
|
|
||||||
// Fields("name", "age").
|
|
||||||
// Unique(),
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
func (b *Builder) Fields(fields ...string) *Builder {
|
|
||||||
b.desc.Fields = fields
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Edges sets the fields index to be unique under the set of edges (sub-graph). For example:
|
|
||||||
//
|
|
||||||
// func (T) Indexes() []ent.Index {
|
|
||||||
//
|
|
||||||
// // Unique "name" field under the "parent" edge.
|
|
||||||
// index.Fields("name").
|
|
||||||
// Edges("parent").
|
|
||||||
// Unique(),
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
func (b *Builder) Edges(edges ...string) *Builder {
|
|
||||||
b.desc.Edges = edges
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unique sets the index to be a unique index.
|
|
||||||
// Note that defining a uniqueness on optional fields won't prevent
|
|
||||||
// duplicates if one of the column contains NULL values.
|
|
||||||
func (b *Builder) Unique() *Builder {
|
|
||||||
b.desc.Unique = true
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// StorageKey sets the storage key of the index. In SQL dialects, it's the index name.
|
|
||||||
func (b *Builder) StorageKey(key string) *Builder {
|
|
||||||
b.desc.StorageKey = key
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Annotations adds a list of annotations to the index object to be used by codegen extensions.
|
|
||||||
//
|
|
||||||
// func (T) Indexes() []ent.Index {
|
|
||||||
//
|
|
||||||
// // Partial index on name where the entity is not deleted.
|
|
||||||
// index.Fields("name").
|
|
||||||
// Annotations(entsql.Prefix(100))
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
func (b *Builder) Annotations(annotations ...schema.Annotation) *Builder {
|
|
||||||
b.desc.Annotations = append(b.desc.Annotations, annotations...)
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Descriptor implements the ent.Descriptor interface.
|
|
||||||
func (b *Builder) Descriptor() *Descriptor {
|
|
||||||
return b.desc
|
|
||||||
}
|
|
@ -1,24 +0,0 @@
|
|||||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
|
||||||
// This source code is licensed under the Apache 2.0 license found
|
|
||||||
// in the LICENSE file in the root directory of this source tree.
|
|
||||||
|
|
||||||
package schema
|
|
||||||
|
|
||||||
// Annotation is used to attach arbitrary metadata to the schema objects in codegen.
|
|
||||||
// The object must be serializable to JSON raw value (e.g. struct, map or slice).
|
|
||||||
//
|
|
||||||
// Template extensions can retrieve this metadata and use it inside their templates.
|
|
||||||
// Read more about it in ent website: https://entgo.io/docs/templates/#annotations.
|
|
||||||
type Annotation interface {
|
|
||||||
// Name defines the name of the annotation to be retrieved by the codegen.
|
|
||||||
Name() string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Merger wraps the single Merge function allows custom annotation to provide
|
|
||||||
// an implementation for merging 2 or more annotations from the same type.
|
|
||||||
//
|
|
||||||
// A common use case is where the same Annotation type is defined both in
|
|
||||||
// mixin.Schema and ent.Schema.
|
|
||||||
type Merger interface {
|
|
||||||
Merge(Annotation) Annotation
|
|
||||||
}
|
|
Loading…
Reference in new issue