-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
201 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package pactasrv | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
|
||
"github.com/RMI/pacta/oapierr" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func checkStringLimitMediumPtr(site string, value *string) error { | ||
if value == nil { | ||
return nil | ||
} | ||
return checkStringLimitMedium(site, *value) | ||
} | ||
|
||
func checkStringLimitMedium(site string, value string) error { | ||
return checkStringLimit(site, value, 10000) | ||
} | ||
|
||
func checkStringLimitSmallPtr(site string, value *string) error { | ||
if value == nil { | ||
return nil | ||
} | ||
return checkStringLimitSmall(site, *value) | ||
} | ||
|
||
func checkStringLimitSmall(site string, value string) error { | ||
return checkStringLimit(site, value, 1000) | ||
} | ||
|
||
func checkStringLimit(site string, value string, byteLimit int) error { | ||
byteLength := len(value) | ||
return checkLimit( | ||
site, | ||
byteLength, formatByteSize(byteLength), | ||
byteLimit, formatByteSize(byteLimit)) | ||
} | ||
|
||
func checkIntLimit(site string, value int, limit int) error { | ||
return checkLimit(site, value, fmt.Sprintf("%d", value), limit, fmt.Sprintf("%d", limit)) | ||
} | ||
|
||
func checkLimit(site string, value int, valueStr string, limit int, limitStr string) error { | ||
if value > limit { | ||
return oapierr.BadRequest( | ||
"Input too large", | ||
zap.String("site", site), | ||
zap.Int("value", value), | ||
zap.Int("limit", limit), | ||
).WithMessage( | ||
fmt.Sprintf("the input for %s is too large (%s exceeds the limit, %s)", | ||
site, | ||
valueStr, | ||
limitStr), | ||
).WithErrorID("INPUT_EXCEEDS_LIMIT") | ||
} | ||
return nil | ||
} | ||
|
||
func formatByteSize(bytes int) string { | ||
if bytes <= 0 { | ||
return "" | ||
} | ||
if bytes < 1000 { | ||
return fmt.Sprintf("%d %s", bytes, dataSizes[0]) | ||
} | ||
k := 1000.0 | ||
i := int(math.Floor(math.Log(float64(bytes)) / math.Log(k))) | ||
return fmt.Sprintf("%.2f %s", float64(bytes)/math.Pow(k, float64(i)), dataSizes[i]) | ||
} | ||
|
||
var dataSizes []string = []string{ | ||
"Bytes", "kB", "MB", "GB", "TB", | ||
} | ||
|
||
func anyError(errs ...error) error { | ||
for _, err := range errs { | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package pactasrv | ||
|
||
import "testing" | ||
|
||
func TestFormatByteSize(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
bytes int | ||
expected string | ||
}{ | ||
{"TestZeroBytes", 0, ""}, | ||
{"TestNegativeBytes", -100, ""}, | ||
{"TestBytes", 7, "7 Bytes"}, | ||
{"TestKilobyte", 2321, "2.32 kB"}, | ||
{"TestMegabyte", 1004999, "1.00 MB"}, | ||
{"TestGigabyte", 40005100000, "40.01 GB"}, | ||
{"TestTerabyte", 100000000000000, "100.00 TB"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := formatByteSize(tt.bytes); got != tt.expected { | ||
t.Errorf("formatByteSize(%d) = %v, want %v", tt.bytes, got, tt.expected) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters