diff --git a/asn1_test.go b/asn1_test.go index eebb0a9..5b74f68 100644 --- a/asn1_test.go +++ b/asn1_test.go @@ -430,6 +430,38 @@ func TestDerSet(t *testing.T) { } } +func TestIgnore(t *testing.T) { + type Type struct { + B string + C bool `asn1:"-"` + } + encodeTest := testCase{ + Type{"abc", true}, + []byte{ + // SEQ LEN=8 + 0x30, 0x05, + // OCTETSTRING LEN=3 + 0x04, 0x03, + // "abc" + 0x61, 0x62, 0x63, + }, + } + decodeTest := testCase{ + Type{"abc", false}, + []byte{ + // SEQ LEN=8 + 0x30, 0x05, + // OCTETSTRING LEN=3 + 0x04, 0x03, + // "abc" + 0x61, 0x62, 0x63, + }, + } + ctx := NewContext() + testEncode(t, ctx, "", encodeTest) + testDecode(t, ctx, "", decodeTest) +} + func TestOptional(t *testing.T) { type Type struct { A int `asn1:"optional"` diff --git a/context.go b/context.go index ad78969..46680da 100644 --- a/context.go +++ b/context.go @@ -169,6 +169,10 @@ func (ctx *Context) AddChoice(choice string, entries []Choice) error { if err != nil { return err } + // Skip if the ignore tag is given + if opts == nil { + continue + } if opts.choice != nil { // TODO Add support for nested choices. return syntaxError( diff --git a/decode.go b/decode.go index 0506723..262943b 100644 --- a/decode.go +++ b/decode.go @@ -138,6 +138,10 @@ func (ctx *Context) DecodeWithOptions(data []byte, obj interface{}, options stri if err != nil { return nil, err } + // Return nil if the ignore tag is given + if opts == nil { + return + } value := reflect.ValueOf(obj) switch value.Kind() { @@ -341,6 +345,10 @@ func (ctx *Context) getExpectedFieldElements(value reflect.Value) ([]expectedFie if err != nil { return nil, err } + // Skip if the ignore tag is given + if opts == nil { + continue + } // Expand choices raw := &rawValue{} if opts.choice == nil { diff --git a/encode.go b/encode.go index 964441d..a724ead 100644 --- a/encode.go +++ b/encode.go @@ -23,6 +23,10 @@ func (ctx *Context) EncodeWithOptions(obj interface{}, options string) (data []b if err != nil { return nil, err } + // Return nil if the ignore tag is given + if opts == nil { + return + } value := reflect.ValueOf(obj) raw, err := ctx.encode(value, opts) @@ -230,6 +234,10 @@ func (ctx *Context) getRawValuesFromFields(value reflect.Value) ([]*rawValue, er if err != nil { return nil, err } + // Skip if the ignore tag is given + if opts == nil { + continue + } raw, err := ctx.encode(fieldValue, opts) if err != nil { return nil, err diff --git a/options.go b/options.go index d744c58..bb2be7e 100644 --- a/options.go +++ b/options.go @@ -38,8 +38,11 @@ func (opts *fieldOptions) validate() error { return nil } -// parseOption returns a parsed fieldOptions or an error. +// parseOption returns a parsed fieldOptions or an error. Returns nil for the ignore tag "-". func parseOptions(s string) (*fieldOptions, error) { + if s == "-" { + return nil, nil + } var opts fieldOptions for _, token := range strings.Split(s, ",") { args := strings.Split(strings.TrimSpace(token), ":")