github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/dms/v1/topics/results.go (about) 1 package topics 2 3 import ( 4 "encoding/json" 5 "strconv" 6 7 "github.com/chnsz/golangsdk" 8 ) 9 10 // CreateResponse is a struct that contains the create response 11 type CreateResponse struct { 12 Name string `json:"id"` 13 } 14 15 // Topic includes the parameters of an topic 16 type Topic struct { 17 Name string `json:"id"` 18 Partition int `json:"partition"` 19 Replication int `json:"replication"` 20 RetentionTime int `json:"retention_time"` 21 TopicType int `json:"topic_type"` 22 PoliciesOnly bool `json:"policiesOnly"` 23 SyncReplication bool `json:"-"` 24 SyncMessageFlush bool `json:"-"` 25 } 26 27 // UnmarshalJSON to override default 28 func (r *Topic) UnmarshalJSON(b []byte) error { 29 type tmp Topic 30 var s struct { 31 tmp 32 SyncReplication interface{} `json:"sync_replication"` 33 SyncMessageFlush interface{} `json:"sync_message_flush"` 34 } 35 err := json.Unmarshal(b, &s) 36 if err != nil { 37 return err 38 } 39 40 *r = Topic(s.tmp) 41 42 switch t := s.SyncReplication.(type) { 43 case string: 44 enabled, _ := strconv.ParseBool(s.SyncReplication.(string)) 45 r.SyncReplication = enabled 46 case bool: 47 r.SyncReplication = t 48 } 49 50 switch t := s.SyncMessageFlush.(type) { 51 case string: 52 enabled, _ := strconv.ParseBool(s.SyncMessageFlush.(string)) 53 r.SyncMessageFlush = enabled 54 case bool: 55 r.SyncMessageFlush = t 56 } 57 58 return err 59 } 60 61 // ListResponse is a struct that contains the list response 62 type ListResponse struct { 63 Total int `json:"total"` 64 Size int `json:"size"` 65 RemainPartitions int `json:"remain_partitions"` 66 MaxPartitions int `json:"max_partitions"` 67 Topics []Topic `json:"topics"` 68 } 69 70 // CreateResult is a struct that contains all the return parameters of creation 71 type CreateResult struct { 72 golangsdk.Result 73 } 74 75 // Extract from CreateResult 76 func (r CreateResult) Extract() (*CreateResponse, error) { 77 var s CreateResponse 78 err := r.Result.ExtractInto(&s) 79 return &s, err 80 } 81 82 // ListResult contains the body of getting detailed 83 type ListResult struct { 84 golangsdk.Result 85 } 86 87 // Extract from ListResult 88 func (r ListResult) Extract() ([]Topic, error) { 89 var s ListResponse 90 err := r.Result.ExtractInto(&s) 91 return s.Topics, err 92 } 93 94 // DeleteResult is a struct which contains the result of deletion 95 type DeleteResult struct { 96 golangsdk.Result 97 } 98 99 // DeleteResponse is a struct that contains the deletion response 100 type DeleteResponse struct { 101 Name string `json:"id"` 102 Success bool `json:"success"` 103 } 104 105 // Extract from DeleteResult 106 func (r DeleteResult) Extract() ([]DeleteResponse, error) { 107 var s struct { 108 Topics []DeleteResponse `json:"topics"` 109 } 110 err := r.Result.ExtractInto(&s) 111 return s.Topics, err 112 }