Repository: fxamacker/cbor
Pull request #472 (2024-01-18 19:25:41)

Add options to support byte string map keys as struct field names
<!--
Thank you for your interest in contributing to fxamacker/cbor!
-->

### Description

Introduces one encode option and one decode option to control the CBOR major type of struct field names.

The current behavior on encode is to produce text strings for field names, unless a field has the `keyasint` tag option, in which case it will serialize as either a positive or negative CBOR int. That remains the default encode behavior, with the new option to produce byte strings instead of text strings for fields that do not have the `keyasint` tag option.

Decoding a CBOR map containing a byte string key into a Go struct value currently produces an error. By overriding the default value of the new decode option, byte string map keys can also be recognized as struct field names.

<!-- For code contributions, please complete all the items below this line. -->
<!-- For documentation-only contributions, please delete everything below this line. -->

#### PR Was Proposed and Welcomed in Currently Open Issue

- [x] This PR was proposed and welcomed by maintainer(s) in issue #471
- [x] Closes or Updates Issue #471 

#### Checklist (for code PR only, ignore for docs PR)

- [x] Include unit tests that cover the new code
- [x] Pass all unit tests 
- [x] Pass all 18 ci linters (golint, gosec, staticcheck, etc.)
- [x] Sign each commit with your real name and email.  
      Last line of each commit message should be in this format:  
      Signed-off-by: Firstname Lastname <firstname.lastname@example.com>
- [x] Certify the Developer's Certificate of Origin 1.1
      (see next section).

#### Certify the Developer's Certificate of Origin 1.1

- [x] By marking this item as completed, I certify 
      the Developer Certificate of Origin 1.1.

```
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
660 York Street, Suite 102,
San Francisco, CA 94110 USA

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.

Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.
```




Patch:
diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml
index f49763c..740de41 100644
--- a/.github/workflows/codeql-analysis.yml
+++ b/.github/workflows/codeql-analysis.yml
@@ -32,14 +32,14 @@ jobs:
 
     # Initializes the CodeQL tools for scanning.
     - name: Initialize CodeQL
-      uses: github/codeql-action/init@e5f05b81d5b6ff8cfa111c80c22c5fd02a384118 # v3.23.0
+      uses: github/codeql-action/init@012739e5082ff0c22ca6d6ab32e07c36df03c4a4 # v3.22.12
       with:
         languages: ${{ matrix.language }}
 
     # Autobuild attempts to build any compiled languages  (C/C++, C#, or Java).
     # If this step fails, then you should remove it and run the build manually (see below)
     - name: Autobuild
-      uses: github/codeql-action/autobuild@e5f05b81d5b6ff8cfa111c80c22c5fd02a384118 # v3.23.0
+      uses: github/codeql-action/autobuild@012739e5082ff0c22ca6d6ab32e07c36df03c4a4 # v3.22.12
 
     - name: Perform CodeQL Analysis
-      uses: github/codeql-action/analyze@e5f05b81d5b6ff8cfa111c80c22c5fd02a384118 # v3.23.0
+      uses: github/codeql-action/analyze@012739e5082ff0c22ca6d6ab32e07c36df03c4a4 # v3.22.12
diff --git a/cache.go b/cache.go
index 2fdf114..8a4a5c8 100644
--- a/cache.go
+++ b/cache.go
@@ -224,6 +224,15 @@ func getEncodingStructType(t reflect.Type) (*encodingStructType, error) {
 			copy(flds[i].cborName[n:], flds[i].name)
 			e.Reset()
 
+			// If cborName contains a text string, then cborNameByteString contains a
+			// string that has the byte string major type but is otherwise identical to
+			// cborName.
+			flds[i].cborNameByteString = make([]byte, len(flds[i].cborName))
+			copy(flds[i].cborNameByteString, flds[i].cborName)
+			// Reset encoded CBOR type to byte string, preserving the "additional
+			// information" bits:
+			flds[i].cborNameByteString[0] = byte(cborTypeByteString) | (flds[i].cborNameByteString[0] & 0x1f)
+
 			hasKeyAsStr = true
 		}
 
