Skip to content
This repository has been archived by the owner on Mar 13, 2019. It is now read-only.

Add an ignore tag "-" like encoding/json #4

Open
wants to merge 2 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
32 changes: 32 additions & 0 deletions asn1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
4 changes: 4 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 8 additions & 0 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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), ":")
Expand Down