File size: 446 Bytes
fea99b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package commonhttp

import (
	"encoding/json"
)

type Union[Primary any, Secondary any] struct {
	Option1 *Primary
	Option2 *Secondary
}

// Implements json.Marshaler with primary having precedence.
func (u Union[Primary, Secondary]) MarshalJSON() ([]byte, error) {
	if u.Option1 != nil {
		return json.Marshal(u.Option1)
	}
	if u.Option2 != nil {
		return json.Marshal(u.Option2)
	}
	// if nothing is set we return empty
	return []byte{}, nil
}