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