Skip to content

Commit

Permalink
Add: custom outgoing metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
youlu-cn committed Jul 18, 2023
1 parent c311ba0 commit 5e1d968
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions metadata/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,78 @@ func (s *ctxWrapper) Context() context.Context {
return context.WithValue(ctx, metadataKey{}, md)
}

func AppendOutgoingMetadata(ctx context.Context, md *common.Metadata) context.Context {
if md == nil {
return context.WithValue(ctx, metadataKey{}, &common.Metadata{})
}
oldMD := IncomingMetadata(ctx)
if oldMD == nil {
return context.WithValue(ctx, metadataKey{}, md)
}
//
if md.GetPlatform() > 0 {
oldMD.Platform = md.Platform
}
if md.GetNetwork() > 0 {
oldMD.Network = md.Network
}
if md.GetPackage() != "" {
oldMD.Package = md.Package
}
if md.GetVersion() != "" {
oldMD.Version = md.Version
}
if md.GetOsVersion() != "" {
oldMD.OsVersion = md.OsVersion
}
if md.GetBrand() != "" {
oldMD.Brand = md.Brand
}
if md.GetModel() != "" {
oldMD.Model = md.Model
}
if md.GetDeviceId() != "" {
oldMD.DeviceId = md.DeviceId
}
if md.GetIsEmulator() {
oldMD.IsEmulator = md.IsEmulator
}
if md.GetIsDebug() {
oldMD.IsDebug = md.IsDebug
}
if md.GetClientIp() != "" {
oldMD.ClientIp = md.ClientIp
}
if md.GetChannel() != "" {
oldMD.Channel = md.Channel
}
if md.GetProduct() != "" {
oldMD.Product = md.Product
}
if md.GetTraceId() != "" {
oldMD.TraceId = md.TraceId
}
if md.GetRiskId() != "" {
oldMD.RiskId = md.RiskId
}
if md.GetUuid() != "" {
oldMD.Uuid = md.Uuid
}
if md.GetUdid() != "" {
oldMD.Udid = md.Udid
}
if md.GetUserAgent() != "" {
oldMD.UserAgent = md.UserAgent
}
if md.GetDeviceMac() != "" {
oldMD.DeviceMac = md.DeviceMac
}
if md.GetAndroidId() != "" {
oldMD.AndroidId = md.AndroidId
}
return context.WithValue(ctx, metadataKey{}, oldMD)
}

func ContextWithProduct(ctx context.Context, product string) context.Context {
return context.WithValue(ctx, metadataKey{}, &common.Metadata{
Product: product,
Expand Down

0 comments on commit 5e1d968

Please sign in to comment.