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