├── version.txt ├── .github ├── release-trigger.yml ├── release-please.yml ├── CODEOWNERS ├── renovate.json ├── sync-repo-settings.yaml └── workflows │ └── ci.yaml ├── go.mod ├── SECURITY.md ├── .editorconfig ├── CHANGELOG.md ├── go.sum ├── adapters ├── doc.go ├── internal_test.go ├── month.go ├── month_test.go ├── fraction.go ├── fraction_test.go ├── date_test.go ├── date.go ├── decimal.go ├── color_test.go ├── decimal_test.go ├── datetime.go ├── datetime_test.go └── color.go ├── README.md ├── CONTRIBUTING.md ├── CODE_OF_CONDUCT.md └── LICENSE /version.txt: -------------------------------------------------------------------------------- 1 | 1.0.1 2 | -------------------------------------------------------------------------------- /.github/release-trigger.yml: -------------------------------------------------------------------------------- 1 | enabled: true 2 | multiScmName: go-type-adapters 3 | -------------------------------------------------------------------------------- /.github/release-please.yml: -------------------------------------------------------------------------------- 1 | --- 2 | releaseType: simple 3 | handleGHRelease: true 4 | primaryBranch: main 5 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # @lukesneeringer originally wrote this. 2 | # The Yoshi/ACT folks have graciously agreed to help. 3 | * @googleapis/yoshi-go @googleapis/actools-go 4 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/googleapis/go-type-adapters 2 | 3 | go 1.21 4 | 5 | require ( 6 | github.com/golang/protobuf v1.5.4 7 | google.golang.org/genproto v0.0.0-20241021214115-324edc3d5d38 8 | google.golang.org/protobuf v1.35.1 9 | ) 10 | 11 | require github.com/google/go-cmp v0.6.0 // indirect 12 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security issue, please use [g.co/vulnz](https://g.co/vulnz). 4 | 5 | The Google Security Team will respond within 5 working days of your report on g.co/vulnz. 6 | 7 | We use g.co/vulnz for our intake, and do coordination and disclosure here using GitHub Security Advisory to privately discuss and fix the issue. 8 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: http://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | # Unix-style newlines with a newline ending every file 7 | [*] 8 | end_of_line = lf 9 | insert_final_newline = true 10 | 11 | # Matches multiple files with brace expansion notation 12 | # Set default charset 13 | [*.go] 14 | indent_style = tab 15 | indent_size = 2 16 | trim_trailing_whitespace = true 17 | -------------------------------------------------------------------------------- /.github/renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "config:base", 4 | ":disableDependencyDashboard" 5 | ], 6 | "commitMessagePrefix": "chore: ", 7 | "groupName": "all", 8 | "postUpdateOptions": [ 9 | "gomodTidy" 10 | ], 11 | "rebaseWhen": "behind-base-branch", 12 | "labels": [ 13 | "automerge" 14 | ], 15 | "schedule": "on tuesday", 16 | "force": { 17 | "constraints": { 18 | "go": "1.21" 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /.github/sync-repo-settings.yaml: -------------------------------------------------------------------------------- 1 | rebaseMergeAllowed: false 2 | squashMergeAllowed: true 3 | mergeCommitAllowed: false 4 | branchProtectionRules: 5 | - pattern: main 6 | isAdminEnforced: true 7 | requiredStatusCheckContexts: 8 | - unit-tests (1.19) 9 | - lint 10 | - cla/google 11 | requiredApprovingReviewCount: 1 12 | requiresCodeOwnerReviews: true 13 | requiresStrictStatusChecks: true 14 | permissionRules: 15 | - team: yoshi-go 16 | permission: push 17 | - team: actools-go 18 | permission: push 19 | -------------------------------------------------------------------------------- /.github/workflows/ci.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | name: ci 3 | on: 4 | pull_request: 5 | branches: 6 | - main 7 | jobs: 8 | unit-tests: 9 | runs-on: ubuntu-latest 10 | strategy: 11 | matrix: 12 | go_version: 13 | - "1.19" 14 | steps: 15 | - uses: actions/setup-go@v5 16 | with: 17 | go-version: ${{ matrix.go_version }} 18 | - uses: actions/checkout@v4 19 | - run: go test -p 1 ./... 20 | lint: 21 | runs-on: ubuntu-latest 22 | steps: 23 | - uses: actions/setup-go@v5 24 | with: 25 | go-version: 1.19 26 | - uses: actions/checkout@v4 27 | - name: golangci-lint 28 | uses: golangci/golangci-lint-action@v6 29 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## [1.0.1](https://github.com/googleapis/go-type-adapters/compare/v1.0.0...v1.0.1) (2024-03-14) 4 | 5 | 6 | ### Bug Fixes 7 | 8 | * JSON syntax ([#91](https://github.com/googleapis/go-type-adapters/issues/91)) ([aa9f8a5](https://github.com/googleapis/go-type-adapters/commit/aa9f8a55b7d4574f4ba37ce763c359facef41255)), closes [#49](https://github.com/googleapis/go-type-adapters/issues/49) 9 | 10 | ## 1.0.0 (2021-04-07) 11 | 12 | 13 | ### Features 14 | 15 | * Adapters for Color, Date, DateTime, Decimal, Fraction, Month ([#1](https://www.github.com/googleapis/go-type-adapters/issues/1)) ([67144ff](https://www.github.com/googleapis/go-type-adapters/commit/67144ff619d74444ff926f76fd198c9a616b0faa)) 16 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= 2 | github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= 3 | github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= 4 | github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= 5 | google.golang.org/genproto v0.0.0-20241021214115-324edc3d5d38 h1:Q3nlH8iSQSRUwOskjbcSMcF2jiYMNiQYZ0c2KEJLKKU= 6 | google.golang.org/genproto v0.0.0-20241021214115-324edc3d5d38/go.mod h1:xBI+tzfqGGN2JBeSebfKXFSdBpWVQ7sLW40PTupVRm4= 7 | google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA= 8 | google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= 9 | -------------------------------------------------------------------------------- /adapters/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // package adapters provides helper functions for the google.type protobuf 16 | // messages (Decimal, Fraction, etc.). 17 | package adapters 18 | -------------------------------------------------------------------------------- /adapters/internal_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package adapters 16 | 17 | import "testing" 18 | 19 | // Note: This only works for primitive types; it is a bludgeon for avoiding 20 | // excessively long-winded tests. 21 | func assertEqual(t *testing.T, name string, got, want interface{}) { 22 | t.Helper() 23 | if got != want { 24 | t.Errorf("%s: got %v, want %v.", name, got, want) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /adapters/month.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package adapters 16 | 17 | import ( 18 | "time" 19 | 20 | mpb "google.golang.org/genproto/googleapis/type/month" 21 | ) 22 | 23 | // ToMonth converts a google.type.Month to a golang Month. 24 | func ToMonth(m mpb.Month) time.Month { 25 | return time.Month(m.Number()) 26 | } 27 | 28 | // ToProtoMonth converts a Go Month to a google.type.Month. 29 | func ToProtoMonth(m time.Month) mpb.Month { 30 | return mpb.Month(m) 31 | } 32 | -------------------------------------------------------------------------------- /adapters/month_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package adapters 16 | 17 | import ( 18 | "testing" 19 | "time" 20 | 21 | mpb "google.golang.org/genproto/googleapis/type/month" 22 | ) 23 | 24 | func TestMonth(t *testing.T) { 25 | for m := range mpb.Month_name { 26 | t.Run("Native", func(t *testing.T) { 27 | native := ToMonth(mpb.Month(m)) 28 | assertEqual(t, native.String(), int(native), int(m)) 29 | }) 30 | t.Run("PB", func(t *testing.T) { 31 | pb := ToProtoMonth(time.Month(m)) 32 | assertEqual(t, pb.String(), int(pb), int(m)) 33 | }) 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /adapters/fraction.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package adapters 16 | 17 | import ( 18 | "math/big" 19 | 20 | fpb "google.golang.org/genproto/googleapis/type/fraction" 21 | ) 22 | 23 | // ProtoFractionToRat returns a math/big Rat (rational number) based on the given 24 | // google.type.fraction. 25 | func ProtoFractionToRat(f *fpb.Fraction) *big.Rat { 26 | return big.NewRat(f.GetNumerator(), f.GetDenominator()) 27 | } 28 | 29 | // RatToProtoFraction returns a google.type.Fraction from a math/big Rat. 30 | func RatToProtoFraction(r *big.Rat) *fpb.Fraction { 31 | return &fpb.Fraction{ 32 | Numerator: r.Num().Int64(), 33 | Denominator: r.Denom().Int64(), 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Go google.type Adapters 2 | 3 | ![ci](https://github.com/googleapis/go-type-adapters/workflows/ci/badge.svg) 4 | ![latest release](https://img.shields.io/github/v/release/googleapis/go-type-adapters) 5 | ![go version](https://img.shields.io/github/go-mod/go-version/googleapis/go-type-adapters) 6 | 7 | This library provides helper functions for converting between the Go 8 | proto messages in `google.type` (as found in [genproto][]) and Go native 9 | types. 10 | 11 | Full docs are at https://pkg.go.dev/github.com/googleapis/go-type-adapters. 12 | 13 | ### Example 14 | 15 | As a simple example, this library can convert between a `google.type.Decimal` 16 | ([proto definition][], [Go docs][]) and a Go [big.Float][]: 17 | 18 | ```go 19 | import ( 20 | "github.com/googleapis/go-type-adapters/adapters" 21 | dpb "google.golang.org/genproto/type/decimal" 22 | ) 23 | 24 | func main() { 25 | decimal := &dpb.Decimal{Value: "12345.678"} 26 | flt, err := adapters.DecimalToFloat(decimal) 27 | if err != nil { 28 | panic(err) 29 | } 30 | // flt is a Go *big.Float and can be used as such... 31 | } 32 | ``` 33 | 34 | [genproto]: https://pkg.go.dev/google.golang.org/genproto 35 | [proto definition]: https://github.com/googleapis/googleapis/blob/master/google/type/decimal.proto 36 | [go docs]: https://pkg.go.dev/google.golang.org/genproto/googleapis/type/decimal 37 | [big.float]: https://golang.org/pkg/math/big/#Float 38 | 39 | ## License 40 | 41 | This software is made available under the [Apache 2.0][] license. 42 | 43 | [apache 2.0]: https://www.apache.org/licenses/LICENSE-2.0 44 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Want to contribute? Great! First, read this page (including the small print at 4 | the end). 5 | 6 | ### Before you contribute 7 | 8 | Before we can use your code, you must sign the Google Individual 9 | [Contributor License Agreement][cla] (CLA), which you can do online. The CLA is 10 | necessary mainly because you own the copyright to your changes, even after your 11 | contribution becomes part of our codebase, so we need your permission to use 12 | and distribute your code. 13 | 14 | We also need to be sure of various other things—for instance, that you'll tell 15 | us if you know that your code infringes on other people's patents. You don't 16 | have to sign the CLA until after you've submitted your code for review and a 17 | member has approved it, but you must do it before we can put your code into our 18 | codebase. 19 | 20 | Before you start working on a larger contribution, you should get in touch with 21 | us first through the issue tracker with your idea so that we can help out and 22 | possibly guide you. Coordinating up front makes it much easier to avoid 23 | frustration later on. 24 | 25 | ### Code reviews 26 | 27 | All submissions, including submissions by project members, require review. We 28 | use GitHub pull requests for this purpose. 29 | 30 | ### The small print 31 | 32 | Contributions made by corporations are covered by a different agreement than 33 | the one above: the [Software Grant and Corporate CLA][corp-cla]. 34 | 35 | [cla]: https://cla.developers.google.com/about/google-individual 36 | [corp-cla]: https://cla.developers.google.com/about/google-corporate 37 | -------------------------------------------------------------------------------- /adapters/fraction_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package adapters 16 | 17 | import ( 18 | "testing" 19 | 20 | fpb "google.golang.org/genproto/googleapis/type/fraction" 21 | ) 22 | 23 | func TestFractionRat(t *testing.T) { 24 | for _, test := range []struct { 25 | name string 26 | num int64 27 | denom int64 28 | }{ 29 | {"Terminating Decimal", 1, 5}, 30 | {"Non-terminating Decimal", 1, 3}, 31 | } { 32 | t.Run(test.name, func(t *testing.T) { 33 | fraction := &fpb.Fraction{ 34 | Numerator: test.num, 35 | Denominator: test.denom, 36 | } 37 | rat := ProtoFractionToRat(fraction) 38 | t.Run("ProtoFractionToRat", func(t *testing.T) { 39 | assertEqual(t, "rat.Num", rat.Num().Int64(), test.num) 40 | assertEqual(t, "rat.Denom", rat.Denom().Int64(), test.denom) 41 | }) 42 | t.Run("RatToProtoFraction", func(t *testing.T) { 43 | fraction = RatToProtoFraction(rat) 44 | assertEqual(t, "frac.Num", fraction.GetNumerator(), test.num) 45 | assertEqual(t, "frac.Denom", fraction.GetDenominator(), test.denom) 46 | }) 47 | }) 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /adapters/date_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package adapters 16 | 17 | import ( 18 | "testing" 19 | "time" 20 | 21 | dpb "google.golang.org/genproto/googleapis/type/date" 22 | ) 23 | 24 | func TestDate(t *testing.T) { 25 | for _, test := range []struct { 26 | name string 27 | y, m, d int 28 | }{ 29 | {"NormalDate", 2012, 4, 21}, 30 | {"LongAgo", 1776, 7, 4}, 31 | {"Future", 2032, 4, 21}, 32 | {"FarFuture", 2062, 4, 21}, 33 | } { 34 | t.Run(test.name, func(t *testing.T) { 35 | datePb := &dpb.Date{Year: int32(test.y), Month: int32(test.m), Day: int32(test.d)} 36 | times := map[string]time.Time{ 37 | "local": ProtoDateToLocalTime(datePb), 38 | "utc": ProtoDateToUTCTime(datePb), 39 | } 40 | for k, time := range times { 41 | t.Run(k, func(t *testing.T) { 42 | assertEqual(t, "year", time.Year(), test.y) 43 | assertEqual(t, "month", int(time.Month()), test.m) 44 | assertEqual(t, "day", time.Day(), test.d) 45 | dtPb := TimeToProtoDate(time) 46 | t.Run("ToProto", func(t *testing.T) { 47 | assertEqual(t, "year", dtPb.GetYear(), int32(test.y)) 48 | assertEqual(t, "month", dtPb.GetMonth(), int32(test.m)) 49 | assertEqual(t, "day", dtPb.GetDay(), int32(test.d)) 50 | }) 51 | }) 52 | } 53 | }) 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /adapters/date.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package adapters 16 | 17 | import ( 18 | "time" 19 | 20 | dpb "google.golang.org/genproto/googleapis/type/date" 21 | ) 22 | 23 | // ProtoDateToLocalTime returns a new Time based on the google.type.Date, in 24 | // the system's time zone. 25 | // 26 | // Hours, minues, seconds, and nanoseconds are set to 0. 27 | func ProtoDateToLocalTime(d *dpb.Date) time.Time { 28 | return ProtoDateToTime(d, time.Local) 29 | } 30 | 31 | // ProtoDateToUTCTime returns a new Time based on the google.type.Date, in UTC. 32 | // 33 | // Hours, minutes, seconds, and nanoseconds are set to 0. 34 | func ProtoDateToUTCTime(d *dpb.Date) time.Time { 35 | return ProtoDateToTime(d, time.UTC) 36 | } 37 | 38 | // ProtoDateToTime returns a new Time based on the google.type.Date and provided 39 | // *time.Location. 40 | // 41 | // Hours, minutes, seconds, and nanoseconds are set to 0. 42 | func ProtoDateToTime(d *dpb.Date, l *time.Location) time.Time { 43 | return time.Date(int(d.GetYear()), time.Month(d.GetMonth()), int(d.GetDay()), 0, 0, 0, 0, l) 44 | } 45 | 46 | // TimeToProtoDate returns a new google.type.Date based on the provided time.Time. 47 | // The location is ignored, as is anything more precise than the day. 48 | func TimeToProtoDate(t time.Time) *dpb.Date { 49 | return &dpb.Date{ 50 | Year: int32(t.Year()), 51 | Month: int32(t.Month()), 52 | Day: int32(t.Day()), 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, and in the interest of 4 | fostering an open and welcoming community, we pledge to respect all people who 5 | contribute through reporting issues, posting feature requests, updating 6 | documentation, submitting pull requests or patches, and other activities. 7 | 8 | We are committed to making participation in this project a harassment-free 9 | experience for everyone, regardless of level of experience, gender, gender 10 | identity and expression, sexual orientation, disability, personal appearance, 11 | body size, race, ethnicity, age, religion, or nationality. 12 | 13 | Examples of unacceptable behavior by participants include: 14 | 15 | - The use of sexualized language or imagery 16 | - Personal attacks 17 | - Trolling or insulting/derogatory comments 18 | - Public or private harassment 19 | - Publishing other's private information, such as physical or electronic 20 | addresses, without explicit permission 21 | - Other unethical or unprofessional conduct. 22 | 23 | Project maintainers have the right and responsibility to remove, edit, or 24 | reject comments, commits, code, wiki edits, issues, and other contributions 25 | that are not aligned to this Code of Conduct. By adopting this Code of Conduct, 26 | project maintainers commit themselves to fairly and consistently applying these 27 | principles to every aspect of managing this project. Project maintainers who do 28 | not follow or enforce the Code of Conduct may be permanently removed from the 29 | project team. 30 | 31 | This code of conduct applies both within project spaces and in public spaces 32 | when an individual is representing the project or its community. 33 | 34 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 35 | reported by opening an issue or contacting one or more of the project 36 | maintainers. 37 | 38 | This Code of Conduct is adapted from the [Contributor Covenant][cc], version 39 | 1.2.0, available at https://contributor-covenant.org/version/1/2/0/. 40 | 41 | [cc]: http://contributor-covenant.org 42 | -------------------------------------------------------------------------------- /adapters/decimal.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package adapters 16 | 17 | import ( 18 | "fmt" 19 | "math" 20 | "math/big" 21 | "regexp" 22 | "strings" 23 | 24 | dpb "google.golang.org/genproto/googleapis/type/decimal" 25 | ) 26 | 27 | // ProtoDecimalToFloat converts the provided google.type.Decimal to a big.Float. 28 | func ProtoDecimalToFloat(d *dpb.Decimal) (*big.Float, error) { 29 | value := strings.ToLower(d.GetValue()) 30 | 31 | // Determine the required precision. 32 | v := value 33 | if strings.ContainsRune(v, 'e') { 34 | v = v[0:strings.IndexRune(v, 'e')] 35 | } 36 | v = nan.ReplaceAllLiteralString(v, "") 37 | prec := uint(math.Pow(2, float64(len(v)+1))) 38 | 39 | // Parse and return a big.Float. 40 | f, _, err := big.ParseFloat(value, 10, prec, big.AwayFromZero) 41 | return f, err 42 | } 43 | 44 | // ProtoDecimalToFloat64 converts the provided google.type.Decimal to a float64. 45 | func ProtoDecimalToFloat64(d *dpb.Decimal) (float64, big.Accuracy, error) { 46 | f, err := ProtoDecimalToFloat(d) 47 | if err != nil { 48 | return 0.0, big.Exact, err 49 | } 50 | f64, accuracy := f.Float64() 51 | return f64, accuracy, nil 52 | } 53 | 54 | // Float64ToProtoDecimal converts the provided float64 to a google.type.Decimal. 55 | func Float64ToProtoDecimal(f float64) *dpb.Decimal { 56 | return &dpb.Decimal{ 57 | Value: fmt.Sprintf("%f", f), 58 | } 59 | } 60 | 61 | // FloatToProtoDecimal converts the provided big.Float to a google.type.Decimal. 62 | func FloatToProtoDecimal(f *big.Float) *dpb.Decimal { 63 | return &dpb.Decimal{ 64 | Value: f.String(), 65 | } 66 | } 67 | 68 | var nan = regexp.MustCompile(`[^\d]`) 69 | -------------------------------------------------------------------------------- /adapters/color_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package adapters 16 | 17 | import ( 18 | "image/color" 19 | "testing" 20 | 21 | wpb "github.com/golang/protobuf/ptypes/wrappers" 22 | cpb "google.golang.org/genproto/googleapis/type/color" 23 | ) 24 | 25 | func TestColorRGBA(t *testing.T) { 26 | for _, test := range []struct { 27 | name string 28 | color *cpb.Color 29 | rgba *color.RGBA 30 | }{ 31 | {"White", &cpb.Color{Red: 1, Green: 1, Blue: 1}, &color.RGBA{R: 255, G: 255, B: 255, A: 255}}, 32 | {"ExplicitAlpha", &cpb.Color{Red: 1, Green: 1, Blue: 1, Alpha: &wpb.FloatValue{Value: 1}}, &color.RGBA{R: 255, G: 255, B: 255, A: 255}}, 33 | {"Red", &cpb.Color{Red: 1, Green: 0, Blue: 0}, &color.RGBA{R: 255, G: 0, B: 0, A: 255}}, 34 | {"Float", &cpb.Color{Red: 0.5, Green: 0, Blue: 0}, &color.RGBA{R: 128, G: 0, B: 0, A: 255}}, 35 | {"MultiFloat", &cpb.Color{Red: 0.25, Green: 0.5, Blue: 0}, &color.RGBA{R: 64, G: 128, B: 0, A: 255}}, 36 | {"Black", &cpb.Color{Red: 0, Green: 0, Blue: 0}, &color.RGBA{R: 0, G: 0, B: 0, A: 255}}, 37 | {"PartialAlpha", &cpb.Color{Red: 0, Green: 0.5, Blue: 0, Alpha: &wpb.FloatValue{Value: 0.5}}, &color.RGBA{R: 0, G: 128, B: 0, A: 128}}, 38 | {"NoAlpha", &cpb.Color{Red: 0, Green: 0.5, Blue: 0, Alpha: &wpb.FloatValue{Value: 0}}, &color.RGBA{R: 0, G: 128, B: 0, A: 0}}, 39 | } { 40 | t.Run(test.name, func(t *testing.T) { 41 | rgba := ProtoColorToRGBA(test.color) 42 | t.Run("RGBA", func(t *testing.T) { 43 | assertEqual(t, "R", rgba.R, test.rgba.R) 44 | assertEqual(t, "G", rgba.G, test.rgba.G) 45 | assertEqual(t, "B", rgba.B, test.rgba.B) 46 | assertEqual(t, "A", rgba.A, test.rgba.A) 47 | }) 48 | t.Run("Color", func(t *testing.T) { 49 | color := RGBAToProtoColor(rgba) 50 | assertEqual(t, "Red", color.GetRed(), test.color.GetRed()) 51 | assertEqual(t, "Green", color.GetGreen(), test.color.GetGreen()) 52 | assertEqual(t, "Blue", color.GetBlue(), test.color.GetBlue()) 53 | if test.color.Alpha == nil { 54 | assertEqual(t, "Alpha", color.GetAlpha().GetValue(), float32(1)) 55 | } else { 56 | assertEqual(t, "Alpha", color.GetAlpha().GetValue(), test.color.GetAlpha().GetValue()) 57 | } 58 | }) 59 | }) 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /adapters/decimal_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package adapters 16 | 17 | import ( 18 | "fmt" 19 | "testing" 20 | 21 | dpb "google.golang.org/genproto/googleapis/type/decimal" 22 | ) 23 | 24 | func TestDecimal(t *testing.T) { 25 | // Test convertion to big.Float. 26 | for v, want := range map[string]string{ 27 | "123": "123", 28 | "123.45": "123.45", 29 | "-123.45": "-123.45", 30 | "+123.45": "123.45", 31 | "123.45e1000": "1.2345e+1002", 32 | "123.45e+10": "1.2345e+12", 33 | "123.45e01": "1234.5", 34 | "123.45e-10": "1.2345e-08", 35 | "123.45e-1000": "1.2345e-998", 36 | "123.45E10": "1.2345e+12", 37 | "123.45e0": "123.45", 38 | } { 39 | decimalPb := &dpb.Decimal{Value: v} 40 | float, err := ProtoDecimalToFloat(decimalPb) 41 | if err != nil { 42 | t.Error(err) 43 | continue 44 | } 45 | t.Run("ToFloat/"+v, func(t *testing.T) { 46 | assertEqual(t, "FloatEqual", float.String(), want) 47 | t.Run("ToDecimal", func(t *testing.T) { 48 | d := FloatToProtoDecimal(float) 49 | assertEqual(t, "DecimalEqual", d.GetValue(), want) 50 | }) 51 | }) 52 | } 53 | 54 | // Test conversion to float64. 55 | for v, want := range map[string]float64{ 56 | "123": 123, 57 | "123.45": 123.45, 58 | "-123.45": -123.45, 59 | "+123.45": 123.45, 60 | "123.45e100": 1.2345e+102, 61 | "123.45e+10": 1.2345e+12, 62 | "123.45e01": 1234.5, 63 | "123.45e-10": 1.2345e-08, 64 | "123.45e-100": 1.2345e-98, 65 | "123.45E10": 1.2345e+12, 66 | "123.45e0": 123.45, 67 | } { 68 | decimalPb := &dpb.Decimal{Value: v} 69 | float, _, err := ProtoDecimalToFloat64(decimalPb) 70 | if err != nil { 71 | t.Error(err) 72 | continue 73 | } 74 | t.Run("ToFloat64/"+v, func(t *testing.T) { 75 | assertEqual(t, "Float64Equal", float, want) 76 | t.Run("ToDecimal", func(t *testing.T) { 77 | d := Float64ToProtoDecimal(float) 78 | assertEqual(t, "DecimalEqual", d.GetValue(), fmt.Sprintf("%f", want)) 79 | }) 80 | }) 81 | } 82 | 83 | // Test the error case for float64 conversion. 84 | t.Run("ErrorCase", func(t *testing.T) { 85 | _, _, err := ProtoDecimalToFloat64(&dpb.Decimal{Value: "invalid"}) 86 | if err == nil { 87 | t.Errorf("Expected error, got %v.", err) 88 | } 89 | }) 90 | } 91 | -------------------------------------------------------------------------------- /adapters/datetime.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package adapters 16 | 17 | import ( 18 | "fmt" 19 | "regexp" 20 | "strconv" 21 | "time" 22 | 23 | dtpb "google.golang.org/genproto/googleapis/type/datetime" 24 | durpb "google.golang.org/protobuf/types/known/durationpb" 25 | ) 26 | 27 | // ProtoDateTimeToTime returns a new Time based on the google.type.DateTime. 28 | // 29 | // It errors if it gets invalid time zone information. 30 | func ProtoDateTimeToTime(d *dtpb.DateTime) (time.Time, error) { 31 | var err error 32 | 33 | // Determine the location. 34 | loc := time.UTC 35 | if tz := d.GetTimeZone(); tz != nil { 36 | loc, err = time.LoadLocation(tz.GetId()) 37 | if err != nil { 38 | return time.Time{}, err 39 | } 40 | } 41 | if offset := d.GetUtcOffset(); offset != nil { 42 | hours := int(offset.GetSeconds()) / 3600 43 | loc = time.FixedZone(fmt.Sprintf("UTC%+d", hours), hours) 44 | } 45 | 46 | // Return the Time. 47 | return time.Date( 48 | int(d.GetYear()), 49 | time.Month(d.GetMonth()), 50 | int(d.GetDay()), 51 | int(d.GetHours()), 52 | int(d.GetMinutes()), 53 | int(d.GetSeconds()), 54 | int(d.GetNanos()), 55 | loc, 56 | ), nil 57 | } 58 | 59 | // TimeToProtoDateTime returns a new google.type.DateTime based on the 60 | // provided time.Time. 61 | // 62 | // It errors if it gets invalid time zone information. 63 | func TimeToProtoDateTime(t time.Time) (*dtpb.DateTime, error) { 64 | dt := &dtpb.DateTime{ 65 | Year: int32(t.Year()), 66 | Month: int32(t.Month()), 67 | Day: int32(t.Day()), 68 | Hours: int32(t.Hour()), 69 | Minutes: int32(t.Minute()), 70 | Seconds: int32(t.Second()), 71 | Nanos: int32(t.Nanosecond()), 72 | } 73 | 74 | // If the location is a UTC offset, encode it as such in the proto. 75 | loc := t.Location().String() 76 | if match := offsetRegexp.FindStringSubmatch(loc); len(match) > 0 { 77 | offsetInt, err := strconv.Atoi(match[1]) 78 | if err != nil { 79 | return nil, err 80 | } 81 | dt.TimeOffset = &dtpb.DateTime_UtcOffset{ 82 | UtcOffset: &durpb.Duration{Seconds: int64(offsetInt) * 3600}, 83 | } 84 | } else if loc != "" { 85 | dt.TimeOffset = &dtpb.DateTime_TimeZone{ 86 | TimeZone: &dtpb.TimeZone{Id: loc}, 87 | } 88 | } 89 | 90 | return dt, nil 91 | } 92 | 93 | var offsetRegexp = regexp.MustCompile(`^UTC([+-][\d]{1,2})$`) 94 | -------------------------------------------------------------------------------- /adapters/datetime_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package adapters 16 | 17 | import ( 18 | "fmt" 19 | "testing" 20 | "time" 21 | 22 | dtpb "google.golang.org/genproto/googleapis/type/datetime" 23 | durpb "google.golang.org/protobuf/types/known/durationpb" 24 | ) 25 | 26 | func TestDateTime(t *testing.T) { 27 | for _, test := range []struct { 28 | name string 29 | y, mo, d, h, mi, s int 30 | tz *dtpb.TimeZone 31 | offset *durpb.Duration 32 | }{ 33 | {"DateTimeTZ", 2012, 4, 21, 11, 30, 0, &dtpb.TimeZone{Id: "America/New_York"}, nil}, 34 | {"DateTimeTZ", 2012, 4, 21, 11, 30, 0, nil, &durpb.Duration{Seconds: 3600 * 5}}, 35 | } { 36 | t.Run(test.name, func(t *testing.T) { 37 | // Get the starting object. 38 | dt := &dtpb.DateTime{ 39 | Year: int32(test.y), 40 | Month: int32(test.mo), 41 | Day: int32(test.d), 42 | Hours: int32(test.h), 43 | Minutes: int32(test.mi), 44 | Seconds: int32(test.s), 45 | } 46 | if test.tz != nil { 47 | dt.TimeOffset = &dtpb.DateTime_TimeZone{TimeZone: test.tz} 48 | } 49 | if test.offset != nil { 50 | dt.TimeOffset = &dtpb.DateTime_UtcOffset{UtcOffset: test.offset} 51 | } 52 | 53 | // Convert to a time.Time. 54 | tm, err := ProtoDateTimeToTime(dt) 55 | if err != nil { 56 | t.Fatal(err) 57 | } 58 | t.Run("ToTime", func(t *testing.T) { 59 | assertEqual(t, "Year", tm.Year(), test.y) 60 | assertEqual(t, "Month", tm.Month(), time.Month(test.mo)) 61 | assertEqual(t, "Day", tm.Day(), test.d) 62 | assertEqual(t, "Hour", tm.Hour(), test.h) 63 | assertEqual(t, "Minute", tm.Minute(), test.mi) 64 | assertEqual(t, "Second", tm.Second(), test.s) 65 | if test.tz != nil { 66 | assertEqual(t, "TZ", tm.Location().String(), test.tz.GetId()) 67 | } 68 | if test.offset != nil { 69 | assertEqual(t, "Offset", tm.Location().String(), fmt.Sprintf("UTC+%d", test.offset.GetSeconds()/3600)) 70 | } 71 | }) 72 | 73 | // Convert back to a duration. 74 | t.Run("ToDateTime", func(t *testing.T) { 75 | durPb, err := TimeToProtoDateTime(tm) 76 | if err != nil { 77 | t.Fatal(err) 78 | } 79 | assertEqual(t, "Year", durPb.GetYear(), int32(test.y)) 80 | assertEqual(t, "Month", durPb.GetMonth(), int32(test.mo)) 81 | assertEqual(t, "Day", durPb.GetDay(), int32(test.d)) 82 | assertEqual(t, "Hour", durPb.GetHours(), int32(test.h)) 83 | assertEqual(t, "Minute", durPb.GetMinutes(), int32(test.mi)) 84 | assertEqual(t, "Second", durPb.GetSeconds(), int32(test.s)) 85 | if test.tz != nil { 86 | assertEqual(t, "TZ", durPb.GetTimeZone().GetId(), test.tz.GetId()) 87 | } 88 | if test.offset != nil { 89 | assertEqual(t, "Offset", durPb.GetUtcOffset().GetSeconds(), test.offset.GetSeconds()) 90 | } 91 | }) 92 | }) 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /adapters/color.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package adapters 16 | 17 | import ( 18 | "image/color" 19 | "math" 20 | 21 | cpb "google.golang.org/genproto/googleapis/type/color" 22 | wpb "google.golang.org/protobuf/types/known/wrapperspb" 23 | ) 24 | 25 | // ProtoColorToRGBA returns an RGBA based on the provided google.type.Color. 26 | // If alpha is not set in the proto, full opacity is assumed. 27 | // 28 | // Note: Converting between a float using [0, 1] to an int using [0, 256) 29 | // causes some cognitive dissonance between accuracy and user expectations. 30 | // For example, most people writing CSS use 0x80 (decimal 128) to mean "half", 31 | // but it is not actually half (it is slightly over). There is actually no 32 | // way to precisely specify the 0.5 float value in a [0, 256) range of 33 | // integers. 34 | // 35 | // This function uses math.Round to address this, meaning that 0.5 will be 36 | // rounded up to 128 rather than rounded down to 127. 37 | // 38 | // Because of this fuzziness and precision loss, it is NOT guaranteed that 39 | // ProtoColorToRGBA and RGBAToProtoColor are exact inverses, and both functions 40 | // will lose precision. 41 | func ProtoColorToRGBA(c *cpb.Color) *color.RGBA { 42 | // Determine the appropriate alpha value. 43 | // If alpha is unset, full opacity is the proper default. 44 | alpha := uint8(255) 45 | if c.Alpha != nil { 46 | alpha = uint8(math.Round(float64(c.GetAlpha().GetValue() * 255))) 47 | } 48 | 49 | // Return the RGBA. 50 | return &color.RGBA{ 51 | R: uint8(math.Round(float64(c.GetRed()) * 255)), 52 | G: uint8(math.Round(float64(c.GetGreen()) * 255)), 53 | B: uint8(math.Round(float64(c.GetBlue()) * 255)), 54 | A: alpha, 55 | } 56 | } 57 | 58 | // RGBAToProtoColor returns a google.type.Color based on the provided RGBA. 59 | // 60 | // Note: Converting between ints using [0, 256) and a float using [0, 1] 61 | // causes some cognitive dissonance between accuracy and user expectations. 62 | // For example, most people using CSS use 0x80 (decimal 128) to mean "half", 63 | // but it is not actually half (it is slightly over). These is actually no 64 | // way to precisely specify the 0.5 float value in a [0, 256) range of 65 | // integers. 66 | // 67 | // This function addresses this by limiting decimal precision to 0.01, on 68 | // the rationale that most precision beyond this point is probably 69 | // unintentional. 70 | // 71 | // Because of this fuzziness and precision loss, it is NOT guaranteed that 72 | // ProtoColorToRGBA and RGBAToProtoColor are exact inverses, and both functions 73 | // will lose precision. 74 | func RGBAToProtoColor(rgba *color.RGBA) *cpb.Color { 75 | return &cpb.Color{ 76 | Red: float32(int(rgba.R)*100/255) / 100, 77 | Green: float32(int(rgba.G)*100/255) / 100, 78 | Blue: float32(int(rgba.B)*100/255) / 100, 79 | Alpha: &wpb.FloatValue{Value: float32(int(rgba.A)*100/255) / 100}, 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | https://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | --------------------------------------------------------------------------------