cuelang.org/go@v0.10.1/encoding/jsonschema/constraints_number.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 jsonschema 16 17 import ( 18 "math/big" 19 20 "cuelang.org/go/cue" 21 "cuelang.org/go/cue/ast" 22 "cuelang.org/go/cue/token" 23 ) 24 25 // Numeric constraints 26 27 func constraintExclusiveMaximum(key string, n cue.Value, s *state) { 28 if n.Kind() == cue.BoolKind { 29 s.exclusiveMax = true 30 return 31 } 32 s.add(n, numType, &ast.UnaryExpr{Op: token.LSS, X: s.number(n)}) 33 } 34 func constraintExclusiveMinimum(key string, n cue.Value, s *state) { 35 if n.Kind() == cue.BoolKind { 36 s.exclusiveMin = true 37 return 38 } 39 s.add(n, numType, &ast.UnaryExpr{Op: token.GTR, X: s.number(n)}) 40 } 41 42 func constraintMinimum(key string, n cue.Value, s *state) { 43 op := token.GEQ 44 if s.exclusiveMin { 45 op = token.GTR 46 } 47 s.add(n, numType, &ast.UnaryExpr{Op: op, X: s.number(n)}) 48 } 49 50 func constraintMaximum(key string, n cue.Value, s *state) { 51 op := token.LEQ 52 if s.exclusiveMax { 53 op = token.LSS 54 } 55 s.add(n, numType, &ast.UnaryExpr{Op: op, X: s.number(n)}) 56 } 57 58 func constraintMultipleOf(key string, n cue.Value, s *state) { 59 multiple := s.number(n) 60 var x big.Int 61 _, _ = n.MantExp(&x) 62 if x.Cmp(big.NewInt(0)) != 1 { 63 s.errf(n, `"multipleOf" value must be < 0; found %s`, n) 64 } 65 math := s.addImport(n, "math") 66 s.add(n, numType, ast.NewCall(ast.NewSel(math, "MultipleOf"), multiple)) 67 }