Skip to content

Enable fields to be marked for omission from a struct #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions syntax/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ func newTypeDecoder(t reflect.Type) decoderFunc {

///// Specific decoders below

func omitDecoder(d *decodeState, v reflect.Value, opts decOpts) int {
// This space intentionally left blank
return 0
}

//////////

func unmarshalerDecoder(d *decodeState, v reflect.Value, opts decOpts) int {
um, ok := v.Interface().(Unmarshaler)
if !ok {
Expand Down Expand Up @@ -313,6 +320,11 @@ func newStructDecoder(t reflect.Type) decoderFunc {
tag := f.Tag.Get("tls")
tagOpts := parseTag(tag)

if tagOpts[omitOption] > 0 {
sd.fieldDecs[i] = omitDecoder
continue
}

sd.fieldOpts[i] = decOpts{
head: tagOpts["head"],
max: tagOpts["max"],
Expand Down
11 changes: 11 additions & 0 deletions syntax/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ func newTypeEncoder(t reflect.Type) encoderFunc {

///// Specific encoders below

func omitEncoder(e *encodeState, v reflect.Value, opts encOpts) {
// This space intentionally left blank
}

//////////

func marshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
if v.Kind() == reflect.Ptr && v.IsNil() {
panic(fmt.Errorf("Cannot encode nil pointer"))
Expand Down Expand Up @@ -243,6 +249,11 @@ func newStructEncoder(t reflect.Type) encoderFunc {
tag := f.Tag.Get("tls")
tagOpts := parseTag(tag)

if tagOpts[omitOption] > 0 {
se.fieldEncs[i] = omitEncoder
continue
}

se.fieldOpts[i] = encOpts{
head: tagOpts["head"],
max: tagOpts["max"],
Expand Down
12 changes: 12 additions & 0 deletions syntax/success_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ func TestSuccessCases(t *testing.T) {
value: struct{ V *uint16 }{V: &dummyUint16},
encoding: unhex("B0A0"),
},
"struct-omit": {
value: struct {
A uint16
B uint16 `tls:"omit"`
C uint16
}{
A: 0xA0A0,
B: 0x0000, // Can be anything on marshal, but will be zero on unmarshal
C: 0xC0C0,
},
encoding: unhex("A0A0C0C0"),
},

// Marshaler
"marshaler": {
Expand Down
6 changes: 6 additions & 0 deletions syntax/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
type tagOptions map[string]uint

var (
omitOption = "omit"
varintOption = "varint"

headOptionNone = "none"
Expand All @@ -24,6 +25,11 @@ var (
func parseTag(tag string) tagOptions {
opts := tagOptions{}
for _, token := range strings.Split(tag, ",") {
if token == omitOption {
opts[omitOption] = 1
break
}

if token == varintOption {
opts[varintOption] = 1
continue
Expand Down