github.com/google/osv-scalibr@v0.4.1/binary/proto/result.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  	"github.com/google/osv-scalibr/result"
    20  
    21  	spb "github.com/google/osv-scalibr/binary/proto/scan_result_go_proto"
    22  	"google.golang.org/protobuf/types/known/timestamppb"
    23  )
    24  
    25  // --- Struct to Proto
    26  
    27  // ScanResultToProto converts a ScanResult go struct into the equivalent proto.
    28  func ScanResultToProto(r *result.ScanResult) (*spb.ScanResult, error) {
    29  	pluginStatus := make([]*spb.PluginStatus, 0, len(r.PluginStatus))
    30  	for _, s := range r.PluginStatus {
    31  		pluginStatus = append(pluginStatus, PluginStatusToProto(s))
    32  	}
    33  
    34  	inventory, err := InventoryToProto(&r.Inventory)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	return &spb.ScanResult{
    40  		Version:      r.Version,
    41  		StartTime:    timestamppb.New(r.StartTime),
    42  		EndTime:      timestamppb.New(r.EndTime),
    43  		Status:       scanStatusToProto(r.Status),
    44  		PluginStatus: pluginStatus,
    45  		Inventory:    inventory,
    46  	}, nil
    47  }
    48  
    49  // --- Proto to Struct
    50  
    51  // ScanResultFromProto converts a ScanResult proto into the equivalent go struct.
    52  func ScanResultFromProto(r *spb.ScanResult) (*result.ScanResult, error) {
    53  	pluginStatus := make([]*plugin.Status, 0, len(r.PluginStatus))
    54  	for _, s := range r.PluginStatus {
    55  		pluginStatus = append(pluginStatus, PluginStatusToStruct(s))
    56  	}
    57  
    58  	inventory := InventoryToStruct(r.Inventory)
    59  
    60  	return &result.ScanResult{
    61  		Version:      r.Version,
    62  		StartTime:    r.StartTime.AsTime(),
    63  		EndTime:      r.EndTime.AsTime(),
    64  		Status:       scanStatusToStruct(r.Status),
    65  		PluginStatus: pluginStatus,
    66  		Inventory:    *inventory,
    67  	}, nil
    68  }