Skip to content

Commit

Permalink
Refactor function names for consistency and clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
arvvoid committed Sep 22, 2024
1 parent 8694c89 commit 0ec22e6
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 75 deletions.
26 changes: 13 additions & 13 deletions cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"golang.org/x/crypto/pkcs12"
)

// CertManager holds the private key, public certificate, and additional info
type CertManager struct {
// certManager holds the private key, public certificate, and additional info
type certManager struct {
privateKey *rsa.PrivateKey
publicCert *x509.Certificate
caCerts []*x509.Certificate // This holds any CA certs
Expand All @@ -25,8 +25,8 @@ type CertManager struct {
expire_days uint16
}

func NewCertManager() *CertManager {
return &CertManager{
func newCertManager() *certManager {
return &certManager{
privateKey: nil,
publicCert: nil,
caCerts: []*x509.Certificate{},
Expand All @@ -40,8 +40,8 @@ func NewCertManager() *CertManager {
}
}

// DecodeP12Cert loads and decodes a P12 certificate, extracting the private key, public cert, and CA certificates
func (cm *CertManager) DecodeP12Cert(certPath string, password string) error {
// decodeP12Cert loads and decodes a P12 certificate, extracting the private key, public cert, and CA certificates
func (cm *certManager) decodeP12Cert(certPath string, password string) error {
// Read the P12 file
certBytes, err := os.ReadFile(certPath)
if err != nil {
Expand Down Expand Up @@ -120,7 +120,7 @@ func (cm *CertManager) DecodeP12Cert(certPath string, password string) error {
}

// Extract the OIB
oib, err := cm.GetCertOIB()
oib, err := cm.getCertOIB()
if err != nil {
return fmt.Errorf("error extracting OIB: %v", err)
}
Expand All @@ -132,8 +132,8 @@ func (cm *CertManager) DecodeP12Cert(certPath string, password string) error {
return nil
}

// GetCertOIB extracts the OIB from the certificate's subject information
func (cm *CertManager) GetCertOIB() (string, error) {
// getCertOIB extracts the OIB from the certificate's subject information
func (cm *certManager) getCertOIB() (string, error) {
if cm.publicCert == nil {
return "", fmt.Errorf("certificate not loaded")
}
Expand All @@ -155,7 +155,7 @@ func (cm *CertManager) GetCertOIB() (string, error) {
return ex[1], nil
}

func (cm *CertManager) DisplayCertInfoText() string {
func (cm *certManager) DisplayCertInfoText() string {
if cm.publicCert == nil {
return "No public certificate available."
}
Expand All @@ -179,7 +179,7 @@ func (cm *CertManager) DisplayCertInfoText() string {
return result
}

func (cm *CertManager) DisplayCertInfoMarkdown() string {
func (cm *certManager) displayCertInfoMarkdown() string {
if cm.publicCert == nil {
return "No public certificate available."
}
Expand All @@ -203,7 +203,7 @@ func (cm *CertManager) DisplayCertInfoMarkdown() string {
return result
}

func (cm *CertManager) DisplayCertInfoHTML() string {
func (cm *certManager) displayCertInfoHTML() string {
if cm.publicCert == nil {
return "<p>No public certificate available.</p>"
}
Expand All @@ -228,7 +228,7 @@ func (cm *CertManager) DisplayCertInfoHTML() string {
return result
}

func (cm *CertManager) DisplayCertInfoKeyPoints() [][2]string {
func (cm *certManager) displayCertInfoKeyPoints() [][2]string {
var result [][2]string

if cm.publicCert == nil {
Expand Down
2 changes: 1 addition & 1 deletion checkcurrency.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

// Helper function to validate if the string is a valid currency format (with 2 decimal places)
func isValidCurrencyFormat(amount string) bool {
func IsValidCurrencyFormat(amount string) bool {
// Regex pattern to match valid decimal with exactly two decimal places
validCurrency := regexp.MustCompile(`^\d+(\.\d{2})$`)
return validCurrency.MatchString(amount)
Expand Down
30 changes: 15 additions & 15 deletions checkcurrency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,77 +7,77 @@ func TestCheckCurrency(t *testing.T) {
t.Logf("Testing currency validation...")

// Test a valid currency
if !isValidCurrencyFormat("100.00") {
if !IsValidCurrencyFormat("100.00") {
t.Fatalf("Expected currency 100.00 to be valid")
}

// Test a valid currency
if !isValidCurrencyFormat("13.12") {
if !IsValidCurrencyFormat("13.12") {
t.Fatalf("Expected currency 13.12 to be valid")
}

// Test a valid currency
if !isValidCurrencyFormat("1.12") {
if !IsValidCurrencyFormat("1.12") {
t.Fatalf("Expected currency 1.12 to be valid")
}

// Test a valid currency
if !isValidCurrencyFormat("134876348653847632687.99") {
if !IsValidCurrencyFormat("134876348653847632687.99") {
t.Fatalf("Expected currency 134876348653847632687.99 to be valid")
}

// Test an invalid currency
if isValidCurrencyFormat("100.001") {
if IsValidCurrencyFormat("100.001") {
t.Fatalf("Expected currency 100.001 to be invalid")
}

// Test an invalid currency
if isValidCurrencyFormat("100,00") {
if IsValidCurrencyFormat("100,00") {
t.Fatalf("Expected currency 100,00 to be invalid")
}

// Test an invalid currency
if isValidCurrencyFormat("100") {
if IsValidCurrencyFormat("100") {
t.Fatalf("Expected currency 100 to be invalid")
}

// Test an invalid currency
if isValidCurrencyFormat("abc") {
if IsValidCurrencyFormat("abc") {
t.Fatalf("Expected currency 100 to be invalid")
}

// Test an invalid currency
if isValidCurrencyFormat("abc.fg") {
if IsValidCurrencyFormat("abc.fg") {
t.Fatalf("Expected currency 100 to be invalid")
}

// Test an invalid currency
if isValidCurrencyFormat("abc.23") {
if IsValidCurrencyFormat("abc.23") {
t.Fatalf("Expected currency 100 to be invalid")
}

// Test an invalid currency
if isValidCurrencyFormat("100.ab") {
if IsValidCurrencyFormat("100.ab") {
t.Fatalf("Expected currency 100 to be invalid")
}

// Test negative currency
if isValidCurrencyFormat("-100.00") {
if IsValidCurrencyFormat("-100.00") {
t.Fatalf("Expected currency 100 to be invalid")
}

//Test zero
if !isValidCurrencyFormat("0.00") {
if !IsValidCurrencyFormat("0.00") {
t.Fatalf("Expected currency 0.00 to be valid")
}

//Test zero
if isValidCurrencyFormat("0.0") {
if IsValidCurrencyFormat("0.0") {
t.Fatalf("Expected currency 0.0 to be invalid")
}

//Test zero
if isValidCurrencyFormat("0") {
if IsValidCurrencyFormat("0") {
t.Fatalf("Expected currency 0 to be invalid")
}
}
12 changes: 6 additions & 6 deletions ciscert.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ type signatureCheckCIScert struct {
SSLverifyPoll *x509.CertPool
}

// ParseAndVerifyEmbeddedCerts parses the embedded certificates, verifies the chain, and returns the public key of the newest valid certificate
func ParseAndVerifyEmbeddedCerts(certFS embed.FS, dir string, pattern string) (*signatureCheckCIScert, error) {
// parseAndVerifyEmbeddedCerts parses the embedded certificates, verifies the chain, and returns the public key of the newest valid certificate
func parseAndVerifyEmbeddedCerts(certFS embed.FS, dir string, pattern string) (*signatureCheckCIScert, error) {
var newestCert *x509.Certificate
var roots *x509.CertPool

Expand Down Expand Up @@ -136,11 +136,11 @@ func ParseAndVerifyEmbeddedCerts(certFS embed.FS, dir string, pattern string) (*
}

// Get demo public key
func GetDemoPublicKey() (*signatureCheckCIScert, error) {
return ParseAndVerifyEmbeddedCerts(demoCISCert, "certDemo", "democis*.pem")
func getDemoPublicKey() (*signatureCheckCIScert, error) {
return parseAndVerifyEmbeddedCerts(demoCISCert, "certDemo", "democis*.pem")
}

// Get production public key
func GetProductionPublicKey() (*signatureCheckCIScert, error) {
return ParseAndVerifyEmbeddedCerts(prodCISCert, "certProd", "fiskalcis*.pem")
func getProductionPublicKey() (*signatureCheckCIScert, error) {
return parseAndVerifyEmbeddedCerts(prodCISCert, "certProd", "fiskalcis*.pem")
}
4 changes: 2 additions & 2 deletions ciscert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestParseAndVerifyEmbeddedCertsDemo(t *testing.T) {
t.Logf("Testing embedded CIS demo certificate...")

// Parse and verify the embedded CIS demo certificate
cert, err := GetDemoPublicKey()
cert, err := getDemoPublicKey()
if err != nil {
t.Fatalf("Failed to parse and verify embedded CIS demo certificate: %v", err)
}
Expand All @@ -36,7 +36,7 @@ func TestParseAndVerifyEmbeddedCertsProd(t *testing.T) {
t.Logf("Testing embedded CIS production certificate...")

// Parse and verify the embedded CIS production certificate
cert, err := GetProductionPublicKey()
cert, err := getProductionPublicKey()
if err != nil {
t.Fatalf("Failed to parse and verify embedded CIS production certificate: %v", err)
}
Expand Down
34 changes: 17 additions & 17 deletions ciscomm.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ import (
"time"
)

// SOAPEnvelope represents a SOAP envelope
type SOAPEnvelope struct {
XMLName xml.Name `xml:"soapenv:Envelope"`
XmlnsT string `xml:"xmlns:tns,attr"` // Declare the tns namespace
Xmlns string `xml:"xmlns:soapenv,attr"`
Body SOAPBody `xml:"soapenv:Body"`
// iSOAPEnvelope represents a SOAP envelope
type iSOAPEnvelope struct {
XMLName xml.Name `xml:"soapenv:Envelope"`
XmlnsT string `xml:"xmlns:tns,attr"` // Declare the tns namespace
Xmlns string `xml:"xmlns:soapenv,attr"`
Body iSOAPBody `xml:"soapenv:Body"`
}

// SOAPBody represents the body of a SOAP envelope
type SOAPBody struct {
// iSOAPBody represents the body of a SOAP envelope
type iSOAPBody struct {
XMLName xml.Name `xml:"soapenv:Body"`
Content []byte `xml:",innerxml"`
}

// SOAPEnvelopeNoNamespace represents a SOAP envelope without namespace (for CIS responses)
// iSOAPEnvelopeNoNamespace represents a SOAP envelope without namespace (for CIS responses)
// This to be more flexible and permissive on unmarhaling responses.
type SOAPEnvelopeNoNamespace struct {
XMLName xml.Name `xml:"Envelope"`
Body SOAPBodyNoNamespace `xml:"Body"`
type iSOAPEnvelopeNoNamespace struct {
XMLName xml.Name `xml:"Envelope"`
Body iSOAPBodyNoNamespace `xml:"Body"`
}

// SOAPBodyNoNamespace represents the body of a SOAP envelope without namespace (for CIS responses)
type SOAPBodyNoNamespace struct {
// iSOAPBodyNoNamespace represents the body of a SOAP envelope without namespace (for CIS responses)
type iSOAPBodyNoNamespace struct {
XMLName xml.Name `xml:"Body"`
Content []byte `xml:",innerxml"`
}
Expand Down Expand Up @@ -61,10 +61,10 @@ func (fe *FiskalEntity) GetResponse(xmlPayload []byte) ([]byte, int, error) {
}

// Prepare the SOAP envelope with the payload
soapEnvelope := SOAPEnvelope{
soapEnvelope := iSOAPEnvelope{
XmlnsT: DefaultNamespace,
Xmlns: "http://schemas.xmlsoap.org/soap/envelope/",
Body: SOAPBody{Content: xmlPayload},
Body: iSOAPBody{Content: xmlPayload},
}
// Marshal the SOAP envelope to XML
marshaledEnvelope, err := xml.Marshal(soapEnvelope)
Expand Down Expand Up @@ -93,7 +93,7 @@ func (fe *FiskalEntity) GetResponse(xmlPayload []byte) ([]byte, int, error) {
}

// Parse the SOAP response
var soapResp SOAPEnvelopeNoNamespace
var soapResp iSOAPEnvelopeNoNamespace
err = xml.Unmarshal(body, &soapResp)
if err != nil {
return body, resp.StatusCode, fmt.Errorf("failed to unmarshal SOAP response: %w", err)
Expand Down
14 changes: 7 additions & 7 deletions fiskal-schema-helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func Naknade(values [][]string) (*NaknadeType, error) {
}
feeName := v[0]
feeAmount := v[1]
if !isValidCurrencyFormat(feeAmount) {
if !IsValidCurrencyFormat(feeAmount) {
return nil, errors.New("the second element of each inner array must be a valid currency format (fee amount)")
}
naknade[i] = &NaknadaType{NazivN: feeName, IznosN: feeAmount}
Expand Down Expand Up @@ -243,14 +243,14 @@ func OtherTaxes(values [][]interface{}) (*OstaliPoreziType, error) {
if !ok {
return nil, errors.New("the third element of each inner array must be a string (base)")
}
if !isValidCurrencyFormat(base) {
if !IsValidCurrencyFormat(base) {
return nil, errors.New("the third element of each inner array must be a valid currency format (base)")
}
amount, ok := v[3].(string)
if !ok {
return nil, errors.New("the fourth element of each inner array must be a string (amount)")
}
if !isValidCurrencyFormat(amount) {
if !IsValidCurrencyFormat(amount) {
return nil, errors.New("the fourth element of each inner array must be a valid currency format (amount)")
}
porezi[i] = &PorezOstaloType{Naziv: name, Stopa: rate, Osnovica: base, Iznos: amount}
Expand Down Expand Up @@ -300,14 +300,14 @@ func NewPNP(values [][]interface{}) (*PorezNaPotrosnjuType, error) {
if !ok {
return nil, errors.New("the second element of each inner array must be a string (tax base)")
}
if !isValidCurrencyFormat(taxBase) {
if !IsValidCurrencyFormat(taxBase) {
return nil, errors.New("the second element of each inner array must be a valid currency format (tax base)")
}
taxAmount, ok := v[2].(string)
if !ok {
return nil, errors.New("the third element of each inner array must be a string (tax amount)")
}
if !isValidCurrencyFormat(taxAmount) {
if !IsValidCurrencyFormat(taxAmount) {
return nil, errors.New("the third element of each inner array must be a valid currency format (tax amount)")
}
porezi[i] = &PorezType{Stopa: taxRate, Osnovica: taxBase, Iznos: taxAmount}
Expand Down Expand Up @@ -357,14 +357,14 @@ func NewPdv(values [][]interface{}) (*PdvType, error) {
if !ok {
return nil, errors.New("the second element of each inner array must be a string (tax base)")
}
if !isValidCurrencyFormat(taxBase) {
if !IsValidCurrencyFormat(taxBase) {
return nil, errors.New("the second element of each inner array must be a valid currency format (tax base)")
}
taxAmount, ok := v[2].(string)
if !ok {
return nil, errors.New("the third element of each inner array must be a string (tax amount)")
}
if !isValidCurrencyFormat(taxAmount) {
if !IsValidCurrencyFormat(taxAmount) {
return nil, errors.New("the third element of each inner array must be a valid currency format (tax amount)")
}
porezi[i] = &PorezType{Stopa: taxRate, Osnovica: taxBase, Iznos: taxAmount}
Expand Down
Loading

0 comments on commit 0ec22e6

Please sign in to comment.