@@ -50,8 +50,7 @@ public struct FunctionSignature {
50
50
/// - Parameter args: Array of arguments
51
51
/// - Throws: JMESPathError.runtime
52
52
func validateArgs( _ args: [ JMESVariable ] ) throws {
53
- guard args. count == self . inputs. count ||
54
- ( args. count > self . inputs. count && self . varArg != nil )
53
+ guard args. count == self . inputs. count || ( args. count > self . inputs. count && self . varArg != nil )
55
54
else {
56
55
throw JMESPathError . runtime ( " Invalid number of arguments, expected \( self . inputs. count) , got \( args. count) " )
57
56
}
@@ -76,13 +75,13 @@ extension JMESVariable {
76
75
func isType( _ type: FunctionSignature . ArgumentType ) -> Bool {
77
76
switch ( self , type) {
78
77
case ( _, . any) ,
79
- ( . string, . string) ,
80
- ( . null, . null) ,
81
- ( . number, . number) ,
82
- ( . boolean, . boolean) ,
83
- ( . array, . array) ,
84
- ( . object, . object) ,
85
- ( . expRef, . expRef) :
78
+ ( . string, . string) ,
79
+ ( . null, . null) ,
80
+ ( . number, . number) ,
81
+ ( . boolean, . boolean) ,
82
+ ( . array, . array) ,
83
+ ( . object, . object) ,
84
+ ( . expRef, . expRef) :
86
85
return true
87
86
88
87
case ( . array( let array) , . typedArray( let elementType) ) :
@@ -128,7 +127,7 @@ public protocol JMESFunction {
128
127
129
128
/// Protocl for JMESPath function that takes a single number
130
129
protocol NumberFunction : JMESFunction {
131
- static func evaluate( _ number: NSNumber ) -> JMESVariable
130
+ static func evaluate( _ number: JMESNumber ) -> JMESVariable
132
131
}
133
132
134
133
extension NumberFunction {
@@ -166,8 +165,8 @@ extension ArrayFunction {
166
165
/// Returns the absolute value of the provided argument. The signature indicates that a number is returned, and that the
167
166
/// input argument must resolve to a number, otherwise a invalid-type error is triggered.
168
167
struct AbsFunction : NumberFunction {
169
- static func evaluate( _ number: NSNumber ) -> JMESVariable {
170
- return . number( . init ( value : abs ( number . doubleValue ) ) )
168
+ static func evaluate( _ number: JMESNumber ) -> JMESVariable {
169
+ . number( number . abs ( ) )
171
170
}
172
171
}
173
172
@@ -177,22 +176,22 @@ struct AvgFunction: ArrayFunction {
177
176
static var signature : FunctionSignature { . init( inputs: . typedArray( . number) ) }
178
177
static func evaluate( _ array: JMESArray ) -> JMESVariable {
179
178
guard array. count > 0 else { return . null }
180
- let total = array. reduce ( 0.0 ) {
179
+ let total = array. reduce ( JMESNumber ( 0 ) ) {
181
180
if case . number( let number) = JMESVariable ( from: $1) {
182
- return $0 + number. doubleValue
181
+ return $0 + number
183
182
} else {
184
183
preconditionFailure ( )
185
184
}
186
185
}
187
- return . number( . init ( value : total / Double( array. count) ) )
186
+ return . number( total / JMESNumber ( Double ( array. count) ) )
188
187
}
189
188
}
190
189
191
190
/// `number ceil(number $value)`
192
191
/// Returns the next highest integer value by rounding up if necessary.
193
192
struct CeilFunction : NumberFunction {
194
- static func evaluate( _ number: NSNumber ) -> JMESVariable {
195
- return . number( . init ( value : ceil ( number . doubleValue ) ) )
193
+ static func evaluate( _ number: JMESNumber ) -> JMESVariable {
194
+ . number( number . ceil ( ) )
196
195
}
197
196
}
198
197
@@ -236,8 +235,8 @@ struct EndsWithFunction: JMESFunction {
236
235
/// `number floor(number $value)`
237
236
/// Returns the next lowest integer value by rounding down if necessary.
238
237
struct FloorFunction : NumberFunction {
239
- static func evaluate( _ number: NSNumber ) -> JMESVariable {
240
- return . number( . init ( value : floor ( number . doubleValue ) ) )
238
+ static func evaluate( _ number: JMESNumber ) -> JMESVariable {
239
+ . number( number . floor ( ) )
241
240
}
242
241
}
243
242
@@ -289,11 +288,11 @@ struct LengthFunction: JMESFunction {
289
288
static func evaluate( args: [ JMESVariable ] , runtime: JMESRuntime ) -> JMESVariable {
290
289
switch args [ 0 ] {
291
290
case . array( let array) :
292
- return . number( . init( value : array. count) )
291
+ return . number( . init( array. count) )
293
292
case . object( let object) :
294
- return . number( . init( value : object. count) )
293
+ return . number( . init( object. count) )
295
294
case . string( let string) :
296
- return . number( . init( value : string. count) )
295
+ return . number( . init( string. count) )
297
296
default :
298
297
preconditionFailure ( )
299
298
}
@@ -342,7 +341,7 @@ struct MaxFunction: JMESFunction {
342
341
case . number( var max) :
343
342
for element in array. dropFirst ( ) {
344
343
if case . number( let number) = JMESVariable ( from: element) {
345
- if number. compare ( max ) == . orderedDescending {
344
+ if number > max {
346
345
max = number
347
346
}
348
347
}
@@ -389,7 +388,7 @@ struct MaxByFunction: JMESFunction {
389
388
for element in array. dropFirst ( ) {
390
389
let value = try runtime. interpret ( JMESVariable ( from: element) , ast: ast)
391
390
if case . number( let number) = value {
392
- if number. compare ( maxValue ) == . orderedDescending {
391
+ if number > maxValue {
393
392
maxValue = number
394
393
maxElement = element
395
394
}
@@ -430,7 +429,7 @@ struct MinFunction: JMESFunction {
430
429
case . number( var min) :
431
430
for element in array {
432
431
if case . number( let number) = JMESVariable ( from: element) {
433
- if number. compare ( min ) == . orderedAscending {
432
+ if number < min {
434
433
min = number
435
434
}
436
435
}
@@ -477,7 +476,7 @@ struct MinByFunction: JMESFunction {
477
476
for element in array. dropFirst ( ) {
478
477
let value = try runtime. interpret ( JMESVariable ( from: element) , ast: ast)
479
478
if case . number( let number) = value {
480
- if number. compare ( minValue ) == . orderedAscending {
479
+ if number < minValue {
481
480
minValue = number
482
481
minElement = element
483
482
}
@@ -603,7 +602,9 @@ struct SortByFunction: JMESFunction {
603
602
let restOfTheValues = try array. dropFirst ( ) . map { element -> ValueAndSortKey in
604
603
let sortValue = try runtime. interpret ( JMESVariable ( from: element) , ast: ast)
605
604
guard sortValue. isSameType ( as: firstSortValue) else {
606
- throw JMESPathError . runtime ( " Sort arguments all have to be the same type, expected \( firstSortValue. getType ( ) ) , instead got \( sortValue. getType ( ) ) " )
605
+ throw JMESPathError . runtime (
606
+ " Sort arguments all have to be the same type, expected \( firstSortValue. getType ( ) ) , instead got \( sortValue. getType ( ) ) "
607
+ )
607
608
}
608
609
return . init( value: element, sortValue: sortValue)
609
610
}
@@ -636,14 +637,21 @@ struct StartsWithFunction: JMESFunction {
636
637
struct SumFunction : ArrayFunction {
637
638
static var signature : FunctionSignature { . init( inputs: . typedArray( . number) ) }
638
639
static func evaluate( _ array: JMESArray ) -> JMESVariable {
639
- let total = array. reduce ( 0.0 ) {
640
- if case . number( let number) = JMESVariable ( from: $1) {
641
- return $0 + number. doubleValue
642
- } else {
643
- preconditionFailure ( )
640
+ guard let first = array. first. map ( { JMESVariable ( from: $0) } ) else { return . number( . init( 0 ) ) }
641
+ switch first {
642
+ case . number( let number) :
643
+ let total = array. dropFirst ( ) . reduce ( number) {
644
+ if case . number( let number) = JMESVariable ( from: $1) {
645
+ return $0 + number
646
+ } else {
647
+ preconditionFailure ( )
648
+ }
644
649
}
650
+ return . number( total)
651
+
652
+ default :
653
+ preconditionFailure ( )
645
654
}
646
- return . number( . init( value: total) )
647
655
}
648
656
}
649
657
@@ -709,7 +717,7 @@ struct ToStringFunction: JMESFunction {
709
717
struct TypeFunction : JMESFunction {
710
718
static var signature : FunctionSignature { . init( inputs: . any) }
711
719
static func evaluate( args: [ JMESVariable ] , runtime: JMESRuntime ) throws -> JMESVariable {
712
- return . string( args [ 0 ] . getType ( ) )
720
+ . string( args [ 0 ] . getType ( ) )
713
721
}
714
722
}
715
723
0 commit comments