github.com/vmware/govmomi@v0.43.0/govc/host/esxcli/response.go (about)

     1  /*
     2  Copyright (c) 2014-2023 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  package esxcli
    17  
    18  import (
    19  	"io"
    20  
    21  	"github.com/vmware/govmomi/vim25/xml"
    22  )
    23  
    24  type Values map[string][]string
    25  
    26  type Response struct {
    27  	Info   *CommandInfoMethod `json:"info"`
    28  	Values []Values           `json:"values"`
    29  	String string             `json:"string"`
    30  }
    31  
    32  func (v Values) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
    33  	for {
    34  		t, err := d.Token()
    35  		if err != nil {
    36  			if err == io.EOF {
    37  				return nil
    38  			}
    39  			return err
    40  		}
    41  
    42  		if s, ok := t.(xml.StartElement); ok {
    43  			t, err = d.Token()
    44  			if err != nil {
    45  				return err
    46  			}
    47  
    48  			key := s.Name.Local
    49  			var val string
    50  			if c, ok := t.(xml.CharData); ok {
    51  				val = string(c)
    52  			}
    53  			v[key] = append(v[key], val)
    54  		}
    55  	}
    56  }
    57  
    58  func (r *Response) Type(start xml.StartElement) string {
    59  	for _, a := range start.Attr {
    60  		if a.Name.Local == "type" {
    61  			return a.Value
    62  		}
    63  	}
    64  	return ""
    65  }
    66  
    67  func (r *Response) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
    68  	stype := r.Type(start)
    69  
    70  	if stype != "ArrayOfDataObject" {
    71  		switch stype {
    72  		case "xsd:string":
    73  			return d.DecodeElement(&r.String, &start)
    74  		}
    75  		v := Values{}
    76  		if err := d.DecodeElement(&v, &start); err != nil {
    77  			return err
    78  		}
    79  		r.Values = append(r.Values, v)
    80  		return nil
    81  	}
    82  
    83  	for {
    84  		t, err := d.Token()
    85  		if err != nil {
    86  			if err == io.EOF {
    87  				return nil
    88  			}
    89  			return err
    90  		}
    91  
    92  		if s, ok := t.(xml.StartElement); ok {
    93  			if s.Name.Local == "DataObject" {
    94  				v := Values{}
    95  				if err := d.DecodeElement(&v, &s); err != nil {
    96  					return err
    97  				}
    98  				r.Values = append(r.Values, v)
    99  			}
   100  		}
   101  	}
   102  }