Extract shared types and add Regulation type with parsing methods

Move cookie banner types (CookieItem, Category, Regulation, BannerConfig,
etc.) into a dedicated types.ts file. Add a coredata.Regulation type with
parsing, JSON marshaling, and database scanning methods. Hardcode the
geoloc-import data directory since the submodule path is fixed.

Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
Émile Ré
2026-05-07 09:25:16 +04:00
parent f695b90b19
commit 4598506076
23 changed files with 308 additions and 114 deletions

View File

@@ -16,24 +16,24 @@ package cookiebanner
import "go.probo.inc/probo/pkg/coredata"
type Regulation string
type Regulation = coredata.Regulation
const (
RegulationNone Regulation = ""
RegulationGDPR Regulation = "GDPR"
RegulationUKGDPR Regulation = "UK_GDPR"
RegulationFADP Regulation = "FADP"
RegulationCCPA Regulation = "CCPA"
RegulationPIPEDA Regulation = "PIPEDA"
RegulationLGPD Regulation = "LGPD"
RegulationLFPDPPP Regulation = "LFPDPPP"
RegulationPOPIA Regulation = "POPIA"
RegulationPDPA Regulation = "PDPA"
RegulationPIPL Regulation = "PIPL"
RegulationPIPA Regulation = "PIPA"
RegulationAPPI Regulation = "APPI"
RegulationDPDP Regulation = "DPDP"
RegulationPDPL Regulation = "PDPL"
RegulationNone = coredata.RegulationNone
RegulationGDPR = coredata.RegulationGDPR
RegulationUKGDPR = coredata.RegulationUKGDPR
RegulationFADP = coredata.RegulationFADP
RegulationCCPA = coredata.RegulationCCPA
RegulationPIPEDA = coredata.RegulationPIPEDA
RegulationLGPD = coredata.RegulationLGPD
RegulationLFPDPPP = coredata.RegulationLFPDPPP
RegulationPOPIA = coredata.RegulationPOPIA
RegulationPDPA = coredata.RegulationPDPA
RegulationPIPL = coredata.RegulationPIPL
RegulationPIPA = coredata.RegulationPIPA
RegulationAPPI = coredata.RegulationAPPI
RegulationDPDP = coredata.RegulationDPDP
RegulationPDPL = coredata.RegulationPDPL
)
const (

View File

@@ -1856,7 +1856,7 @@ func (s *Service) RecordConsent(
ConsentData: req.ConsentData,
Action: req.Action,
SdkVersion: req.SdkVersion,
Regulation: string(req.Regulation),
Regulation: req.Regulation,
CreatedAt: time.Now(),
}

View File

@@ -40,7 +40,7 @@ type (
ConsentData json.RawMessage `db:"consent_data"`
Action CookieConsentAction `db:"action"`
SdkVersion string `db:"sdk_version"`
Regulation string `db:"regulation"`
Regulation Regulation `db:"regulation"`
CreatedAt time.Time `db:"created_at"`
}

152
pkg/coredata/regulation.go Normal file
View File

@@ -0,0 +1,152 @@
// Copyright (c) 2026 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
import (
"database/sql/driver"
"encoding/json"
"fmt"
)
type Regulation string
const (
RegulationNone Regulation = ""
RegulationGDPR Regulation = "GDPR"
RegulationUKGDPR Regulation = "UK_GDPR"
RegulationFADP Regulation = "FADP"
RegulationCCPA Regulation = "CCPA"
RegulationPIPEDA Regulation = "PIPEDA"
RegulationLGPD Regulation = "LGPD"
RegulationLFPDPPP Regulation = "LFPDPPP"
RegulationPOPIA Regulation = "POPIA"
RegulationPDPA Regulation = "PDPA"
RegulationPIPL Regulation = "PIPL"
RegulationPIPA Regulation = "PIPA"
RegulationAPPI Regulation = "APPI"
RegulationDPDP Regulation = "DPDP"
RegulationPDPL Regulation = "PDPL"
)
func Regulations() []Regulation {
return []Regulation{
RegulationGDPR,
RegulationUKGDPR,
RegulationFADP,
RegulationCCPA,
RegulationPIPEDA,
RegulationLGPD,
RegulationLFPDPPP,
RegulationPOPIA,
RegulationPDPA,
RegulationPIPL,
RegulationPIPA,
RegulationAPPI,
RegulationDPDP,
RegulationPDPL,
}
}
func ParseRegulation(s string) (Regulation, error) {
switch Regulation(s) {
case RegulationNone:
return RegulationNone, nil
case RegulationGDPR:
return RegulationGDPR, nil
case RegulationUKGDPR:
return RegulationUKGDPR, nil
case RegulationFADP:
return RegulationFADP, nil
case RegulationCCPA:
return RegulationCCPA, nil
case RegulationPIPEDA:
return RegulationPIPEDA, nil
case RegulationLGPD:
return RegulationLGPD, nil
case RegulationLFPDPPP:
return RegulationLFPDPPP, nil
case RegulationPOPIA:
return RegulationPOPIA, nil
case RegulationPDPA:
return RegulationPDPA, nil
case RegulationPIPL:
return RegulationPIPL, nil
case RegulationPIPA:
return RegulationPIPA, nil
case RegulationAPPI:
return RegulationAPPI, nil
case RegulationDPDP:
return RegulationDPDP, nil
case RegulationPDPL:
return RegulationPDPL, nil
default:
return "", fmt.Errorf("invalid Regulation value: %q", s)
}
}
func (r Regulation) String() string {
return string(r)
}
func (r *Regulation) Scan(value any) error {
var v string
switch val := value.(type) {
case string:
v = val
case []byte:
v = string(val)
default:
return fmt.Errorf("unsupported type for Regulation: %T", value)
}
parsed, err := ParseRegulation(v)
if err != nil {
return err
}
*r = parsed
return nil
}
func (r Regulation) Value() (driver.Value, error) {
if r == RegulationNone {
return "", nil
}
if _, err := ParseRegulation(string(r)); err != nil {
return nil, fmt.Errorf("invalid Regulation: %s", r)
}
return string(r), nil
}
func (r Regulation) MarshalJSON() ([]byte, error) {
return json.Marshal(string(r))
}
func (r *Regulation) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return fmt.Errorf("cannot unmarshal Regulation: %w", err)
}
parsed, err := ParseRegulation(s)
if err != nil {
return err
}
*r = parsed
return nil
}

