util

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 26, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Copyright 2025 The Hulo Authors. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

Copyright 2025 The Hulo Authors. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

Copyright 2025 The Hulo Authors. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Download added in v0.2.0

func Download(url string, out io.Writer) error

func Exists added in v0.2.0

func Exists(path string) bool

func LoadConfigure added in v0.2.0

func LoadConfigure[T any](path string) (T, error)

func SaveConfigure added in v0.2.0

func SaveConfigure[T any](path string, config T) error

Types

type Allocator added in v0.2.0

type Allocator struct {
	// contains filtered or unexported fields
}

Allocator manages variable name allocation and mapping

It provides a way to generate unique variable names and maintain a mapping between original names and generated names. This is useful for code generation where you need to ensure variable names don't conflict and potentially obfuscate the original names.

Example:

allocator := NewAllocator(
	WithLock(&util.NoOpLocker{}),
	WithPrefix("_var_"),
	WithInitialCounter(100),
)
name := allocator.AllocName("userCount")  // might return "_var_101"

func NewAllocator added in v0.2.0

func NewAllocator(opts ...AllocatorOption) *Allocator

NewAllocator creates a new Allocator with the given options

Example:

allocator := NewAllocator(
	WithLock(&util.NoOpLocker{}),
	WithPrefix("_var_"),
	WithInitialCounter(100),
)

func (*Allocator) AllocName added in v0.2.0

func (a *Allocator) AllocName(originalName string) string

AllocName allocates a new unique name for the given original name

If the original name has already been allocated, it returns the previously generated name. Otherwise, it generates a new unique name.

Example:

name := allocator.AllocName("count")  // might return "_v1"
name2 := allocator.AllocName("count") // returns the same "_v1"

func (*Allocator) GetGeneratedName added in v0.2.0

func (a *Allocator) GetGeneratedName(originalName string) string

GetGeneratedName returns the generated name for an original name

Returns empty string if the original name is not found.

Example:

genName := allocator.GetGeneratedName("count")  // returns "_v1"

func (*Allocator) GetOriginalName added in v0.2.0

func (a *Allocator) GetOriginalName(generatedName string) string

GetOriginalName returns the original name for a generated name

Returns empty string if the generated name is not found.

Example:

origName := allocator.GetOriginalName("_v1")  // returns "count"

type AllocatorOption added in v0.2.0

type AllocatorOption func(*Allocator)

AllocatorOption defines a function that configures an Allocator

func WithInitialCounter added in v0.2.0

func WithInitialCounter(counter uint64) AllocatorOption

WithInitialCounter sets the initial counter value

Example:

allocator := NewAllocator(WithInitialCounter(100))

func WithLock added in v0.2.0

func WithLock(lock sync.Locker) AllocatorOption

WithLock sets the lock implementation for the Allocator

Example:

allocator := NewAllocator(WithLock(&util.NoOpLocker{}))

func WithPrefix added in v0.2.0

func WithPrefix(prefix string) AllocatorOption

WithPrefix sets the prefix for generated variable names

Example:

allocator := NewAllocator(WithPrefix("_var_"))

type NoOpLocker

type NoOpLocker struct{}

NoOpLocker is a virtual implementation of Locker

This is a no-op implementation that does nothing when Lock() or Unlock() is called. It's useful for testing or when you want to maintain the locking interface without actual synchronization.

func (*NoOpLocker) Lock

func (n *NoOpLocker) Lock()

Lock is a no-op implementation

func (*NoOpLocker) Unlock

func (n *NoOpLocker) Unlock()

Unlock is a no-op implementation

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL