...

Source file src/victortoso.com/qapi-go/pkg/qapi/alternates.go

Documentation: victortoso.com/qapi-go/pkg/qapi

     1  package qapi
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  	"strings"
     8  )
     9  
    10  // This is a string value or the explicit lack of a string (null
    11  // pointer in C).  Intended for cases when 'optional absent' already
    12  // has a different meaning.
    13  //
    14  // Since: 2.10
    15  type StrOrNull struct {
    16  	// Options are:
    17  	// * s (string): the string value
    18  	// * n (nil): no string value
    19  	Value Any
    20  }
    21  
    22  func (s StrOrNull) MarshalJSON() ([]byte, error) {
    23  	typestr := fmt.Sprintf("%T", s.Value)
    24  	typestr = typestr[strings.LastIndex(typestr, ".")+1:]
    25  
    26  	// Runtime check for supported types
    27  	if typestr != "<nil>" &&
    28  		typestr != "string" &&
    29  		typestr != "*string" {
    30  		return nil, errors.New(fmt.Sprintf("Type is not supported: %s", typestr))
    31  	}
    32  
    33  	b, err := json.Marshal(s.Value)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	return b, nil
    39  }
    40  
    41  func (s *StrOrNull) UnmarshalJSON(data []byte) error {
    42  	// Check for null
    43  	{
    44  		var value *string
    45  		if err := StrictDecode(&value, data); err == nil && value == nil {
    46  			s.Value = nil
    47  			return nil
    48  		}
    49  	}
    50  	// Check for str
    51  	{
    52  		var value string
    53  		if err := StrictDecode(&value, data); err == nil {
    54  			s.Value = value
    55  			return nil
    56  		}
    57  	}
    58  	// Check type to error out nicely
    59  	{
    60  		var value Any
    61  		if err := json.Unmarshal(data, &value); err != nil {
    62  			return err
    63  		}
    64  		return errors.New(fmt.Sprintf("Unsupported type %T (value: %v)", value, value))
    65  	}
    66  }
    67  
    68  // Since: 4.1
    69  type BlockDirtyBitmapMergeSource struct {
    70  	// Options are:
    71  	// * local (string): name of the bitmap, attached to the same node as target bitmap.
    72  	// * external (BlockDirtyBitmap): bitmap with specified node
    73  	Value Any
    74  }
    75  
    76  func (s BlockDirtyBitmapMergeSource) MarshalJSON() ([]byte, error) {
    77  	typestr := fmt.Sprintf("%T", s.Value)
    78  	typestr = typestr[strings.LastIndex(typestr, ".")+1:]
    79  
    80  	// Runtime check for supported types
    81  	if typestr != "<nil>" &&
    82  		typestr != "string" &&
    83  		typestr != "BlockDirtyBitmap" {
    84  		return nil, errors.New(fmt.Sprintf("Type is not supported: %s", typestr))
    85  	}
    86  
    87  	b, err := json.Marshal(s.Value)
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  
    92  	return b, nil
    93  }
    94  
    95  func (s *BlockDirtyBitmapMergeSource) UnmarshalJSON(data []byte) error {
    96  	// Check for str
    97  	{
    98  		var value string
    99  		if err := StrictDecode(&value, data); err == nil {
   100  			s.Value = value
   101  			return nil
   102  		}
   103  	}
   104  	// Check for BlockDirtyBitmap
   105  	{
   106  		var value BlockDirtyBitmap
   107  		if err := StrictDecode(&value, data); err == nil {
   108  			s.Value = value
   109  			return nil
   110  		}
   111  	}
   112  	// Check type to error out nicely
   113  	{
   114  		var value Any
   115  		if err := json.Unmarshal(data, &value); err != nil {
   116  			return err
   117  		}
   118  		return errors.New(fmt.Sprintf("Unsupported type %T (value: %v)", value, value))
   119  	}
   120  }
   121  
   122  // Specifies which metadata structures should be guarded against unintended
   123  // overwriting.
   124  //
   125  // Since: 2.9
   126  type Qcow2OverlapChecks struct {
   127  	// Options are:
   128  	// * flags (Qcow2OverlapCheckFlags): set of flags for separate specification of each metadata structure type
   129  	// * mode (Qcow2OverlapCheckMode): named mode which chooses a specific set of flags
   130  	Value Any
   131  }
   132  
   133  func (s Qcow2OverlapChecks) MarshalJSON() ([]byte, error) {
   134  	typestr := fmt.Sprintf("%T", s.Value)
   135  	typestr = typestr[strings.LastIndex(typestr, ".")+1:]
   136  
   137  	// Runtime check for supported types
   138  	if typestr != "<nil>" &&
   139  		typestr != "Qcow2OverlapCheckFlags" &&
   140  		typestr != "Qcow2OverlapCheckMode" {
   141  		return nil, errors.New(fmt.Sprintf("Type is not supported: %s", typestr))
   142  	}
   143  
   144  	b, err := json.Marshal(s.Value)
   145  	if err != nil {
   146  		return nil, err
   147  	}
   148  
   149  	return b, nil
   150  }
   151  
   152  func (s *Qcow2OverlapChecks) UnmarshalJSON(data []byte) error {
   153  	// Check for Qcow2OverlapCheckFlags
   154  	{
   155  		var value Qcow2OverlapCheckFlags
   156  		if err := StrictDecode(&value, data); err == nil {
   157  			s.Value = value
   158  			return nil
   159  		}
   160  	}
   161  	// Check for Qcow2OverlapCheckMode
   162  	{
   163  		var value Qcow2OverlapCheckMode
   164  		if err := StrictDecode(&value, data); err == nil {
   165  			s.Value = value
   166  			return nil
   167  		}
   168  	}
   169  	// Check type to error out nicely
   170  	{
   171  		var value Any
   172  		if err := json.Unmarshal(data, &value); err != nil {
   173  			return err
   174  		}
   175  		return errors.New(fmt.Sprintf("Unsupported type %T (value: %v)", value, value))
   176  	}
   177  }
   178  
   179  // Reference to a block device.
   180  //
   181  // Since: 2.9
   182  type BlockdevRef struct {
   183  	// Options are:
   184  	// * definition (BlockdevOptions): defines a new block device inline
   185  	// * reference (string): references the ID of an existing block device
   186  	Value Any
   187  }
   188  
   189  func (s BlockdevRef) MarshalJSON() ([]byte, error) {
   190  	typestr := fmt.Sprintf("%T", s.Value)
   191  	typestr = typestr[strings.LastIndex(typestr, ".")+1:]
   192  
   193  	// Runtime check for supported types
   194  	if typestr != "<nil>" &&
   195  		typestr != "BlockdevOptions" &&
   196  		typestr != "string" {
   197  		return nil, errors.New(fmt.Sprintf("Type is not supported: %s", typestr))
   198  	}
   199  
   200  	b, err := json.Marshal(s.Value)
   201  	if err != nil {
   202  		return nil, err
   203  	}
   204  
   205  	return b, nil
   206  }
   207  
   208  func (s *BlockdevRef) UnmarshalJSON(data []byte) error {
   209  	// Check for BlockdevOptions
   210  	{
   211  		var value BlockdevOptions
   212  		if err := StrictDecode(&value, data); err == nil {
   213  			s.Value = value
   214  			return nil
   215  		}
   216  	}
   217  	// Check for str
   218  	{
   219  		var value string
   220  		if err := StrictDecode(&value, data); err == nil {
   221  			s.Value = value
   222  			return nil
   223  		}
   224  	}
   225  	// Check type to error out nicely
   226  	{
   227  		var value Any
   228  		if err := json.Unmarshal(data, &value); err != nil {
   229  			return err
   230  		}
   231  		return errors.New(fmt.Sprintf("Unsupported type %T (value: %v)", value, value))
   232  	}
   233  }
   234  
   235  // Reference to a block device.
   236  //
   237  // Since: 2.9
   238  type BlockdevRefOrNull struct {
   239  	// Options are:
   240  	// * definition (BlockdevOptions): defines a new block device inline
   241  	// * reference (string): references the ID of an existing block device. An empty string means that no block device should be referenced. Deprecated; use null instead.
   242  	// * null (nil): No block device should be referenced (since 2.10)
   243  	Value Any
   244  }
   245  
   246  func (s BlockdevRefOrNull) MarshalJSON() ([]byte, error) {
   247  	typestr := fmt.Sprintf("%T", s.Value)
   248  	typestr = typestr[strings.LastIndex(typestr, ".")+1:]
   249  
   250  	// Runtime check for supported types
   251  	if typestr != "<nil>" &&
   252  		typestr != "BlockdevOptions" &&
   253  		typestr != "string" &&
   254  		typestr != "*string" {
   255  		return nil, errors.New(fmt.Sprintf("Type is not supported: %s", typestr))
   256  	}
   257  
   258  	b, err := json.Marshal(s.Value)
   259  	if err != nil {
   260  		return nil, err
   261  	}
   262  
   263  	return b, nil
   264  }
   265  
   266  func (s *BlockdevRefOrNull) UnmarshalJSON(data []byte) error {
   267  	// Check for null
   268  	{
   269  		var value *string
   270  		if err := StrictDecode(&value, data); err == nil && value == nil {
   271  			s.Value = nil
   272  			return nil
   273  		}
   274  	}
   275  	// Check for BlockdevOptions
   276  	{
   277  		var value BlockdevOptions
   278  		if err := StrictDecode(&value, data); err == nil {
   279  			s.Value = value
   280  			return nil
   281  		}
   282  	}
   283  	// Check for str
   284  	{
   285  		var value string
   286  		if err := StrictDecode(&value, data); err == nil {
   287  			s.Value = value
   288  			return nil
   289  		}
   290  	}
   291  	// Check type to error out nicely
   292  	{
   293  		var value Any
   294  		if err := json.Unmarshal(data, &value); err != nil {
   295  			return err
   296  		}
   297  		return errors.New(fmt.Sprintf("Unsupported type %T (value: %v)", value, value))
   298  	}
   299  }
   300  

View as plain text