| 1 |
// Copyright (c) 2014-2019 Ludovic Fauvet |
| 2 |
// Licensed under the MIT license |
| 3 |
|
| 4 |
package rpc |
| 5 |
|
| 6 |
import ( |
| 7 |
"github.com/etix/mirrorbits/mirrors" |
| 8 |
"github.com/golang/protobuf/ptypes" |
| 9 |
) |
| 10 |
|
| 11 |
func MirrorToRPC(m *mirrors.Mirror) (*Mirror, error) { |
| 12 |
stateSince, err := ptypes.TimestampProto(m.StateSince.Time) |
| 13 |
if err != nil { |
| 14 |
return nil, err |
| 15 |
} |
| 16 |
lastSync, err := ptypes.TimestampProto(m.LastSync.Time) |
| 17 |
if err != nil { |
| 18 |
return nil, err |
| 19 |
} |
| 20 |
lastSuccessfulSync, err := ptypes.TimestampProto(m.LastSuccessfulSync.Time) |
| 21 |
if err != nil { |
| 22 |
return nil, err |
| 23 |
} |
| 24 |
lastModTime, err := ptypes.TimestampProto(m.LastModTime.Time) |
| 25 |
if err != nil { |
| 26 |
return nil, err |
| 27 |
} |
| 28 |
return &Mirror{ |
| 29 |
ID: int32(m.ID), |
| 30 |
Name: m.Name, |
| 31 |
HttpURL: m.HttpURL, |
| 32 |
RsyncURL: m.RsyncURL, |
| 33 |
FtpURL: m.FtpURL, |
| 34 |
SponsorName: m.SponsorName, |
| 35 |
SponsorURL: m.SponsorURL, |
| 36 |
SponsorLogoURL: m.SponsorLogoURL, |
| 37 |
AdminName: m.AdminName, |
| 38 |
AdminEmail: m.AdminEmail, |
| 39 |
CustomData: m.CustomData, |
| 40 |
ContinentOnly: m.ContinentOnly, |
| 41 |
CountryOnly: m.CountryOnly, |
| 42 |
ASOnly: m.ASOnly, |
| 43 |
Score: int32(m.Score), |
| 44 |
Latitude: m.Latitude, |
| 45 |
Longitude: m.Longitude, |
| 46 |
ContinentCode: m.ContinentCode, |
| 47 |
CountryCodes: m.CountryCodes, |
| 48 |
ExcludedCountryCodes: m.ExcludedCountryCodes, |
| 49 |
Asnum: uint32(m.Asnum), |
| 50 |
Comment: m.Comment, |
| 51 |
Enabled: m.Enabled, |
| 52 |
HttpUp: m.HttpUp, |
| 53 |
HttpsUp: m.HttpsUp, |
| 54 |
HttpDownReason: m.HttpDownReason, |
| 55 |
HttpsDownReason: m.HttpsDownReason, |
| 56 |
StateSince: stateSince, |
| 57 |
AllowRedirects: int32(m.AllowRedirects), |
| 58 |
LastSync: lastSync, |
| 59 |
LastSuccessfulSync: lastSuccessfulSync, |
| 60 |
LastModTime: lastModTime, |
| 61 |
}, nil |
| 62 |
} |
| 63 |
|
| 64 |
func MirrorFromRPC(m *Mirror) (*mirrors.Mirror, error) { |
| 65 |
stateSince, err := ptypes.Timestamp(m.StateSince) |
| 66 |
if err != nil { |
| 67 |
return nil, err |
| 68 |
} |
| 69 |
lastSync, err := ptypes.Timestamp(m.LastSync) |
| 70 |
if err != nil { |
| 71 |
return nil, err |
| 72 |
} |
| 73 |
lastSuccessfulSync, err := ptypes.Timestamp(m.LastSuccessfulSync) |
| 74 |
if err != nil { |
| 75 |
return nil, err |
| 76 |
} |
| 77 |
lastModTime, err := ptypes.Timestamp(m.LastModTime) |
| 78 |
if err != nil { |
| 79 |
return nil, err |
| 80 |
} |
| 81 |
return &mirrors.Mirror{ |
| 82 |
ID: int(m.ID), |
| 83 |
Name: m.Name, |
| 84 |
HttpURL: m.HttpURL, |
| 85 |
RsyncURL: m.RsyncURL, |
| 86 |
FtpURL: m.FtpURL, |
| 87 |
SponsorName: m.SponsorName, |
| 88 |
SponsorURL: m.SponsorURL, |
| 89 |
SponsorLogoURL: m.SponsorLogoURL, |
| 90 |
AdminName: m.AdminName, |
| 91 |
AdminEmail: m.AdminEmail, |
| 92 |
CustomData: m.CustomData, |
| 93 |
ContinentOnly: m.ContinentOnly, |
| 94 |
CountryOnly: m.CountryOnly, |
| 95 |
ASOnly: m.ASOnly, |
| 96 |
Score: int(m.Score), |
| 97 |
Latitude: m.Latitude, |
| 98 |
Longitude: m.Longitude, |
| 99 |
ContinentCode: m.ContinentCode, |
| 100 |
CountryCodes: m.CountryCodes, |
| 101 |
ExcludedCountryCodes: m.ExcludedCountryCodes, |
| 102 |
Asnum: uint(m.Asnum), |
| 103 |
Comment: m.Comment, |
| 104 |
Enabled: m.Enabled, |
| 105 |
HttpUp: m.HttpUp, |
| 106 |
HttpsUp: m.HttpsUp, |
| 107 |
HttpDownReason: m.HttpDownReason, |
| 108 |
HttpsDownReason: m.HttpsDownReason, |
| 109 |
StateSince: mirrors.Time{}.FromTime(stateSince), |
| 110 |
AllowRedirects: mirrors.Redirects(m.AllowRedirects), |
| 111 |
LastSync: mirrors.Time{}.FromTime(lastSync), |
| 112 |
LastSuccessfulSync: mirrors.Time{}.FromTime(lastSuccessfulSync), |
| 113 |
LastModTime: mirrors.Time{}.FromTime(lastModTime), |
| 114 |
}, nil |
| 115 |
} |