diff --git a/decode.go b/decode.go
index 0c6df07..015ac9d 100644
--- a/decode.go
+++ b/decode.go
@@ -429,6 +429,23 @@ func (bstsm ByteStringToStringMode) valid() bool {
 	return bstsm >= 0 && bstsm < maxByteStringToStringMode
 }
 
+// FieldNameByteStringMode specifies the behavior when decoding a CBOR byte string map key as a Go struct field name.
+type FieldNameByteStringMode int
+
+const (
+	// FieldNameByteStringForbidden generates an error on an attempt to decode a CBOR byte string map key as a Go struct field name.
+	FieldNameByteStringForbidden FieldNameByteStringMode = iota
+
+	// FieldNameByteStringAllowed permits CBOR byte string map keys to be recognized as Go struct field names.
+	FieldNameByteStringAllowed
+
+	maxFieldNameByteStringMode
+)
+
+func (fnbsm FieldNameByteStringMode) valid() bool {
+	return fnbsm >= 0 && fnbsm < maxFieldNameByteStringMode
+}
+
 // DecOptions specifies decoding options.
 type DecOptions struct {
 	// DupMapKey specifies whether to enforce duplicate map key.
@@ -493,6 +510,10 @@ type DecOptions struct {
 
 	// ByteStringToString specifies the behavior when decoding a CBOR byte string into a Go string.
 	ByteStringToString ByteStringToStringMode
+
+	// FieldNameByteString specifies the behavior when decoding a CBOR byte string map key as a
+	// Go struct field name.
+	FieldNameByteString FieldNameByteStringMode
 }
 
 // DecMode returns DecMode with immutable options and no tags (safe for concurrency).
@@ -613,6 +634,9 @@ func (opts DecOptions) decMode() (*decMode, error) {
 	if !opts.ByteStringToString.valid() {
 		return nil, errors.New("cbor: invalid ByteStringToString " + strconv.Itoa(int(opts.ByteStringToString)))
 	}
+	if !opts.FieldNameByteString.valid() {
+		return nil, errors.New("cbor: invalid FieldNameByteString " + strconv.Itoa(int(opts.FieldNameByteString)))
+	}
 	dm := decMode{
 		dupMapKey:             opts.DupMapKey,
 		timeTag:               opts.TimeTag,
@@ -630,6 +654,7 @@ func (opts DecOptions) decMode() (*decMode, error) {
 		bigIntDec:             opts.BigIntDec,
 		defaultByteStringType: opts.DefaultByteStringType,
 		byteStringToString:    opts.ByteStringToString,
+		fieldNameByteString:   opts.FieldNameByteString,
 	}
 	return &dm, nil
 }
@@ -698,6 +723,7 @@ type decMode struct {
 	bigIntDec             BigIntDecMode
 	defaultByteStringType reflect.Type
 	byteStringToString    ByteStringToStringMode
+	fieldNameByteString   FieldNameByteStringMode
 }
 
 var defaultDecMode, _ = DecOptions{}.decMode()
@@ -720,6 +746,7 @@ func (dm *decMode) DecOptions() DecOptions {
 		BigIntDec:             dm.bigIntDec,
 		DefaultByteStringType: dm.defaultByteStringType,
 		ByteStringToString:    dm.byteStringToString,
+		FieldNameByteString:   dm.fieldNameByteString,
 	}
 }
 
@@ -1848,15 +1875,19 @@ func (d *decoder) parseMapToStruct(v reflect.Value, tInfo *typeInfo) error { //n
 		var k interface{} // Used by duplicate map key detection
 
 		t := d.nextCBORType()
-		if t == cborTypeTextString {
+		if t == cborTypeTextString || (t == cborTypeByteString && d.dm.fieldNameByteString == FieldNameByteStringAllowed) {
 			var keyBytes []byte
-			keyBytes, lastErr = d.parseTextString()
-			if lastErr != nil {
-				if err == nil {
-					err = lastErr
+			if t == cborTypeTextString {
+				keyBytes, lastErr = d.parseTextString()
+				if lastErr != nil {
+					if err == nil {
+						err = lastErr
+					}
+					d.skip() // skip value
+					continue
 				}
-				d.skip() // skip value
-				continue
+			} else { // cborTypeByteString
+				keyBytes, _ = d.parseByteString()
 			}
 
 			keyLen := len(keyBytes)
diff --git a/encode.go b/encode.go
index eba09c5..5e9c5a4 100644
--- a/encode.go
+++ b/encode.go
@@ -340,6 +340,23 @@ func (om OmitEmptyMode) valid() bool {
 	return om >= 0 && om < maxOmitEmptyMode
 }
 
+// FieldNameMode specifies the CBOR type to use when encoding struct field names.
+type FieldNameMode int
+
+const (
+	// FieldNameToTextString encodes struct fields to CBOR text string (major type 3).
+	FieldNameToTextString FieldNameMode = iota
+
+	// FieldNameToTextString encodes struct fields to CBOR byte string (major type 2).
+	FieldNameToByteString
+
+	maxFieldNameMode
+)
+
+func (fnm FieldNameMode) valid() bool {
+	return fnm >= 0 && fnm < maxFieldNameMode
+}
+
 // EncOptions specifies encoding options.
 type EncOptions struct {
 	// Sort specifies sorting order.
@@ -381,6 +398,9 @@ type EncOptions struct {
 	// - CBOR text string (major type 3) is default
 	// - CBOR byte string (major type 2)
 	String StringMode
+
+	// FieldName specifies the CBOR type to use when encoding struct field names.
+	FieldName FieldNameMode
 }
 
 // CanonicalEncOptions returns EncOptions for "Canonical CBOR" encoding,
@@ -563,6 +583,9 @@ func (opts EncOptions) encMode() (*encMode, error) {
 	if err != nil {
 		return nil, err
 	}
+	if !opts.FieldName.valid() {
+		return nil, errors.New("cbor: invalid FieldName " + strconv.Itoa(int(opts.FieldName)))
+	}
 	em := encMode{
 		sort:            opts.Sort,
 		shortestFloat:   opts.ShortestFloat,
@@ -577,6 +600,7 @@ func (opts EncOptions) encMode() (*encMode, error) {
 		omitEmpty:       opts.OmitEmpty,
 		stringType:      opts.String,
 		stringMajorType: stringMajorType,
+		fieldName:       opts.FieldName,
 	}
 	return &em, nil
 }
@@ -603,6 +627,7 @@ type encMode struct {
 	omitEmpty       OmitEmptyMode
 	stringType      StringMode
 	stringMajorType cborType
+	fieldName       FieldNameMode
 }
 
 var defaultEncMode, _ = EncOptions{}.encMode()
@@ -621,6 +646,7 @@ func (em *encMode) EncOptions() EncOptions {
 		TagsMd:        em.tagsMd,
 		OmitEmpty:     em.omitEmpty,
 		String:        em.stringType,
+		FieldName:     em.fieldName,
 	}
 }
 
@@ -1137,7 +1163,11 @@ func encodeFixedLengthStruct(e *encoderBuffer, em *encMode, v reflect.Value, fld
 
 	for i := 0; i < len(flds); i++ {
 		f := flds[i]
-		e.Write(f.cborName)
+		if !f.keyAsInt && em.fieldName == FieldNameToByteString {
+			e.Write(f.cborNameByteString)
+		} else { // int or text string
+			e.Write(f.cborName)
+		}
 
 		fv := v.Field(f.idx[0])
 		if err := f.ef(e, em, fv); err != nil {
@@ -1189,7 +1219,11 @@ func encodeStruct(e *encoderBuffer, em *encMode, v reflect.Value) (err error) {
 			}
 		}
 
-		kve.Write(f.cborName)
+		if !f.keyAsInt && em.fieldName == FieldNameToByteString {
+			kve.Write(f.cborNameByteString)
+		} else { // int or text string
+			kve.Write(f.cborName)
+		}
 
 		if err := f.ef(kve, em, fv); err != nil {
 			putEncoderBuffer(kve)
diff --git a/structfields.go b/structfields.go
index e811aa1..23a12be 100644
--- a/structfields.go
+++ b/structfields.go
@@ -10,17 +10,18 @@ import (
 )
 
 type field struct {
-	name      string
-	nameAsInt int64 // used to decoder to match field name with CBOR int
-	cborName  []byte
-	idx       []int
-	typ       reflect.Type
-	ef        encodeFunc
-	ief       isEmptyFunc
-	typInfo   *typeInfo // used to decoder to reuse type info
-	tagged    bool      // used to choose dominant field (at the same level tagged fields dominate untagged fields)
-	omitEmpty bool      // used to skip empty field
-	keyAsInt  bool      // used to encode/decode field name as int
+	name               string
+	nameAsInt          int64 // used to decoder to match field name with CBOR int
+	cborName           []byte
+	cborNameByteString []byte // major type 2 name encoding iff cborName has major type 3
+	idx                []int
+	typ                reflect.Type
+	ef                 encodeFunc
+	ief                isEmptyFunc
+	typInfo            *typeInfo // used to decoder to reuse type info
+	tagged             bool      // used to choose dominant field (at the same level tagged fields dominate untagged fields)
+	omitEmpty          bool      // used to skip empty field
+	keyAsInt           bool      // used to encode/decode field name as int
 }
 
 type fields []*field


Test patch:
diff --git a/decode_test.go b/decode_test.go
index 8da9899..a7a583f 100644
--- a/decode_test.go
+++ b/decode_test.go
@@ -8302,6 +8302,70 @@ func TestUnmarshalByteStringToString(t *testing.T) {
 	}
 }
 
+func TestDecModeInvalidFieldNameByteStringMode(t *testing.T) {
+	for _, tc := range []struct {
+		name         string
+		opts         DecOptions
+		wantErrorMsg string
+	}{
+		{
+			name:         "below range of valid modes",
+			opts:         DecOptions{FieldNameByteString: -1},
+			wantErrorMsg: "cbor: invalid FieldNameByteString -1",
+		},
+		{
+			name:         "above range of valid modes",
+			opts:         DecOptions{FieldNameByteString: 101},
+			wantErrorMsg: "cbor: invalid FieldNameByteString 101",
+		},
+	} {
+		t.Run(tc.name, func(t *testing.T) {
+			_, err := tc.opts.DecMode()
+			if err == nil {
+				t.Errorf("DecMode() didn't return an error")
+			} else if err.Error() != tc.wantErrorMsg {
+				t.Errorf("DecMode() returned error %q, want %q", err.Error(), tc.wantErrorMsg)
+			}
+		})
+	}
+}
+
+func TestUnmarshalFieldNameByteString(t *testing.T) {
+	allowed, err := DecOptions{
+		FieldNameByteString: FieldNameByteStringAllowed,
+	}.DecMode()
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	var s struct {
+		F int64 `json:"f"`
+	}
+
+	err = allowed.Unmarshal(hexDecode("a1414601"), &s) // {h'46': 1}
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if s.F != 1 {
+		t.Errorf("expected field F to be set to 1, got %d", s.F)
+	}
+
+	forbidden, err := DecOptions{
+		FieldNameByteString: FieldNameByteStringForbidden,
+	}.DecMode()
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	const wantMsg = "cbor: cannot unmarshal byte string into Go value of type string (map key is of type byte string and cannot be used to match struct field name)"
+	if err := forbidden.Unmarshal(hexDecode("a1414601"), &s); err == nil {
+		t.Errorf("expected non-nil error")
+	} else if gotMsg := err.Error(); gotMsg != wantMsg {
+		t.Errorf("expected error %q, got %q", wantMsg, gotMsg)
+	}
+}
+
 func isCBORNil(data []byte) bool {
 	return len(data) > 0 && (data[0] == 0xf6 || data[0] == 0xf7)
 }
diff --git a/encode_test.go b/encode_test.go
index ec92334..516a248 100644
--- a/encode_test.go
+++ b/encode_test.go
@@ -3698,6 +3698,34 @@ func TestEncModeStringType(t *testing.T) {
 	}
 }
 
+func TestEncModeInvalidFieldNameMode(t *testing.T) {
+	for _, tc := range []struct {
+		name         string
+		opts         EncOptions
+		wantErrorMsg string
+	}{
+		{
+			name:         "",
+			opts:         EncOptions{FieldName: -1},
+			wantErrorMsg: "cbor: invalid FieldName -1",
+		},
+		{
+			name:         "",
+			opts:         EncOptions{FieldName: 101},
+			wantErrorMsg: "cbor: invalid FieldName 101",
+		},
+	} {
+		t.Run(tc.name, func(t *testing.T) {
+			_, err := tc.opts.EncMode()
+			if err == nil {
+				t.Errorf("EncMode() didn't return an error")
+			} else if err.Error() != tc.wantErrorMsg {
+				t.Errorf("EncMode() returned error %q, want %q", err.Error(), tc.wantErrorMsg)
+			}
+		})
+	}
+}
+
 func TestEncIndefiniteLengthOption(t *testing.T) {
 	// Default option allows indefinite length items
 	var buf bytes.Buffer
@@ -4055,3 +4083,69 @@ func TestMarshalStringType(t *testing.T) {
 		})
 	}
 }
+
+func TestMarshalFieldNameType(t *testing.T) {
+	for _, tc := range []struct {
+		name string
+		opts EncOptions
+		in   interface{}
+		want []byte
+	}{
+		{
+			name: "fixed-length to text string",
+			opts: EncOptions{FieldName: FieldNameToTextString},
+			in: struct {
+				F1 int `cbor:"1,keyasint"`
+				F2 int `cbor:"a"`
+				F3 int `cbor:"-3,keyasint"`
+			}{},
+			want: hexDecode("a301006161002200"),
+		},
+		{
+			name: "fixed-length to byte string",
+			opts: EncOptions{FieldName: FieldNameToByteString},
+			in: struct {
+				F1 int `cbor:"1,keyasint"`
+				F2 int `cbor:"a"`
+				F3 int `cbor:"-3,keyasint"`
+			}{},
+			want: hexDecode("a301004161002200"),
+		},
+		{
+			name: "variable-length to text string",
+			opts: EncOptions{FieldName: FieldNameToTextString},
+			in: struct {
+				F1 int `cbor:"1,omitempty,keyasint"`
+				F2 int `cbor:"a,omitempty"`
+				F3 int `cbor:"-3,omitempty,keyasint"`
+			}{F1: 7, F2: 7, F3: 7},
+			want: hexDecode("a301076161072207"),
+		},
+		{
+			name: "variable-length to byte string",
+			opts: EncOptions{FieldName: FieldNameToByteString},
+			in: struct {
+				F1 int `cbor:"1,omitempty,keyasint"`
+				F2 int `cbor:"a,omitempty"`
+				F3 int `cbor:"-3,omitempty,keyasint"`
+			}{F1: 7, F2: 7, F3: 7},
+			want: hexDecode("a301074161072207"),
+		},
+	} {
+		t.Run(tc.name, func(t *testing.T) {
+			em, err := tc.opts.EncMode()
+			if err != nil {
+				t.Fatal(err)
+			}
+
+			got, err := em.Marshal(tc.in)
+			if err != nil {
+				t.Errorf("unexpected error from Marshal(%q): %v", tc.in, err)
+			}
+
+			if !bytes.Equal(got, tc.want) {
+				t.Errorf("Marshal(%q): wanted %x, got %x", tc.in, tc.want, got)
+			}
+		})
+	}
+}