Help us shape the future of Go! Take the 2026 Go Developer Survey

Source file src/database/sql/convert.go

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Type conversions for Scan.
     6  
     7  package sql
     8  
     9  import (
    10  	"bytes"
    11  	"database/sql/driver"
    12  	"database/sql/internal"
    13  	"errors"
    14  	"fmt"
    15  	"reflect"
    16  	"strconv"
    17  	"time"
    18  	"unicode"
    19  	"unicode/utf8"
    20  	_ "unsafe" // for linkname
    21  	"uuid"
    22  )
    23  
    24  var errNilPtr = errors.New("destination pointer is nil") // embedded in descriptive error
    25  
    26  func describeNamedValue(nv *driver.NamedValue) string {
    27  	if len(nv.Name) == 0 {
    28  		return fmt.Sprintf("$%d", nv.Ordinal)
    29  	}
    30  	return fmt.Sprintf("with name %q", nv.Name)
    31  }
    32  
    33  func validateNamedValueName(name string) error {
    34  	if len(name) == 0 {
    35  		return nil
    36  	}
    37  	r, _ := utf8.DecodeRuneInString(name)
    38  	if unicode.IsLetter(r) {
    39  		return nil
    40  	}
    41  	return fmt.Errorf("name %q does not begin with a letter", name)
    42  }
    43  
    44  // ccChecker wraps the driver.ColumnConverter and allows it to be used
    45  // as if it were a NamedValueChecker. If the driver ColumnConverter
    46  // is not present then the NamedValueChecker will return driver.ErrSkip.
    47  type ccChecker struct {
    48  	cci  driver.ColumnConverter
    49  	want int
    50  }
    51  
    52  func (c ccChecker) CheckNamedValue(nv *driver.NamedValue) error {
    53  	if c.cci == nil {
    54  		return driver.ErrSkip
    55  	}
    56  	// The column converter shouldn't be called on any index
    57  	// it isn't expecting. The final error will be thrown
    58  	// in the argument converter loop.
    59  	index := nv.Ordinal - 1
    60  	if c.want >= 0 && c.want <= index {
    61  		return nil
    62  	}
    63  
    64  	// First, see if the value itself knows how to convert
    65  	// itself to a driver type. For example, a NullString
    66  	// struct changing into a string or nil.
    67  	if vr, ok := nv.Value.(driver.Valuer); ok {
    68  		sv, err := callValuerValue(vr)
    69  		if err != nil {
    70  			return err
    71  		}
    72  		if !driver.IsValue(sv) {
    73  			return fmt.Errorf("non-subset type %T returned from Value", sv)
    74  		}
    75  		nv.Value = sv
    76  	}
    77  
    78  	// Second, ask the column to sanity check itself. For
    79  	// example, drivers might use this to make sure that
    80  	// an int64 values being inserted into a 16-bit
    81  	// integer field is in range (before getting
    82  	// truncated), or that a nil can't go into a NOT NULL
    83  	// column before going across the network to get the
    84  	// same error.
    85  	var err error
    86  	arg := nv.Value
    87  	nv.Value, err = c.cci.ColumnConverter(index).ConvertValue(arg)
    88  	if err != nil {
    89  		return err
    90  	}
    91  	if !driver.IsValue(nv.Value) {
    92  		return fmt.Errorf("driver ColumnConverter error converted %T to unsupported type %T", arg, nv.Value)
    93  	}
    94  	return nil
    95  }
    96  
    97  // defaultCheckNamedValue wraps the default ColumnConverter to have the same
    98  // function signature as the CheckNamedValue in the driver.NamedValueChecker
    99  // interface.
   100  func defaultCheckNamedValue(nv *driver.NamedValue) (err error) {
   101  	nv.Value, err = driver.DefaultParameterConverter.ConvertValue(nv.Value)
   102  	return err
   103  }
   104  
   105  // driverArgsConnLocked converts arguments from callers of Stmt.Exec and
   106  // Stmt.Query into driver Values.
   107  //
   108  // The statement ds may be nil, if no statement is available.
   109  //
   110  // ci must be locked.
   111  func driverArgsConnLocked(ci driver.Conn, ds *driverStmt, args []any) ([]driver.NamedValue, error) {
   112  	nvargs := make([]driver.NamedValue, len(args))
   113  
   114  	// -1 means the driver doesn't know how to count the number of
   115  	// placeholders, so we won't sanity check input here and instead let the
   116  	// driver deal with errors.
   117  	want := -1
   118  
   119  	var si driver.Stmt
   120  	var cc ccChecker
   121  	if ds != nil {
   122  		si = ds.si
   123  		want = ds.si.NumInput()
   124  		cc.want = want
   125  	}
   126  
   127  	// Check all types of interfaces from the start.
   128  	// Drivers may opt to use the NamedValueChecker for special
   129  	// argument types, then return driver.ErrSkip to pass it along
   130  	// to the column converter.
   131  	nvc, ok := si.(driver.NamedValueChecker)
   132  	if !ok {
   133  		nvc, _ = ci.(driver.NamedValueChecker)
   134  	}
   135  	cci, ok := si.(driver.ColumnConverter)
   136  	if ok {
   137  		cc.cci = cci
   138  	}
   139  
   140  	// Loop through all the arguments, checking each one.
   141  	// If no error is returned simply increment the index
   142  	// and continue. However, if driver.ErrRemoveArgument
   143  	// is returned the argument is not included in the query
   144  	// argument list.
   145  	var err error
   146  	var n int
   147  	for _, arg := range args {
   148  		nv := &nvargs[n]
   149  		if np, ok := arg.(NamedArg); ok {
   150  			if err = validateNamedValueName(np.Name); err != nil {
   151  				return nil, err
   152  			}
   153  			arg = np.Value
   154  			nv.Name = np.Name
   155  		}
   156  		nv.Ordinal = n + 1
   157  		nv.Value = arg
   158  
   159  		// Checking sequence has four routes:
   160  		// A: 1. Default
   161  		// B: 1. NamedValueChecker 2. Column Converter 3. Default
   162  		// C: 1. NamedValueChecker 3. Default
   163  		// D: 1. Column Converter 2. Default
   164  		//
   165  		// The only time a Column Converter is called is first
   166  		// or after NamedValueConverter. If first it is handled before
   167  		// the nextCheck label. Thus for repeats tries only when the
   168  		// NamedValueConverter is selected should the Column Converter
   169  		// be used in the retry.
   170  		checker := defaultCheckNamedValue
   171  		nextCC := false
   172  		switch {
   173  		case nvc != nil:
   174  			nextCC = cci != nil
   175  			checker = nvc.CheckNamedValue
   176  		case cci != nil:
   177  			checker = cc.CheckNamedValue
   178  		}
   179  
   180  	nextCheck:
   181  		err = checker(nv)
   182  		switch err {
   183  		case nil:
   184  			n++
   185  			continue
   186  		case driver.ErrRemoveArgument:
   187  			nvargs = nvargs[:len(nvargs)-1]
   188  			continue
   189  		case driver.ErrSkip:
   190  			if nextCC {
   191  				nextCC = false
   192  				checker = cc.CheckNamedValue
   193  			} else {
   194  				checker = defaultCheckNamedValue
   195  			}
   196  			goto nextCheck
   197  		default:
   198  			return nil, fmt.Errorf("sql: converting argument %s type: %w", describeNamedValue(nv), err)
   199  		}
   200  	}
   201  
   202  	// Check the length of arguments after conversion to allow for omitted
   203  	// arguments.
   204  	if want != -1 && len(nvargs) != want {
   205  		return nil, fmt.Errorf("sql: expected %d arguments, got %d", want, len(nvargs))
   206  	}
   207  
   208  	return nvargs, nil
   209  }
   210  
   211  // convertAssign is the same as convertAssignRows, but without the optional
   212  // rows argument.
   213  //
   214  // convertAssign should be an internal detail,
   215  // but widely used packages access it using linkname.
   216  // Notable members of the hall of shame include:
   217  //   - ariga.io/entcache
   218  //
   219  // Do not remove or change the type signature.
   220  // See go.dev/issue/67401.
   221  //
   222  //go:linkname convertAssign
   223  func convertAssign(dest any, src any) error {
   224  	return convertAssignRows(dest, src, nil)
   225  }
   226  
   227  // ConvertAssign copies the value in src to the value pointed at by dest.
   228  // See the documentation on [Rows.Scan] for details on conversions.
   229  // dest must be a pointer or must implement [Scanner].
   230  //
   231  // Implementations of [driver.RowsColumnScanner] should pass through
   232  // their [driver.ScanContext] parameter.
   233  // In other cases, pass driver.ScanContext{} as the context.
   234  //
   235  // ConvertAssign is intended for use by driver implementations.
   236  // Most users should not need to use it directly.
   237  func ConvertAssign(scanCtx driver.ScanContext, dest any, src driver.Value) error {
   238  	rows, _ := internal.ScanContextValue(internal.ScanContext(scanCtx)).(*Rows)
   239  	return convertAssignRows(dest, src, rows)
   240  }
   241  
   242  // convertAssignRows copies to dest the value in src, converting it if possible.
   243  // An error is returned if the copy would result in loss of information.
   244  // dest should be a pointer type. If rows is passed in, the rows will
   245  // be used as the parent for any cursor values converted from a
   246  // driver.Rows to a *Rows.
   247  func convertAssignRows(dest, src any, rows *Rows) error {
   248  	// Common cases, without reflect.
   249  	switch s := src.(type) {
   250  	case int64:
   251  		switch d := dest.(type) {
   252  		case *int64:
   253  			if d == nil {
   254  				return errNilPtr
   255  			}
   256  			*d = s
   257  			return nil
   258  		case *int:
   259  			if d == nil {
   260  				return errNilPtr
   261  			}
   262  			if int64(int(s)) == s {
   263  				*d = int(s)
   264  				return nil
   265  			}
   266  			// Out of range for int, let the generic path produce the error.
   267  		}
   268  	case float64:
   269  		switch d := dest.(type) {
   270  		case *float64:
   271  			if d == nil {
   272  				return errNilPtr
   273  			}
   274  			*d = s
   275  			return nil
   276  		}
   277  	case string:
   278  		switch d := dest.(type) {
   279  		case *string:
   280  			if d == nil {
   281  				return errNilPtr
   282  			}
   283  			*d = s
   284  			return nil
   285  		case *[]byte:
   286  			if d == nil {
   287  				return errNilPtr
   288  			}
   289  			*d = []byte(s)
   290  			return nil
   291  		case *RawBytes:
   292  			if d == nil {
   293  				return errNilPtr
   294  			}
   295  			*d = rows.setrawbuf(append(rows.rawbuf(), s...))
   296  			return nil
   297  		case *uuid.UUID:
   298  			if d == nil {
   299  				return errNilPtr
   300  			}
   301  			u, err := uuid.Parse(s)
   302  			if err != nil {
   303  				return fmt.Errorf("converting driver.Value type string (%q) to a UUID: %v", s, err)
   304  			}
   305  			*d = u
   306  			return nil
   307  		}
   308  	case []byte:
   309  		switch d := dest.(type) {
   310  		case *string:
   311  			if d == nil {
   312  				return errNilPtr
   313  			}
   314  			*d = string(s)
   315  			return nil
   316  		case *any:
   317  			if d == nil {
   318  				return errNilPtr
   319  			}
   320  			*d = bytes.Clone(s)
   321  			return nil
   322  		case *[]byte:
   323  			if d == nil {
   324  				return errNilPtr
   325  			}
   326  			*d = bytes.Clone(s)
   327  			return nil
   328  		case *RawBytes:
   329  			if d == nil {
   330  				return errNilPtr
   331  			}
   332  			*d = s
   333  			return nil
   334  		case *uuid.UUID:
   335  			if d == nil {
   336  				return errNilPtr
   337  			}
   338  			if len(s) == len(*d) {
   339  				copy((*d)[:], s)
   340  				return nil
   341  			}
   342  			var u uuid.UUID
   343  			err := u.UnmarshalText(s)
   344  			if err != nil {
   345  				return fmt.Errorf("converting driver.Value type []byte (%q) to a UUID: %v", s, err)
   346  			}
   347  			*d = u
   348  			return nil
   349  		}
   350  	case time.Time:
   351  		switch d := dest.(type) {
   352  		case *time.Time:
   353  			*d = s
   354  			return nil
   355  		case *string:
   356  			*d = s.Format(time.RFC3339Nano)
   357  			return nil
   358  		case *[]byte:
   359  			if d == nil {
   360  				return errNilPtr
   361  			}
   362  			*d = s.AppendFormat(make([]byte, 0, len(time.RFC3339Nano)), time.RFC3339Nano)
   363  			return nil
   364  		case *RawBytes:
   365  			if d == nil {
   366  				return errNilPtr
   367  			}
   368  			*d = rows.setrawbuf(s.AppendFormat(rows.rawbuf(), time.RFC3339Nano))
   369  			return nil
   370  		}
   371  	case decimalDecompose:
   372  		switch d := dest.(type) {
   373  		case decimalCompose:
   374  			return d.Compose(s.Decompose(nil))
   375  		}
   376  	case nil:
   377  		switch d := dest.(type) {
   378  		case *any:
   379  			if d == nil {
   380  				return errNilPtr
   381  			}
   382  			*d = nil
   383  			return nil
   384  		case *[]byte:
   385  			if d == nil {
   386  				return errNilPtr
   387  			}
   388  			*d = nil
   389  			return nil
   390  		case *RawBytes:
   391  			if d == nil {
   392  				return errNilPtr
   393  			}
   394  			*d = nil
   395  			return nil
   396  		}
   397  	// The driver is returning a cursor the client may iterate over.
   398  	case driver.Rows:
   399  		d, ok := dest.(*Rows)
   400  		if ok {
   401  			if d == nil {
   402  				return errNilPtr
   403  			}
   404  			if rows == nil {
   405  				return errors.New("invalid context to convert cursor rows, missing parent *Rows")
   406  			}
   407  			// This is hazardous and not really correct: If the user provides us
   408  			// with the same *Rows for each Scan (which they very likely will),
   409  			// then this overwrites the previously-used Rows, including its mutexes.
   410  			// The chained row cancel function below will also repeatedly reference
   411  			// the same *Rows.
   412  			*d = Rows{
   413  				dc:          rows.dc,
   414  				releaseConn: func(error) {},
   415  				rowsi:       s,
   416  			}
   417  			// Chain the cancel function.
   418  			//
   419  			// This has problems:
   420  			//   - Repeatedly wrapping the cancel func is inefficient compared to
   421  			//     just storing a []*Rows of children.
   422  			//   - The cancel func is wrapped for each cursor read. If we scan N
   423  			//     rows, each with a child cursor, we end up with N chained cancel
   424  			//     funcs. (Also, if the user is reusing a Rows--see above--the cancel
   425  			//     funcs might all be referencing the same underlying Rows cursor.)
   426  			//   - It seems like it would be reasonable to invalidate a cursor
   427  			//     after advancing to the next parent row (the row which contains
   428  			//     the cursor). We don't do that now, and it isn't clear that we can
   429  			//     change this.
   430  			parentCancel := rows.cancel
   431  			rows.cancel = func() {
   432  				// When Rows.cancel is called, the closemu will be locked as well.
   433  				// So we can access rows.lasterr.
   434  				d.close(rows.lasterr)
   435  				if parentCancel != nil {
   436  					parentCancel()
   437  				}
   438  			}
   439  			return nil
   440  		}
   441  	}
   442  
   443  	var sv reflect.Value
   444  
   445  	switch d := dest.(type) {
   446  	case *string:
   447  		sv = reflect.ValueOf(src)
   448  		switch sv.Kind() {
   449  		case reflect.Bool,
   450  			reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
   451  			reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
   452  			reflect.Float32, reflect.Float64:
   453  			*d = asString(src)
   454  			return nil
   455  		}
   456  	case *[]byte:
   457  		sv = reflect.ValueOf(src)
   458  		if b, ok := asBytes(nil, sv); ok {
   459  			*d = b
   460  			return nil
   461  		}
   462  	case *RawBytes:
   463  		sv = reflect.ValueOf(src)
   464  		if b, ok := asBytes(rows.rawbuf(), sv); ok {
   465  			*d = rows.setrawbuf(b)
   466  			return nil
   467  		}
   468  	case *bool:
   469  		bv, err := driver.Bool.ConvertValue(src)
   470  		if err == nil {
   471  			*d = bv.(bool)
   472  		}
   473  		return err
   474  	case *any:
   475  		*d = src
   476  		return nil
   477  	}
   478  
   479  	if scanner, ok := dest.(Scanner); ok {
   480  		return scanner.Scan(src)
   481  	}
   482  
   483  	dpv := reflect.ValueOf(dest)
   484  	if dpv.Kind() != reflect.Pointer {
   485  		return errors.New("destination not a pointer")
   486  	}
   487  	if dpv.IsNil() {
   488  		return errNilPtr
   489  	}
   490  
   491  	if !sv.IsValid() {
   492  		sv = reflect.ValueOf(src)
   493  	}
   494  
   495  	dv := reflect.Indirect(dpv)
   496  	if sv.IsValid() && sv.Type().AssignableTo(dv.Type()) {
   497  		switch b := src.(type) {
   498  		case []byte:
   499  			dv.Set(reflect.ValueOf(bytes.Clone(b)))
   500  		default:
   501  			dv.Set(sv)
   502  		}
   503  		return nil
   504  	}
   505  
   506  	if dv.Kind() == sv.Kind() && sv.Type().ConvertibleTo(dv.Type()) {
   507  		dv.Set(sv.Convert(dv.Type()))
   508  		return nil
   509  	}
   510  
   511  	// The following conversions use a string value as an intermediate representation
   512  	// to convert between various numeric types.
   513  	//
   514  	// This also allows scanning into user defined types such as "type Int int64".
   515  	// For symmetry, also check for string destination types.
   516  	switch dv.Kind() {
   517  	case reflect.Pointer:
   518  		if src == nil {
   519  			dv.SetZero()
   520  			return nil
   521  		}
   522  		dv.Set(reflect.New(dv.Type().Elem()))
   523  		return convertAssignRows(dv.Interface(), src, rows)
   524  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   525  		if src == nil {
   526  			return fmt.Errorf("converting NULL to %s is unsupported", dv.Kind())
   527  		}
   528  		s := asString(src)
   529  		i64, err := strconv.ParseInt(s, 10, dv.Type().Bits())
   530  		if err != nil {
   531  			err = strconvErr(err)
   532  			return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
   533  		}
   534  		dv.SetInt(i64)
   535  		return nil
   536  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
   537  		if src == nil {
   538  			return fmt.Errorf("converting NULL to %s is unsupported", dv.Kind())
   539  		}
   540  		s := asString(src)
   541  		u64, err := strconv.ParseUint(s, 10, dv.Type().Bits())
   542  		if err != nil {
   543  			err = strconvErr(err)
   544  			return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
   545  		}
   546  		dv.SetUint(u64)
   547  		return nil
   548  	case reflect.Float32, reflect.Float64:
   549  		if src == nil {
   550  			return fmt.Errorf("converting NULL to %s is unsupported", dv.Kind())
   551  		}
   552  		s := asString(src)
   553  		f64, err := strconv.ParseFloat(s, dv.Type().Bits())
   554  		if err != nil {
   555  			err = strconvErr(err)
   556  			return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
   557  		}
   558  		dv.SetFloat(f64)
   559  		return nil
   560  	case reflect.String:
   561  		if src == nil {
   562  			return fmt.Errorf("converting NULL to %s is unsupported", dv.Kind())
   563  		}
   564  		switch v := src.(type) {
   565  		case string:
   566  			dv.SetString(v)
   567  			return nil
   568  		case []byte:
   569  			dv.SetString(string(v))
   570  			return nil
   571  		}
   572  	}
   573  
   574  	return fmt.Errorf("unsupported Scan, storing driver.Value type %T into type %T", src, dest)
   575  }
   576  
   577  func strconvErr(err error) error {
   578  	if ne, ok := err.(*strconv.NumError); ok {
   579  		return ne.Err
   580  	}
   581  	return err
   582  }
   583  
   584  func asString(src any) string {
   585  	switch v := src.(type) {
   586  	case string:
   587  		return v
   588  	case []byte:
   589  		return string(v)
   590  	}
   591  	rv := reflect.ValueOf(src)
   592  	switch rv.Kind() {
   593  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   594  		return strconv.FormatInt(rv.Int(), 10)
   595  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
   596  		return strconv.FormatUint(rv.Uint(), 10)
   597  	case reflect.Float64:
   598  		return strconv.FormatFloat(rv.Float(), 'g', -1, 64)
   599  	case reflect.Float32:
   600  		return strconv.FormatFloat(rv.Float(), 'g', -1, 32)
   601  	case reflect.Bool:
   602  		return strconv.FormatBool(rv.Bool())
   603  	}
   604  	return fmt.Sprintf("%v", src)
   605  }
   606  
   607  func asBytes(buf []byte, rv reflect.Value) (b []byte, ok bool) {
   608  	switch rv.Kind() {
   609  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   610  		return strconv.AppendInt(buf, rv.Int(), 10), true
   611  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
   612  		return strconv.AppendUint(buf, rv.Uint(), 10), true
   613  	case reflect.Float32:
   614  		return strconv.AppendFloat(buf, rv.Float(), 'g', -1, 32), true
   615  	case reflect.Float64:
   616  		return strconv.AppendFloat(buf, rv.Float(), 'g', -1, 64), true
   617  	case reflect.Bool:
   618  		return strconv.AppendBool(buf, rv.Bool()), true
   619  	case reflect.String:
   620  		s := rv.String()
   621  		return append(buf, s...), true
   622  	}
   623  	return
   624  }
   625  
   626  var valuerReflectType = reflect.TypeFor[driver.Valuer]()
   627  
   628  // callValuerValue returns vr.Value(), with one exception:
   629  // If vr.Value is an auto-generated method on a pointer type and the
   630  // pointer is nil, it would panic at runtime in the panicwrap
   631  // method. Treat it like nil instead.
   632  // Issue 8415.
   633  //
   634  // This is so people can implement driver.Value on value types and
   635  // still use nil pointers to those types to mean nil/NULL, just like
   636  // string/*string.
   637  //
   638  // This function is mirrored in the database/sql/driver package.
   639  func callValuerValue(vr driver.Valuer) (v driver.Value, err error) {
   640  	if rv := reflect.ValueOf(vr); rv.Kind() == reflect.Pointer &&
   641  		rv.IsNil() &&
   642  		rv.Type().Elem().Implements(valuerReflectType) {
   643  		return nil, nil
   644  	}
   645  	return vr.Value()
   646  }
   647  
   648  // decimal composes or decomposes a decimal value to and from individual parts.
   649  // There are four parts: a boolean negative flag, a form byte with three possible states
   650  // (finite=0, infinite=1, NaN=2), a base-2 big-endian integer
   651  // coefficient (also known as a significand) as a []byte, and an int32 exponent.
   652  // These are composed into a final value as "decimal = (neg) (form=finite) coefficient * 10 ^ exponent".
   653  // A zero length coefficient is a zero value.
   654  // The big-endian integer coefficient stores the most significant byte first (at coefficient[0]).
   655  // If the form is not finite the coefficient and exponent should be ignored.
   656  // The negative parameter may be set to true for any form, although implementations are not required
   657  // to respect the negative parameter in the non-finite form.
   658  //
   659  // Implementations may choose to set the negative parameter to true on a zero or NaN value,
   660  // but implementations that do not differentiate between negative and positive
   661  // zero or NaN values should ignore the negative parameter without error.
   662  // If an implementation does not support Infinity it may be converted into a NaN without error.
   663  // If a value is set that is larger than what is supported by an implementation,
   664  // an error must be returned.
   665  // Implementations must return an error if a NaN or Infinity is attempted to be set while neither
   666  // are supported.
   667  //
   668  // NOTE(kardianos): This is an experimental interface. See https://golang.org/issue/30870
   669  type decimal interface {
   670  	decimalDecompose
   671  	decimalCompose
   672  }
   673  
   674  type decimalDecompose interface {
   675  	// Decompose returns the internal decimal state in parts.
   676  	// If the provided buf has sufficient capacity, buf may be returned as the coefficient with
   677  	// the value set and length set as appropriate.
   678  	Decompose(buf []byte) (form byte, negative bool, coefficient []byte, exponent int32)
   679  }
   680  
   681  type decimalCompose interface {
   682  	// Compose sets the internal decimal value from parts. If the value cannot be
   683  	// represented then an error should be returned.
   684  	Compose(form byte, negative bool, coefficient []byte, exponent int32) error
   685  }
   686  

View as plain text