github.com/dolthub/go-mysql-server@v0.18.0/sql/mysql_db/privilege_set_json.go (about) 1 // Copyright 2022 Dolthub, Inc. 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 // http://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 package mysql_db 16 17 import ( 18 "encoding/json" 19 "fmt" 20 21 "github.com/dolthub/go-mysql-server/sql" 22 ) 23 24 // privilegeSetMarshaler handles marshaling duties to and from JSON for a PrivilegeSet. 25 type privilegeSetMarshaler struct { 26 GlobalStatic []string 27 Databases []privilegeSetMarshalerDatabase 28 } 29 30 // privilegeSetMarshalerDatabase handles marshaling duties to and from JSON for a database in a PrivilegeSet. 31 type privilegeSetMarshalerDatabase struct { 32 Name string 33 Privileges []string 34 Tables []privilegeSetMarshalerTable 35 } 36 37 // privilegeSetMarshalerTable handles marshaling duties to and from JSON for a table in a PrivilegeSet. 38 type privilegeSetMarshalerTable struct { 39 Name string 40 Privileges []string 41 Columns []privilegeSetMarshalerColumn 42 } 43 44 // privilegeSetMarshalerColumn handles marshaling duties to and from JSON for a column in a PrivilegeSet. 45 type privilegeSetMarshalerColumn struct { 46 Name string 47 Privileges []string 48 } 49 50 var _ json.Marshaler = PrivilegeSet{} 51 var _ json.Unmarshaler = (*PrivilegeSet)(nil) 52 53 // MarshalJSON implements the interface json.Marshaler. This is deprecated functionality, as serialization has been 54 // replaced by flatbuffers. 55 func (ps PrivilegeSet) MarshalJSON() ([]byte, error) { 56 globalStaticPrivs := ps.ToSlice() 57 psm := privilegeSetMarshaler{ 58 GlobalStatic: make([]string, len(globalStaticPrivs)), 59 Databases: make([]privilegeSetMarshalerDatabase, len(ps.databases)), 60 } 61 for i, globalStaticPriv := range globalStaticPrivs { 62 psm.GlobalStatic[i] = globalStaticPriv.String() 63 } 64 for dbIndex, database := range ps.GetDatabases() { 65 dbPrivs := database.ToSlice() 66 dbm := privilegeSetMarshalerDatabase{ 67 Name: database.Name(), 68 Privileges: make([]string, len(dbPrivs)), 69 Tables: make([]privilegeSetMarshalerTable, len(database.(PrivilegeSetDatabase).tables)), 70 } 71 for i, dbPriv := range dbPrivs { 72 dbm.Privileges[i] = dbPriv.String() 73 } 74 psm.Databases[dbIndex] = dbm 75 76 for tblIndex, table := range database.GetTables() { 77 tblPrivs := table.ToSlice() 78 tbm := privilegeSetMarshalerTable{ 79 Name: table.Name(), 80 Privileges: make([]string, len(table.(PrivilegeSetTable).privs)), 81 Columns: make([]privilegeSetMarshalerColumn, len(table.(PrivilegeSetTable).columns)), 82 } 83 for i, tblPriv := range tblPrivs { 84 tbm.Privileges[i] = tblPriv.String() 85 } 86 dbm.Tables[tblIndex] = tbm 87 88 for colIndex, column := range table.GetColumns() { 89 colPrivs := column.ToSlice() 90 cbm := privilegeSetMarshalerColumn{ 91 Name: column.Name(), 92 Privileges: make([]string, len(column.(PrivilegeSetColumn).privs)), 93 } 94 for i, colPriv := range colPrivs { 95 cbm.Privileges[i] = colPriv.String() 96 } 97 tbm.Columns[colIndex] = cbm 98 } 99 } 100 } 101 102 return json.Marshal(psm) 103 } 104 105 // UnmarshalJSON implements the interface json.Unmarshaler. This is deprecated functionality, as serialization has been 106 // replaced by flatbuffers. 107 func (ps *PrivilegeSet) UnmarshalJSON(jsonData []byte) error { 108 ps.globalStatic = make(map[sql.PrivilegeType]struct{}) 109 ps.globalDynamic = make(map[string]bool) 110 ps.databases = make(map[string]PrivilegeSetDatabase) 111 psm := privilegeSetMarshaler{} 112 err := json.Unmarshal(jsonData, &psm) 113 if err != nil { 114 return err 115 } 116 for _, privStr := range psm.GlobalStatic { 117 priv, ok := sql.PrivilegeTypeFromString(privStr) 118 if !ok { 119 return fmt.Errorf(`unknown privilege type: "%s"`, priv) 120 } 121 ps.AddGlobalStatic(priv) 122 } 123 for _, database := range psm.Databases { 124 for _, privStr := range database.Privileges { 125 priv, ok := sql.PrivilegeTypeFromString(privStr) 126 if !ok { 127 return fmt.Errorf(`unknown privilege type: "%s"`, priv) 128 } 129 ps.AddDatabase(database.Name, priv) 130 } 131 132 for _, table := range database.Tables { 133 for _, privStr := range table.Privileges { 134 priv, ok := sql.PrivilegeTypeFromString(privStr) 135 if !ok { 136 return fmt.Errorf(`unknown privilege type: "%s"`, priv) 137 } 138 ps.AddTable(database.Name, table.Name, priv) 139 } 140 141 for _, column := range table.Columns { 142 for _, privStr := range column.Privileges { 143 priv, ok := sql.PrivilegeTypeFromString(privStr) 144 if !ok { 145 return fmt.Errorf(`unknown privilege type: "%s"`, priv) 146 } 147 ps.AddColumn(database.Name, table.Name, column.Name, priv) 148 } 149 } 150 } 151 } 152 return nil 153 }