github.com/google/osv-scalibr@v0.4.1/binary/proto/plugin.go (about) 1 // Copyright 2025 Google LLC 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 proto 16 17 import ( 18 "github.com/google/osv-scalibr/plugin" 19 20 spb "github.com/google/osv-scalibr/binary/proto/scan_result_go_proto" 21 ) 22 23 var ( 24 // structToProtoScanStatus is a map of struct ScanStatus to their corresponding proto values. 25 structToProtoScanStatus = map[plugin.ScanStatusEnum]spb.ScanStatus_ScanStatusEnum{ 26 plugin.ScanStatusSucceeded: spb.ScanStatus_SUCCEEDED, 27 plugin.ScanStatusPartiallySucceeded: spb.ScanStatus_PARTIALLY_SUCCEEDED, 28 plugin.ScanStatusFailed: spb.ScanStatus_FAILED, 29 plugin.ScanStatusUnspecified: spb.ScanStatus_UNSPECIFIED, 30 } 31 32 protoToStructScanStatus = func() map[spb.ScanStatus_ScanStatusEnum]plugin.ScanStatusEnum { 33 m := make(map[spb.ScanStatus_ScanStatusEnum]plugin.ScanStatusEnum) 34 for k, v := range structToProtoScanStatus { 35 m[v] = k 36 } 37 if len(m) != len(structToProtoScanStatus) { 38 panic("protoToStructScanStatus does not contain all values from structToProtoScanStatus") 39 } 40 return m 41 }() 42 ) 43 44 // --- Struct to Proto 45 46 // PluginStatusToProto converts a plugin.Status go struct into the equivalent proto. 47 func PluginStatusToProto(s *plugin.Status) *spb.PluginStatus { 48 if s == nil { 49 return nil 50 } 51 52 return &spb.PluginStatus{ 53 Name: s.Name, 54 Version: int32(s.Version), 55 Status: scanStatusToProto(s.Status), 56 } 57 } 58 59 func scanStatusToProto(s *plugin.ScanStatus) *spb.ScanStatus { 60 if s == nil { 61 return nil 62 } 63 statusEnum := structToProtoScanStatus[s.Status] 64 return &spb.ScanStatus{Status: statusEnum, FailureReason: s.FailureReason, FileErrors: fileErrorsToProto(s.FileErrors)} 65 } 66 67 func fileErrorsToProto(s []*plugin.FileError) []*spb.FileError { 68 if s == nil { 69 return nil 70 } 71 var res []*spb.FileError 72 for _, e := range s { 73 res = append(res, &spb.FileError{FilePath: e.FilePath, ErrorMessage: e.ErrorMessage}) 74 } 75 return res 76 } 77 78 // --- Proto to Struct 79 80 // PluginStatusToStruct converts a plugin.Status proto into the equivalent go struct. 81 func PluginStatusToStruct(s *spb.PluginStatus) *plugin.Status { 82 if s == nil { 83 return nil 84 } 85 86 return &plugin.Status{ 87 Name: s.GetName(), 88 Version: int(s.GetVersion()), 89 Status: scanStatusToStruct(s.GetStatus()), 90 } 91 } 92 93 func scanStatusToStruct(s *spb.ScanStatus) *plugin.ScanStatus { 94 if s == nil { 95 return nil 96 } 97 statusEnum := protoToStructScanStatus[s.GetStatus()] 98 return &plugin.ScanStatus{Status: statusEnum, FailureReason: s.GetFailureReason(), FileErrors: fileErrorsToStruct(s.GetFileErrors())} 99 } 100 101 func fileErrorsToStruct(s []*spb.FileError) []*plugin.FileError { 102 if s == nil { 103 return nil 104 } 105 var res []*plugin.FileError 106 for _, e := range s { 107 res = append(res, &plugin.FileError{FilePath: e.GetFilePath(), ErrorMessage: e.GetErrorMessage()}) 108 } 109 return res 110 }