File: int.go

package info (click to toggle)
golang-github-mimuret-golang-iij-dpf 0.9.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 1,340 kB
  • sloc: makefile: 55
file content (51 lines) | stat: -rw-r--r-- 962 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package types

import (
	"bytes"
	"encoding/json"

	jsoniter "github.com/json-iterator/go"
)

// 0 is null
type NullablePositiveInt32 int32

func (t *NullablePositiveInt32) UnmarshalJSON(bs []byte) error {
	if bytes.Equal(bs, []byte("null")) {
		return nil
	}
	var i32 int32
	if err := json.Unmarshal(bs, &i32); err != nil {
		return err
	}
	*t = NullablePositiveInt32(i32)
	return nil
}

func (t NullablePositiveInt32) MarshalJSON() ([]byte, error) {
	if t == 0 {
		return []byte("null"), nil
	}
	return jsoniter.Marshal(int32(t))
}

type NullablePositiveInt64 int64

func (t *NullablePositiveInt64) UnmarshalJSON(bs []byte) error {
	if bytes.Equal(bs, []byte("null")) {
		return nil
	}
	var i64 int64
	if err := json.Unmarshal(bs, &i64); err != nil {
		return err
	}
	*t = NullablePositiveInt64(i64)
	return nil
}

func (t NullablePositiveInt64) MarshalJSON() ([]byte, error) {
	if t == 0 {
		return []byte("null"), nil
	}
	return jsoniter.Marshal(int64(t))
}