View File

@@ -100,9 +100,9 @@ func (s *Service) LookupCountry(ctx context.Context, ip string) (coredata.Countr
err := s.pgClient.WithConn(
ctx,
func(ctx context.Context, conn pg.Querier) error {
var lookupErr error
cc, lookupErr = coredata.LookupCountryByIP(ctx, conn, ip)
return lookupErr
var err error
cc, err = coredata.LookupCountryByIP(ctx, conn, ip)
return err
},
)
if err != nil {
@@ -118,9 +118,9 @@ func (s *Service) IsPopulated(ctx context.Context) (bool, error) {
err := s.pgClient.WithConn(
ctx,
func(ctx context.Context, conn pg.Querier) error {
var lookupErr error
populated, lookupErr = coredata.IsIPCountryBlocksPopulated(ctx, conn)
return lookupErr
var err error
populated, err = coredata.IsIPCountryBlocksPopulated(ctx, conn)
return err
},
)
if err != nil {

View File

@@ -66,6 +66,7 @@ type CookieConsentRecord implements Node {
consentData: String!
action: CookieConsentAction!
sdkVersion: String!
regulation: String!
createdAt: Datetime!
}

View File

@@ -81,6 +81,7 @@ func NewCookieConsentRecord(r *coredata.CookieConsentRecord) *CookieConsentRecor
ConsentData: string(r.ConsentData),
Action: r.Action,
SdkVersion: r.SdkVersion,
Regulation: r.Regulation.String(),
CreatedAt: r.CreatedAt,
}
}