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" |
Marcel van Lohuizen | ed90d00 | 2020-03-30 17:18:12 +0200 | [diff] [blame] | 19 | "strings" |
Marcel van Lohuizen | 6bf3697 | 2019-08-06 20:15:58 +0200 | [diff] [blame] | 20 | |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 21 | "github.com/cockroachdb/apd/v2" |
| 22 | |
| 23 | "cuelang.org/go/cue" |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 24 | "cuelang.org/go/cue/ast" |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 25 | "cuelang.org/go/cue/format" |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 26 | "cuelang.org/go/cue/literal" |
| 27 | "cuelang.org/go/cue/token" |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | // See https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#format |
| 31 | var cueToOpenAPI = map[string]string{ |
| 32 | "int32": "int32", |
| 33 | "int64": "int64", |
| 34 | |
| 35 | "float64": "double", |
| 36 | "float32": "float", |
| 37 | |
| 38 | "string": "string", |
| 39 | "bytes": "binary", |
| 40 | |
Marcel van Lohuizen | 6bf3697 | 2019-08-06 20:15:58 +0200 | [diff] [blame] | 41 | "time.Time": "dateTime", |
Marcel van Lohuizen | 6bf3697 | 2019-08-06 20:15:58 +0200 | [diff] [blame] | 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 | ed90d00 | 2020-03-30 17:18:12 +0200 | [diff] [blame] | 57 | var expr, arg string |
| 58 | op, a := v.Expr() |
| 59 | if op == cue.CallOp { |
| 60 | v = a[0] |
| 61 | if len(a) == 2 { |
| 62 | arg = fmt.Sprintf(" (%s)", a[1].Eval()) |
| 63 | } |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 64 | } |
Marcel van Lohuizen | ed90d00 | 2020-03-30 17:18:12 +0200 | [diff] [blame] | 65 | if inst, ref := v.Reference(); len(ref) > 0 { |
| 66 | expr = inst.ImportPath + "." + strings.Join(ref, ".") |
| 67 | expr += arg |
| 68 | } else { |
| 69 | // TODO: have some function to extract normalized builtin types. |
| 70 | b, err := format.Node(v.Syntax(cue.Final())) |
| 71 | if err != nil { |
| 72 | return "" |
| 73 | } |
| 74 | expr = string(b) |
| 75 | } |
| 76 | if s, ok := cueToOpenAPI[expr]; ok { |
Marcel van Lohuizen | 6bf3697 | 2019-08-06 20:15:58 +0200 | [diff] [blame] | 77 | return s |
| 78 | } |
| 79 | s := fmt.Sprint(v) |
| 80 | return cueToOpenAPI[s] |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Jason Wang | 2b56efa | 2019-08-21 13:35:53 -0700 | [diff] [blame] | 83 | func getDeprecated(v cue.Value) bool { |
| 84 | // only looking at protobuf attribute for now. |
| 85 | a := v.Attribute("protobuf") |
| 86 | r, _ := a.Flag(1, "deprecated") |
| 87 | return r |
| 88 | } |
| 89 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 90 | func simplify(b *builder, t *ast.StructLit) { |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 91 | if b.format == "" { |
| 92 | return |
| 93 | } |
| 94 | switch b.typ { |
| 95 | case "number", "integer": |
| 96 | simplifyNumber(t, b.format) |
| 97 | } |
| 98 | } |
| 99 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 100 | func simplifyNumber(t *ast.StructLit, format string) string { |
| 101 | fields := t.Elts |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 102 | k := 0 |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 103 | for i, d := range fields { |
| 104 | switch label(d) { |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 105 | case "minimum": |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 106 | if decimalEqual(minMap[format], value(d)) { |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 107 | continue |
| 108 | } |
| 109 | case "maximum": |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 110 | if decimalEqual(maxMap[format], value(d)) { |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 111 | continue |
| 112 | } |
| 113 | } |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 114 | fields[k] = fields[i] |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 115 | k++ |
| 116 | } |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 117 | t.Elts = fields[:k] |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 118 | return format |
| 119 | } |
| 120 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 121 | func decimalEqual(d *apd.Decimal, v ast.Expr) bool { |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 122 | if d == nil { |
| 123 | return false |
| 124 | } |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 125 | lit, ok := v.(*ast.BasicLit) |
| 126 | if !ok || (lit.Kind != token.INT && lit.Kind != token.FLOAT) { |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 127 | return false |
| 128 | } |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 129 | n := literal.NumInfo{} |
| 130 | if literal.ParseNum(lit.Value, &n) != nil { |
| 131 | return false |
| 132 | } |
| 133 | var b apd.Decimal |
| 134 | if n.Decimal(&b) != nil { |
| 135 | return false |
| 136 | } |
| 137 | return d.Cmp(&b) == 0 |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 138 | } |
| 139 | |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 140 | func mustDecimal(s string) *apd.Decimal { |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 141 | d, _, err := apd.NewFromString(s) |
| 142 | if err != nil { |
| 143 | panic(err) |
| 144 | } |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 145 | return d |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | var ( |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 149 | minMap = map[string]*apd.Decimal{ |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 150 | "int32": mustDecimal("-2147483648"), |
| 151 | "int64": mustDecimal("-9223372036854775808"), |
| 152 | "float": mustDecimal("-3.40282346638528859811704183484516925440e+38"), |
| 153 | "double": mustDecimal("-1.797693134862315708145274237317043567981e+308"), |
| 154 | } |
Marcel van Lohuizen | f51c879 | 2020-03-05 17:53:29 +0100 | [diff] [blame] | 155 | maxMap = map[string]*apd.Decimal{ |
Marcel van Lohuizen | a0d2a40 | 2019-06-29 14:07:41 +0200 | [diff] [blame] | 156 | "int32": mustDecimal("2147483647"), |
| 157 | "int64": mustDecimal("9223372036854775807"), |
| 158 | "float": mustDecimal("+3.40282346638528859811704183484516925440e+38"), |
| 159 | "double": mustDecimal("+1.797693134862315708145274237317043567981e+308"), |
| 160 | } |
| 161 | ) |