diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-12-17 14:59:07 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-12-17 14:59:07 +0300 |
commit | 8b573c94895dc0ac0e1d9d59cf3e8745e8b539ca (patch) | |
tree | 544930fb309b30317ae9797a9683768705d664c4 /workhorse/internal/lsif_transformer/parser/id.go | |
parent | 4b1de649d0168371549608993deac953eb692019 (diff) |
Add latest changes from gitlab-org/gitlab@13-7-stable-eev13.7.0-rc42
Diffstat (limited to 'workhorse/internal/lsif_transformer/parser/id.go')
-rw-r--r-- | workhorse/internal/lsif_transformer/parser/id.go | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/workhorse/internal/lsif_transformer/parser/id.go b/workhorse/internal/lsif_transformer/parser/id.go new file mode 100644 index 00000000000..2adc4e092f5 --- /dev/null +++ b/workhorse/internal/lsif_transformer/parser/id.go @@ -0,0 +1,52 @@ +package parser + +import ( + "encoding/json" + "errors" + "strconv" +) + +const ( + minId = 1 + maxId = 20 * 1000 * 1000 +) + +type Id int32 + +func (id *Id) UnmarshalJSON(b []byte) error { + if len(b) > 0 && b[0] != '"' { + if err := id.unmarshalInt(b); err != nil { + return err + } + } else { + if err := id.unmarshalString(b); err != nil { + return err + } + } + + if *id < minId || *id > maxId { + return errors.New("json: id is invalid") + } + + return nil +} + +func (id *Id) unmarshalInt(b []byte) error { + return json.Unmarshal(b, (*int32)(id)) +} + +func (id *Id) unmarshalString(b []byte) error { + var s string + if err := json.Unmarshal(b, &s); err != nil { + return err + } + + i, err := strconv.Atoi(s) + if err != nil { + return err + } + + *id = Id(i) + + return nil +} |