| package main |
| |
| import ( |
| pb "feed/app/generated/ext/maps/booking/offers/proto" |
| "fmt" |
| "log" |
| |
| money "google.golang.org/genproto/googleapis/type/money" |
| timestamppb "google.golang.org/protobuf/types/known/timestamppb" |
| dayofweek "google.golang.org/genproto/googleapis/type/dayofweek" |
| timeofday "google.golang.org/genproto/googleapis/type/timeofday" |
| proto "google.golang.org/protobuf/proto" |
| |
| "google.golang.org/protobuf/encoding/protojson" |
| ) |
| |
| func main() { |
| // 200 MB feed file size limit |
| const MaxBytesDataFile = 200 * 1024 * 1024 |
| |
| // Create OfferFeed with offers |
| feed := &pb.OfferFeed{} |
| |
| // Build the offers |
| offer := &pb.Offer{ |
| OfferId: "offer-1", |
| EntityIds: []string{"dining-1"}, |
| OfferSource: pb.OfferSource_OFFER_SOURCE_AGGREGATOR, |
| ActionType: pb.ActionType_ACTION_TYPE_DINING, |
| OfferModes: []pb.OfferMode{ |
| pb.OfferMode_OFFER_MODE_WALK_IN, |
| pb.OfferMode_OFFER_MODE_FREE_RESERVATION, |
| }, |
| OfferCategory: pb.OfferCategory_OFFER_CATEGORY_BASE_OFFER, |
| OfferDetails: &pb.OfferDetails{ |
| OfferDisplayText: "₹100 off on your order", |
| OfferSpecification: &pb.OfferDetails_DiscountValue{ |
| DiscountValue: &money.Money{ |
| CurrencyCode: "INR", |
| Units: 100, |
| }, |
| }, |
| }, |
| OfferRestrictions: &pb.OfferRestrictions{ |
| CombinableWithOtherOffers: true, |
| CombinableOfferCategories: []pb.OfferCategory{ |
| pb.OfferCategory_OFFER_CATEGORY_ADD_ON_PAYMENT_OFFER, |
| pb.OfferCategory_OFFER_CATEGORY_ADD_ON_COUPON_OFFER, |
| }, |
| }, |
| Terms: &pb.Terms{ |
| RestrictedToCertainUsers: false, |
| TermsAndConditions: "Valid on all menu items.", |
| }, |
| ValidityPeriods: []*pb.ValidityPeriod{ |
| { |
| ValidPeriod: &pb.ValidityRange{ |
| ValidFromTime: ×tamppb.Timestamp{Seconds: 1687062000}, |
| ValidThroughTime: ×tamppb.Timestamp{Seconds: 1956556800}, |
| }, |
| TimeOfDay: []*pb.TimeOfDayWindow{ |
| // Monday - Thursday Window |
| { |
| TimeWindows: &pb.TimeOfDayRange{ |
| OpenTime: &timeofday.TimeOfDay{Hours: 13}, |
| CloseTime: &timeofday.TimeOfDay{Hours: 23}, |
| }, |
| DayOfWeek: []dayofweek.DayOfWeek{ |
| dayofweek.DayOfWeek_MONDAY, |
| dayofweek.DayOfWeek_TUESDAY, |
| dayofweek.DayOfWeek_WEDNESDAY, |
| dayofweek.DayOfWeek_THURSDAY, |
| }, |
| }, |
| // Friday - Sunday Window |
| { |
| TimeWindows: &pb.TimeOfDayRange{ |
| OpenTime: &timeofday.TimeOfDay{Hours: 13}, |
| CloseTime: &timeofday.TimeOfDay{ |
| Hours: 23, |
| Minutes: 59, |
| Seconds: 59, |
| }, |
| }, |
| DayOfWeek: []dayofweek.DayOfWeek{ |
| dayofweek.DayOfWeek_FRIDAY, |
| dayofweek.DayOfWeek_SATURDAY, |
| dayofweek.DayOfWeek_SUNDAY, |
| }, |
| }, |
| }, |
| }, |
| }, |
| OfferUrl: "https://www.example-restaurant.com/offer/base_offer_1", |
| ImageUrl: "https://www.example-restaurant.com/images/offer_base.jpg", |
| } |
| |
| // Example testing for feed size |
| // Protocol buffer message must be less than 2 GiB |
| // https://protobuf.dev/programming-guides/proto-limits/ |
| // It is recommended to not exceed 200 MB, as there is an Actions |
| // Center limit of 200 MB per file after compression. |
| offerSize := proto.Size(offer) |
| currentFeedSize := proto.Size(feed) |
| |
| if currentFeedSize + offerSize < MaxBytesDataFile { |
| feed.Data = append(feed.Data, offer) |
| } else { |
| // 1. Serialize and save the current 'feed' to a file |
| // 2. Reset 'feed' to a new empty OfferFeed |
| // 3. Add the 'offer' to the new feed |
| } |
| |
| // Serialize to JSON |
| marshaler := protojson.MarshalOptions{ |
| UseProtoNames: true, |
| } |
| |
| jsonOutput, err := marshaler.Marshal(feed) |
| if err != nil { |
| log.Fatalf("Failed to marshal feed: %v", err) |
| } |
| |
| fmt.Println(string(jsonOutput)) |
| } |