...

Source file src/github.com/jackc/pgtype/int8.go

Documentation: github.com/jackc/pgtype

     1  package pgtype
     2  
     3  import (
     4  	"database/sql/driver"
     5  	"encoding/binary"
     6  	"encoding/json"
     7  	"fmt"
     8  	"math"
     9  	"strconv"
    10  
    11  	"github.com/jackc/pgio"
    12  )
    13  
    14  type Int8 struct {
    15  	Int    int64
    16  	Status Status
    17  }
    18  
    19  func (dst *Int8) Set(src interface{}) error {
    20  	if src == nil {
    21  		*dst = Int8{Status: Null}
    22  		return nil
    23  	}
    24  
    25  	if value, ok := src.(interface{ Get() interface{} }); ok {
    26  		value2 := value.Get()
    27  		if value2 != value {
    28  			return dst.Set(value2)
    29  		}
    30  	}
    31  
    32  	switch value := src.(type) {
    33  	case int8:
    34  		*dst = Int8{Int: int64(value), Status: Present}
    35  	case uint8:
    36  		*dst = Int8{Int: int64(value), Status: Present}
    37  	case int16:
    38  		*dst = Int8{Int: int64(value), Status: Present}
    39  	case uint16:
    40  		*dst = Int8{Int: int64(value), Status: Present}
    41  	case int32:
    42  		*dst = Int8{Int: int64(value), Status: Present}
    43  	case uint32:
    44  		*dst = Int8{Int: int64(value), Status: Present}
    45  	case int64:
    46  		*dst = Int8{Int: int64(value), Status: Present}
    47  	case uint64:
    48  		if value > math.MaxInt64 {
    49  			return fmt.Errorf("%d is greater than maximum value for Int8", value)
    50  		}
    51  		*dst = Int8{Int: int64(value), Status: Present}
    52  	case int:
    53  		if int64(value) < math.MinInt64 {
    54  			return fmt.Errorf("%d is greater than maximum value for Int8", value)
    55  		}
    56  		if int64(value) > math.MaxInt64 {
    57  			return fmt.Errorf("%d is greater than maximum value for Int8", value)
    58  		}
    59  		*dst = Int8{Int: int64(value), Status: Present}
    60  	case uint:
    61  		if uint64(value) > math.MaxInt64 {
    62  			return fmt.Errorf("%d is greater than maximum value for Int8", value)
    63  		}
    64  		*dst = Int8{Int: int64(value), Status: Present}
    65  	case string:
    66  		num, err := strconv.ParseInt(value, 10, 64)
    67  		if err != nil {
    68  			return err
    69  		}
    70  		*dst = Int8{Int: num, Status: Present}
    71  	case float32:
    72  		if value > math.MaxInt64 {
    73  			return fmt.Errorf("%f is greater than maximum value for Int8", value)
    74  		}
    75  		*dst = Int8{Int: int64(value), Status: Present}
    76  	case float64:
    77  		if value > math.MaxInt64 {
    78  			return fmt.Errorf("%f is greater than maximum value for Int8", value)
    79  		}
    80  		*dst = Int8{Int: int64(value), Status: Present}
    81  	case *int8:
    82  		if value == nil {
    83  			*dst = Int8{Status: Null}
    84  		} else {
    85  			return dst.Set(*value)
    86  		}
    87  	case *uint8:
    88  		if value == nil {
    89  			*dst = Int8{Status: Null}
    90  		} else {
    91  			return dst.Set(*value)
    92  		}
    93  	case *int16:
    94  		if value == nil {
    95  			*dst = Int8{Status: Null}
    96  		} else {
    97  			return dst.Set(*value)
    98  		}
    99  	case *uint16:
   100  		if value == nil {
   101  			*dst = Int8{Status: Null}
   102  		} else {
   103  			return dst.Set(*value)
   104  		}
   105  	case *int32:
   106  		if value == nil {
   107  			*dst = Int8{Status: Null}
   108  		} else {
   109  			return dst.Set(*value)
   110  		}
   111  	case *uint32:
   112  		if value == nil {
   113  			*dst = Int8{Status: Null}
   114  		} else {
   115  			return dst.Set(*value)
   116  		}
   117  	case *int64:
   118  		if value == nil {
   119  			*dst = Int8{Status: Null}
   120  		} else {
   121  			return dst.Set(*value)
   122  		}
   123  	case *uint64:
   124  		if value == nil {
   125  			*dst = Int8{Status: Null}
   126  		} else {
   127  			return dst.Set(*value)
   128  		}
   129  	case *int:
   130  		if value == nil {
   131  			*dst = Int8{Status: Null}
   132  		} else {
   133  			return dst.Set(*value)
   134  		}
   135  	case *uint:
   136  		if value == nil {
   137  			*dst = Int8{Status: Null}
   138  		} else {
   139  			return dst.Set(*value)
   140  		}
   141  	case *string:
   142  		if value == nil {
   143  			*dst = Int8{Status: Null}
   144  		} else {
   145  			return dst.Set(*value)
   146  		}
   147  	case *float32:
   148  		if value == nil {
   149  			*dst = Int8{Status: Null}
   150  		} else {
   151  			return dst.Set(*value)
   152  		}
   153  	case *float64:
   154  		if value == nil {
   155  			*dst = Int8{Status: Null}
   156  		} else {
   157  			return dst.Set(*value)
   158  		}
   159  	default:
   160  		if originalSrc, ok := underlyingNumberType(src); ok {
   161  			return dst.Set(originalSrc)
   162  		}
   163  		return fmt.Errorf("cannot convert %v to Int8", value)
   164  	}
   165  
   166  	return nil
   167  }
   168  
   169  func (dst Int8) Get() interface{} {
   170  	switch dst.Status {
   171  	case Present:
   172  		return dst.Int
   173  	case Null:
   174  		return nil
   175  	default:
   176  		return dst.Status
   177  	}
   178  }
   179  
   180  func (src *Int8) AssignTo(dst interface{}) error {
   181  	return int64AssignTo(int64(src.Int), src.Status, dst)
   182  }
   183  
   184  func (dst *Int8) DecodeText(ci *ConnInfo, src []byte) error {
   185  	if src == nil {
   186  		*dst = Int8{Status: Null}
   187  		return nil
   188  	}
   189  
   190  	n, err := strconv.ParseInt(string(src), 10, 64)
   191  	if err != nil {
   192  		return err
   193  	}
   194  
   195  	*dst = Int8{Int: n, Status: Present}
   196  	return nil
   197  }
   198  
   199  func (dst *Int8) DecodeBinary(ci *ConnInfo, src []byte) error {
   200  	if src == nil {
   201  		*dst = Int8{Status: Null}
   202  		return nil
   203  	}
   204  
   205  	if len(src) != 8 {
   206  		return fmt.Errorf("invalid length for int8: %v", len(src))
   207  	}
   208  
   209  	n := int64(binary.BigEndian.Uint64(src))
   210  
   211  	*dst = Int8{Int: n, Status: Present}
   212  	return nil
   213  }
   214  
   215  func (src Int8) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
   216  	switch src.Status {
   217  	case Null:
   218  		return nil, nil
   219  	case Undefined:
   220  		return nil, errUndefined
   221  	}
   222  
   223  	return append(buf, strconv.FormatInt(src.Int, 10)...), nil
   224  }
   225  
   226  func (src Int8) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
   227  	switch src.Status {
   228  	case Null:
   229  		return nil, nil
   230  	case Undefined:
   231  		return nil, errUndefined
   232  	}
   233  
   234  	return pgio.AppendInt64(buf, src.Int), nil
   235  }
   236  
   237  // Scan implements the database/sql Scanner interface.
   238  func (dst *Int8) Scan(src interface{}) error {
   239  	if src == nil {
   240  		*dst = Int8{Status: Null}
   241  		return nil
   242  	}
   243  
   244  	switch src := src.(type) {
   245  	case int64:
   246  		*dst = Int8{Int: src, Status: Present}
   247  		return nil
   248  	case string:
   249  		return dst.DecodeText(nil, []byte(src))
   250  	case []byte:
   251  		srcCopy := make([]byte, len(src))
   252  		copy(srcCopy, src)
   253  		return dst.DecodeText(nil, srcCopy)
   254  	}
   255  
   256  	return fmt.Errorf("cannot scan %T", src)
   257  }
   258  
   259  // Value implements the database/sql/driver Valuer interface.
   260  func (src Int8) Value() (driver.Value, error) {
   261  	switch src.Status {
   262  	case Present:
   263  		return int64(src.Int), nil
   264  	case Null:
   265  		return nil, nil
   266  	default:
   267  		return nil, errUndefined
   268  	}
   269  }
   270  
   271  func (src Int8) MarshalJSON() ([]byte, error) {
   272  	switch src.Status {
   273  	case Present:
   274  		return []byte(strconv.FormatInt(src.Int, 10)), nil
   275  	case Null:
   276  		return []byte("null"), nil
   277  	case Undefined:
   278  		return nil, errUndefined
   279  	}
   280  
   281  	return nil, errBadStatus
   282  }
   283  
   284  func (dst *Int8) UnmarshalJSON(b []byte) error {
   285  	var n *int64
   286  	err := json.Unmarshal(b, &n)
   287  	if err != nil {
   288  		return err
   289  	}
   290  
   291  	if n == nil {
   292  		*dst = Int8{Status: Null}
   293  	} else {
   294  		*dst = Int8{Int: *n, Status: Present}
   295  	}
   296  
   297  	return nil
   298  }
   299  

View as plain text