github.com/CycloneDX/sbom-utility@v0.16.0/schema/cyclonedx_unmarshal.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 /* 3 * Licensed to the Apache Software Foundation (ASF) under one or more 4 * contributor license agreements. See the NOTICE file distributed with 5 * this work for additional information regarding copyright ownership. 6 * The ASF licenses this file to You under the Apache License, Version 2.0 7 * (the "License"); you may not use this file except in compliance with 8 * the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package schema 20 21 import ( 22 "encoding/json" 23 "reflect" 24 ) 25 26 // -------------------------------------------------------------------------------- 27 // Custom unmarshallers 28 // -------------------------------------------------------------------------------- 29 30 // -------------------------------------- 31 // UnMarshal from JSON 32 // -------------------------------------- 33 34 func UnMarshalDocument(data interface{}) (*CDXBom, error) { 35 getLogger().Enter() 36 defer getLogger().Exit() 37 38 // we need to marshal the data to normalize it to a []byte 39 jsonString, errMarshal := json.Marshal(data) 40 41 if errMarshal != nil { 42 return nil, errMarshal 43 } 44 45 // optimistically, prepare the receiving structure and unmarshal 46 var bom CDXBom 47 errUnmarshal := json.Unmarshal(jsonString, &bom) 48 49 if errUnmarshal != nil { 50 getLogger().Warningf("unmarshal failed: %v", errUnmarshal) 51 } 52 53 //getLogger().Tracef("\n%s", getLogger().FormatStruct(lc)) 54 return &bom, errUnmarshal 55 } 56 57 func UnMarshalMetadata(data interface{}) (CDXMetadata, error) { 58 getLogger().Enter() 59 defer getLogger().Exit() 60 61 // we need to marshal the data to normalize it to a []byte 62 jsonString, errMarshal := json.Marshal(data) 63 64 if errMarshal != nil { 65 return CDXMetadata{}, errMarshal 66 } 67 68 // optimistically, prepare the receiving structure and unmarshal 69 metadata := CDXMetadata{} 70 errUnmarshal := json.Unmarshal(jsonString, &metadata) 71 72 if errUnmarshal != nil { 73 getLogger().Warningf("unmarshal failed: %v", errUnmarshal) 74 } 75 76 //getLogger().Tracef("\n%s", getLogger().FormatStruct(lc)) 77 return metadata, errUnmarshal 78 } 79 80 func UnMarshalLicenseChoice(data interface{}) (CDXLicenseChoice, error) { 81 getLogger().Enter() 82 defer getLogger().Exit() 83 84 // we need to marshal the data to normalize it to a []byte 85 jsonString, errMarshal := json.Marshal(data) 86 87 if errMarshal != nil { 88 return CDXLicenseChoice{}, errMarshal 89 } 90 91 // optimistically, prepare the receiving structure and unmarshal 92 lc := CDXLicenseChoice{} 93 errUnmarshal := json.Unmarshal(jsonString, &lc) 94 95 if errUnmarshal != nil { 96 getLogger().Warningf("unmarshal failed: %v", errUnmarshal) 97 } 98 99 //getLogger().Tracef("\n%s", getLogger().FormatStruct(lc)) 100 return lc, errUnmarshal 101 } 102 103 func UnMarshalComponent(data interface{}) (CDXComponent, error) { 104 getLogger().Enter() 105 defer getLogger().Exit() 106 107 // we need to marshal the data to normalize it to a []byte 108 jsonString, errMarshal := json.Marshal(data) 109 110 if errMarshal != nil { 111 return CDXComponent{}, errMarshal 112 } 113 114 // optimistically, prepare the receiving structure and unmarshal 115 component := CDXComponent{} 116 errUnmarshal := json.Unmarshal(jsonString, &component) 117 118 if errUnmarshal != nil { 119 getLogger().Warningf("unmarshal failed: %v", errUnmarshal) 120 } 121 122 //getLogger().Tracef("\n%s", getLogger().FormatStruct(component)) 123 return component, errUnmarshal 124 } 125 126 func UnMarshalComponents(data interface{}) ([]CDXComponent, error) { 127 getLogger().Enter() 128 defer getLogger().Exit() 129 130 var components []CDXComponent 131 132 // we need to marshal the data to normalize it to a []byte 133 jsonString, errMarshal := json.Marshal(data) 134 if errMarshal != nil { 135 return components, errMarshal 136 } 137 138 // unmarshal into custom structure 139 errUnmarshal := json.Unmarshal(jsonString, &components) 140 if errUnmarshal != nil { 141 getLogger().Warningf("unmarshal failed: %v", errUnmarshal) 142 } 143 144 return components, errUnmarshal 145 } 146 147 func UnMarshalProperties(data interface{}) (properties []CDXProperty, err error) { 148 getLogger().Enter() 149 defer getLogger().Exit() 150 151 // we need to marshal the data to normalize it to a []byte 152 jsonString, err := json.Marshal(data) 153 if err != nil { 154 return 155 } 156 157 // unmarshal into custom structure 158 err = json.Unmarshal(jsonString, &properties) 159 if err != nil { 160 getLogger().Warningf("unmarshal failed: %v", err) 161 } 162 163 return 164 } 165 166 func UnMarshalProperty(data interface{}) (property CDXProperty, err error) { 167 getLogger().Enter() 168 defer getLogger().Exit() 169 170 // we need to marshal the data to normalize it to a []byte 171 jsonString, err := json.Marshal(data) 172 if err != nil { 173 return 174 } 175 176 // unmarshal into custom structure 177 err = json.Unmarshal(jsonString, &property) 178 if err != nil { 179 getLogger().Warningf("unmarshal failed: %v", err) 180 } 181 182 return 183 } 184 185 // -------------------------------------- 186 // Utils 187 // -------------------------------------- 188 189 func (property *CDXProperty) Equals(testProperty CDXProperty) bool { 190 return reflect.DeepEqual(*property, testProperty) 191 }