3838ErrInvalid = errors .New ("invalid state detected" )
3939ErrInvalidIndex = errors .New ("invalid index referenced" )
4040
41+ ErrExpectedObject = errors .New ("invalid value, expected object" )
42+
4143rawJSONArray = []byte ("[]" )
4244rawJSONObject = []byte ("{}" )
4345rawJSONNull = []byte ("null" )
@@ -134,6 +136,10 @@ func (n *lazyNode) UnmarshalJSON(data []byte) error {
134136}
135137
136138func (n * partialDoc ) TrustMarshalJSON (buf * bytes.Buffer ) error {
139+ if n .obj == nil {
140+ return ErrExpectedObject
141+ }
142+
137143if err := buf .WriteByte ('{' ); err != nil {
138144return err
139145}
@@ -557,6 +563,10 @@ func findObject(pd *container, path string, options *ApplyOptions) (container, s
557563}
558564
559565func (d * partialDoc ) set (key string , val * lazyNode , options * ApplyOptions ) error {
566+ if d .obj == nil {
567+ return ErrExpectedObject
568+ }
569+
560570found := false
561571for _ , k := range d .keys {
562572if k == key {
@@ -579,6 +589,11 @@ func (d *partialDoc) get(key string, options *ApplyOptions) (*lazyNode, error) {
579589if key == "" {
580590return d .self , nil
581591}
592+
593+ if d .obj == nil {
594+ return nil , ErrExpectedObject
595+ }
596+
582597v , ok := d .obj [key ]
583598if ! ok {
584599return v , errors .Wrapf (ErrMissing , "unable to get nonexistent key: %s" , key )
@@ -587,6 +602,10 @@ func (d *partialDoc) get(key string, options *ApplyOptions) (*lazyNode, error) {
587602}
588603
589604func (d * partialDoc ) remove (key string , options * ApplyOptions ) error {
605+ if d .obj == nil {
606+ return ErrExpectedObject
607+ }
608+
590609_ , ok := d .obj [key ]
591610if ! ok {
592611if options .AllowMissingPathOnRemove {
0 commit comments