Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 1 | // Copyright 2019 CUE Authors |
| 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 | // http://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 openapi |
| 16 | |
| 17 | import ( |
Marcel van Lohuizen | 6bf3697 | 2019-08-06 20:15:58 +0200 | [diff] [blame] | 18 | "fmt" |
| 19 | |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 20 | "github.com/cockroachdb/apd/v2" |
| 21 | |
| 22 | "cuelang.org/go/cue" |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 23 | "cuelang.org/go/cue/ast" |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 24 | "cuelang.org/go/cue/format" |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 25 | "cuelang.org/go/cue/literal" |
| 26 | "cuelang.org/go/cue/token" |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | // See https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#format |
| 30 | var cueToOpenAPI = map[string]string{ |
| 31 | "int32": "int32", |
| 32 | "int64": "int64", |
| 33 | |
| 34 | "float64": "double", |
| 35 | "float32": "float", |
| 36 | |
| 37 | "string": "string", |
| 38 | "bytes": "binary", |
| 39 | |
Marcel van Lohuizen | 6bf3697 | 2019-08-06 20:15:58 +0200 | [diff] [blame] | 40 | "time.Time": "dateTime", |
| 41 | "time.Time ()": "dateTime", |
| 42 | `time.Format ("2006-01-02")`: "date", |
| 43 | |
| 44 | // TODO: if a format is more strict (e.g. using zeros instead of nines |
| 45 | // for fractional seconds), we could still use this as an approximation. |
| 46 | `time.Format ("2006-01-02T15:04:05.999999999Z07:00")`: "dateTime", |
| 47 | |
| 48 | // TODO: password. |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | func extractFormat(v cue.Value) string { |
| 52 | switch k := v.IncompleteKind(); { |
| 53 | case k&cue.NumberKind != 0, k&cue.StringKind != 0, k&cue.BytesKind != 0: |
| 54 | default: |
| 55 | return "" |
| 56 | } |
Marcel van Lohuizen | f4649b2 | 2020-02-28 16:50:45 +0100 | [diff] [blame] | 57 | b, err := format.Node(v.Syntax(cue.Final())) |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 58 | if err != nil { |
| 59 | return "" |
| 60 | } |
Marcel van Lohuizen | 6bf3697 | 2019-08-06 20:15:58 +0200 | [diff] [blame] | 61 | if s, ok := cueToOpenAPI[string(b)]; ok { |
| 62 | return s |
| 63 | } |
| 64 | s := fmt.Sprint(v) |
| 65 | return cueToOpenAPI[s] |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Jason Wang | 2b56efa | 2019-08-21 13:35:53 -0700 | [diff] [blame] | 68 | func getDeprecated(v cue.Value) bool { |
| 69 | // only looking at protobuf attribute for now. |
| 70 | a := v.Attribute("protobuf") |
| 71 | r, _ := a.Flag(1, "deprecated") |
| 72 | return r |
| 73 | } |
| 74 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 75 | func simplify(b *builder, t *ast.StructLit) { |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 76 | if b.format == "" { |
| 77 | return |
| 78 | } |
| 79 | switch b.typ { |
| 80 | case "number", "integer": |
| 81 | simplifyNumber(t, b.format) |
| 82 | } |
| 83 | } |
| 84 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 85 | func simplifyNumber(t *ast.StructLit, format string) string { |
| 86 | fields := t.Elts |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 87 | k := 0 |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 88 | for i, d := range fields { |
| 89 | switch label(d) { |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 90 | case "minimum": |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 91 | if decimalEqual(minMap[format], value(d)) { |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 92 | continue |
| 93 | } |
| 94 | case "maximum": |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 95 | if decimalEqual(maxMap[format], value(d)) { |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 96 | continue |
| 97 | } |
| 98 | } |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 99 | fields[k] = fields[i] |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 100 | k++ |
| 101 | } |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 102 | t.Elts = fields[:k] |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 103 | return format |
| 104 | } |
| 105 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 106 | func decimalEqual(d *apd.Decimal, v ast.Expr) bool { |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 107 | if d == nil { |
| 108 | return false |
| 109 | } |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 110 | lit, ok := v.(*ast.BasicLit) |
| 111 | if !ok || (lit.Kind != token.INT && lit.Kind != token.FLOAT) { |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 112 | return false |
| 113 | } |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 114 | n := literal.NumInfo{} |
| 115 | if literal.ParseNum(lit.Value, &n) != nil { |
| 116 | return false |
| 117 | } |
| 118 | var b apd.Decimal |
| 119 | if n.Decimal(&b) != nil { |
| 120 | return false |
| 121 | } |
| 122 | return d.Cmp(&b) == 0 |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 125 | func mustDecimal(s string) *apd.Decimal { |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 126 | d, _, err := apd.NewFromString(s) |
| 127 | if err != nil { |
| 128 | panic(err) |
| 129 | } |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 130 | return d |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | var ( |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 134 | minMap = map[string]*apd.Decimal{ |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 135 | "int32": mustDecimal("-2147483648"), |
| 136 | "int64": mustDecimal("-9223372036854775808"), |
| 137 | "float": mustDecimal("-3.40282346638528859811704183484516925440e+38"), |
| 138 | "double": mustDecimal("-1.797693134862315708145274237317043567981e+308"), |
| 139 | } |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 140 | maxMap = map[string]*apd.Decimal{ |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 141 | "int32": mustDecimal("2147483647"), |
| 142 | "int64": mustDecimal("9223372036854775807"), |
| 143 | "float": mustDecimal("+3.40282346638528859811704183484516925440e+38"), |
| 144 | "double": mustDecimal("+1.797693134862315708145274237317043567981e+308"), |
| 145 | } |
| 146 | ) |