go-micro.dev/v5@v5.12.0/store/postgres/metadata.go (about) 1 // Copyright 2020 Asim Aslam 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // Original source: github.com/micro/go-plugins/v3/store/cockroach/metadata.go 16 17 package postgres 18 19 import ( 20 "database/sql/driver" 21 "encoding/json" 22 "errors" 23 ) 24 25 // https://github.com/upper/db/blob/master/postgresql/custom_types.go#L43 26 type Metadata map[string]interface{} 27 28 // Scan satisfies the sql.Scanner interface. 29 func (m *Metadata) Scan(src interface{}) error { 30 source, ok := src.([]byte) 31 if !ok { 32 return errors.New("Type assertion .([]byte) failed.") 33 } 34 35 var i interface{} 36 err := json.Unmarshal(source, &i) 37 if err != nil { 38 return err 39 } 40 41 *m, ok = i.(map[string]interface{}) 42 if !ok { 43 return errors.New("Type assertion .(map[string]interface{}) failed.") 44 } 45 46 return nil 47 } 48 49 // Value satisfies the driver.Valuer interface. 50 func (m Metadata) Value() (driver.Value, error) { 51 j, err := json.Marshal(m) 52 return j, err 53 } 54 55 func toMetadata(m *Metadata) map[string]interface{} { 56 md := make(map[string]interface{}) 57 for k, v := range *m { 58 md[k] = v 59 } 60 return md 61 }